环境:
- python 3.8.10
- Django 3.2.5
项目目录结构:
PrjRoot
|my_project
|— __init__.py
|— 其它
|my_app
|— __init__.py
|— my_util.py
|— 其它
- 原因: 环境变量中没有找到工具所在的目录,添加到环境变量中即可
其中my_util.py
是我自己写的工具类,在views.py中导入,在用命令行启动时抛出如下错误:
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/hq/PycharmProjects/django_learn/path_test/urls.py", line 2, in <module>
from . import views
File "/Users/hq/PycharmProjects/django_learn/path_test/views.py", line 2, in <module>
import my_util
ModuleNotFoundError: No module named 'my_util'
解决方法:
在my_app/__init__.py
中加入下面的代码
import sys
sys.path.append(__path__[0])