app-main-folder
/local
/__init__.py
/run.py
constants.py
我试图从run.py
中的常量导入--它抛出了这个错误
Traceback (most recent call last):
File "local/run.py", line 4, in <module>
from init import app
File "/home/manavarthivenkat/ANUESERVICES--BACKEND/local/init.py", line 5, in <module>
from constants import BaseConstants
ModuleNotFoundError: No module named 'constants'
发布于 2022-11-27 00:38:28
pip install constants
在您的shell上尝试以下操作,并尝试运行run.py
确保加载常量库
发布于 2022-11-27 00:53:14
这是因为Python假定所有python文件都在当前目录中。您应该告诉编译器您正在寻找其他地方的文件。
from app-main-folder.constants import constants # assuming constants is the name of your class in that constants.py
发布于 2022-11-27 01:03:25
您可以使用sys.path.append
告诉Python解释器在哪里搜索模块。
首先,可以检查当前的Python路径。
import sys
from pprint import pprint
pprint(sys.path)
如果输出中没有包含到constants.py
目录的路径,则可以通过以下方式手动添加该路径
import sys
sys.path.append("/path/to/constants.py")
https://stackoverflow.com/questions/74586603
复制相似问题