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'))

就很正常的启动了。

疑问🤔️

有没有人遇到这个问题的?Pathlib在被django解析的时候出问题了?

Stay hungry, stay foolish.
讨论数量: 1
Jason990420

sys.path

  1. 字符串列表,用于指定模块的搜索路径。 从环境变量PYTHONPATH初始化,再加上与安装有关的默认值。

  2. 在程序启动时进行初始化,该列表的第一项path[0]是包含用于调用Python解释器的脚本的目录。 如果脚本目录不可用(例如, 以交互方式调用解释器或从标准输入中读取脚本),则path[0]为空字符串,该字符串将引导Python首先搜索当前目录中的模块。 请注意,由于PYTHONPATH的结果,在插入条目之前插入了脚本目录。

  3. 程序可以出于自己的目的随意修改此列表。 只能将以下兩种類型添加到sys.path中。

    • strings, 字符串
    • bytes, 字节
  4. 导入过程中将忽略所有其他数据类型。

>>> BASE_DIR = 'D:/tmp'
>>> Path(BASE_DIR).joinpath('apps')
WindowsPath('D:/tmp/apps')
>>> type(Path(BASE_DIR).joinpath('apps'))
<class 'pathlib.WindowsPath'>
>>> os.path.join(BASE_DIR, 'apps')
'D:/tmp\\apps'
>>> type(os.path.join(BASE_DIR, 'apps'))
<class 'str'>
3年前 评论
runstone (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!