我试图了解我的问题在哪里。这是我的项目
└── test_folder
|___ __init__.py
├── foled_1
│ ├── __init__.py
│ └── file_1.py
└── foled_2
├── __init__.py
├── file_2.py
当我尝试从pycharm中运行file_1.py的脚本时,一切都对我来说非常有效。但是,当我尝试通过终端(python3 file_1.py(运行此文件时,我会收到错误:
Traceback (most recent call last):
File "file_1.py", line 1, in <module>
from foled_2.file_2 import a
ModuleNotFoundError: No module named 'foled_2'
file_1.py的脚本是:
from foled_2.file_2 import a
print(a)
print("Hello")
file_2.py的脚本是:
a = 2
如果您像python foled_1/file_1.py
这样的测试文件夹运行文件。
为什么这不起作用?
这是因为在Pycharm中,您将test_folder定义为基本项目文件夹。 每当您运行脚本时,它都会从该文件夹运行,即将术语当前路径作为基本文件夹。
在您的代码中,您已经提到您需要foled_2
,因为您在FOLED_1路径并在此处运行脚本,所以Python无法找到foled_2
文件夹/软件包。
Solutuion
因此,如果您想运行脚本,请从终端中的TestFolder运行它。 python foled_1/file_1.py