问题
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