【问题标题】:ModuleNotFoundError when running python script from a batch file从批处理文件运行 python 脚本时出现 ModuleNotFoundError
【发布时间】:2019-01-20 11:04:05
【问题描述】:

我有一个名为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-3.x batch-file google-translate


    【解决方案1】:

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

    使用

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

    而不是

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

    【讨论】:

      【解决方案2】:

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

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

      然后:

      pip3 install googletrans
      

      【讨论】:

        猜你喜欢
        • 2019-03-22
        • 2021-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-17
        相关资源
        最近更新 更多