简体   繁体   English

在PyCharm外部运行代码时的ModuleNotFoundError

[英]ModuleNotFoundError when running the code outside PyCharm

I have a module called my_module.py which is under the directory test/my_modules/my_module.py . 我有一个名为my_module.py的模块,它位于test/my_modules/my_module.py目录下。

When I am trying to import that module in a different module using: 当我尝试使用以下方法在另一个模块中导入该模块时:

from test.my_modules.my_module import something

everything seems to be OK when running the code from PyCharm but when running the code outside PyCharm I get the following error: 从PyCharm运行代码时,一切似乎都没问题,但是当在PyCharm外部运行代码时,我收到以下错误:

ModuleNotFoundError: No module named 'test'

PS: Using Python 3.6 so __init__.py shouldn't be required in every module. PS:使用Python 3.6所以每个模块都不需要__init__.py

The working directory of PyCharm and the one where you are running the code outside of PyCharm are different. PyCharm的工作目录和你在PyCharm之外运行代码的工作目录是不同的。 Hence, it does not find the module test anymore. 因此,它不再找到模块test

You need to change the relative path, where you want to import it from or add the folder, where the module test is located to your PYTHONPATH environmental variable, so that Python can find it. 您需要更改相关路径,将其从中导入或将模块test所在的文件夹添加到PYTHONPATH环境变量中,以便Python可以找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM