ModuleNotFoundError when running python script from a batch file

ε祈祈猫儿з 提交于 2019-12-14 02:38:27

问题


I have a simple python script called sc.py that translates a word. This is my code:

#! 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)

The script works as expected when I run it from command line, for example like this:

python sc.py something it

And I get the expected result:

qualcosa

Then I created a batch file so I can call this script from anywhere by just writing translate. This is my batch file called translate.bat :

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

I've added the folder where the batch file to the path, but when I try to run it I get ModuleNotFoundError like this:

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'

I have no idea why this is happening, has anyone encountered anything similar?


回答1:


That could be there is multiple version of python installed or install googletrans for all version of python.

Use

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

instead of

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


来源:https://stackoverflow.com/questions/54275817/modulenotfounderror-when-running-python-script-from-a-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!