django中app模块找不到
环境介绍
django verison=3.13, python version=3.9.0
因为要使用多个app,遂在项目目录下创建了apps
这个文件夹放置其他app,在settings.py
中也添加了系统路径,但问题就在这里,如下:
Pathlib方式
sys.path.append(Path(BASE_DIR).joinpath('apps'))
然后启动的时候就报错了。
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/local/Cellar/python@3.9/3.9.0/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 950, in _bootstrap_inner
self.run()
File "/usr/local/Cellar/python@3.9/3.9.0/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 888, in run
self._target(*self._args, **self._kwargs)
File "/Users/dapeng/.virtualenvs/dj_new/lib/python3.9/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/dapeng/.virtualenvs/dj_new/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "/Users/dapeng/.virtualenvs/dj_new/lib/python3.9/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
raise _exception[1]
File "/Users/dapeng/.virtualenvs/dj_new/lib/python3.9/site-packages/django/core/management/__init__.py", line 357, in execute
autoreload.check_errors(django.setup)()
File "/Users/dapeng/.virtualenvs/dj_new/lib/python3.9/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/dapeng/.virtualenvs/dj_new/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/dapeng/.virtualenvs/dj_new/lib/python3.9/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/Users/dapeng/.virtualenvs/dj_new/lib/python3.9/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/local/Cellar/python@3.9/3.9.0/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'user'
os.path方式
sys.path.append(os.path.join(BASE_DIR, 'apps'))
就很正常的启动了。
疑问
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
print看下结果啊,或者debug启动打断点看看这两者的输出差异