来自批处理文件的 运行 python 脚本时出现 ModuleNotFoundError

ModuleNotFoundError when running python script from a batch file

我有一个名为 sc.py 的简单 python 脚本可以翻译单词。这是我的代码:

#! python3

from googletrans import Translator
import sys

translator = Translator()

dest = 'hr'

if len(sys.argv) > 1:
    try:
        dest = sys.argv[2]
    except:
        pass

    translated = translator.translate(sys.argv[1],  dest = dest)
    print(translated.text)

当我从命令行 运行 脚本按预期工作时,例如:

python sc.py something it

我得到了预期的结果:

qualcosa

然后我创建了一个批处理文件,这样我就可以通过编写 translate 从任何地方调用这个脚本。这是我的批处理文件 translate.bat :

@py.exe D:\path\to\the\script\sc.py %*

我已经将批处理文件所在的文件夹添加到路径中,但是当我尝试 运行 它时,我得到 ModuleNotFoundError 像这样:

Traceback (most recent call last):
  File "D:\path\to\the\script\sc.py", line 3, in <module>
    from googletrans import Translator
ModuleNotFoundError: No module named 'googletrans'

我不知道为什么会这样,有没有人遇到过类似的事情?

可能是安装了多个版本的 python 或为所有版本的 python 安装了 googletrans

使用

python D:\path\to\the\script\sc.py %*

而不是

@py.exe D:\path\to\the\script\sc.py %* 

对于python 3.7,在CMD中输入以下内容:

CD C:\Users\[path]\Continuum\anaconda3\Lib\site-packages

然后:

pip3 install googletrans