错误显示:控制台中的ModuleNotFoundError

问题描述 投票:0回答:2

我目前正在为在线课程进行翻译练习,但是我的在线结构没有得到正确的答案。我在PyCharm IDE中使用库“翻译”。代码是:

from translate import Translator
translator = Translator(to_lang="es")

try:
    with open("./totrans.txt", mode="r", encoding="utf-8") as file:
        read_file = file.read()
        translation = translator.translate(read_file)
        with open("./translatd.txt", mode="w") as final_file:
            final_file.write(translation)
except FileNotFoundError as e:
    print("Check your file path")

并且不断显示以下错误:

(venv) D:\Programación\Python\Translator2\venv>python main.py
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    from translate import Translator
ModuleNotFoundError: No module named 'translate'

在我的目录中,这是一个定义有内部Translator的翻译模块。

我已经尝试使用python3附带的“运行”工具来运行代码,并且没有出现任何问题,可以正确执行代码,但是我想通过控制台来完成。

我也尝试过放置孔目录路径,如下所示:

from translate.translate import Translator

[也将翻译以(.py)结尾(尽管我知道这是不正确的,但请尝试尝试)。我也尝试在控制台中输入python3 main.py而不是python main.py

如果能回答这个问题,我将非常高兴。非常感谢社区!

我目前正在为在线课程进行翻译,但是我的在线结构没有得到正确的答案。我在PyCharm IDE中使用库“翻译”。代码是:从...

python python-3.x error-handling console
2个回答
0
投票

有一些选择。在PyCharm中,您可以将文件夹添加到根目录中,如下所示:


-1
投票

如果您的main.py在您要尝试调用的包中,则可以尝试此操作,看看它是否有效:from .translate import Translator(请参见“翻译”前的点)

© www.soinside.com 2019 - 2024. All rights reserved.