从批处理文件运行python脚本时出现ModuleNotFoundError [英] ModuleNotFoundError when running python script from a batch file

查看:408
本文介绍了从批处理文件运行python脚本时出现ModuleNotFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我得到了预期的结果:

qualcosa

然后,我创建了一个批处理文件,因此我只需编写translate就可以从任何地方调用此脚本.这是我的批处理文件,名为translate.bat:

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 %*

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

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?

推荐答案

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

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

使用

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

代替

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

这篇关于从批处理文件运行python脚本时出现ModuleNotFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆