无法在Django 1.6中导入自定义应用

在 Django 1.6 中,如果你无法导入自定义应用,通常是由于一些常见的配置问题,如 INSTALLED_APPS 配置不正确、应用目录结构不符合 Django 规范,或者路径问题。

1、问题背景

在使用 Django 1.6 创建一个简单的项目时,遇到了导入自定义应用的问题。项目结构如下:

复制代码
root/
    manage.py
    mysite/
        apps/
            __init__.py
            app1/
                urls.py
                views.py
        settings.py
        urls.py
        __init__.py

settings.py 文件中,已将 app1 安装到 INSTALLED_APPS 中。当尝试在根 urls.py 中修改导入自定义应用的代码时,却遇到了以下错误:

复制代码
Traceback (most recent call last):
  File "D:\Python33\lib\site-packages\django\core\urlresolvers.py", line 339, in         urlconf_module
    return self._urlconf_module
AttributeError: 'RegexURLResolver' object has no attribute '_urlconf_module'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Python33\lib\site-packages\django\core\handlers\base.py", line 101, in         get_response
    resolver_match = resolver.resolve(request.path_info)
  File "D:\Python33\lib\site-packages\django\core\urlresolvers.py", line 318, in resolve
    for pattern in self.url_patterns:
  File "D:\Python33\lib\site-packages\django\core\urlresolvers.py", line 346, in         url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "D:\Python33\lib\site-packages\django\core\urlresolvers.py", line 341, in         urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "D:\Python33\lib\importlib\__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1584, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 584, in _check_name_wrapper
  File "<frozen importlib._bootstrap>", line 1022, in load_module
  File "<frozen importlib._bootstrap>", line 1003, in load_module
  File "<frozen importlib._bootstrap>", line 560, in module_for_loader_wrapper
  File "<frozen importlib._bootstrap>", line 868, in _load_module
  File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
  File "D:/project/worldcup\worldcup\urls.py", line 4, in <module>
    url(r'^app1/', include('apps.app1.urls')),
  File "D:\Python33\lib\site-packages\django\conf\urls\__init__.py", line 26, in include
    urlconf_module = import_module(urlconf_module)
  File "D:\Python33\lib\importlib\__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1584, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1512, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1584, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1512, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1584, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1529, in _find_and_load_unlocked
ImportError: No module named 'apps'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Python33\lib\wsgiref\handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "D:\Python33\lib\site-packages\django\contrib\staticfiles\handlers.py", line 67, in __call__
    return self.application(environ, start_response)
  File "D:\Python33\lib\site-packages\django\core\handlers\wsgi.py", line 206, in __call__
    response = self.get_response(request)
  File "D:\Python33\lib\site-packages\django\core\handlers\base.py", line 196, in         get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "D:\Python33\lib\site-packages\django\core\handlers\base.py", line 231, in         handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "D:\Python33\lib\site-packages\django\views\debug.py", line 69, in                 technical_500_response
    html = reporter.get_traceback_html()
  File "D:\Python33\lib\site-packages\django\views\debug.py", line 323, in                 get_traceback_html
    c = Context(self.get_traceback_data())
  File "D:\Python33\lib\site-packages\django\views\debug.py", line 281, in                 get_traceback_data
    frames = self.get_traceback_frames()
  File "D:\Python33\lib\site-packages\django\views\debug.py", line 428, in                 get_traceback_frames
    pre_context_lineno, pre_context, context_line, post_context =                         self._get_lines_from_file(filename, lineno, 7, loader,                 module_name)
  File "D:\Python33\lib\site-packages\django\views\debug.py", line 379, in                 _get_lines_from_file
    source = loader.get_source(module_name)
  File "<frozen importlib._bootstrap>", line 605, in _requires_frozen_wrapper
ImportError: importlib._bootstrap is not a frozen module
[08/Dec/2013 22:25:59] "GET /user/hello/ HTTP/1.1" 500 59

2、解决方案

要解决此问题,可以执行以下步骤:

  1. 确保 apps 目录在 PYTHONPATH 中。可以通过在命令行中输入 echo $PYTHONPATH 来检查 PYTHONPATH。如果 apps 目录不在 PYTHONPATH 中,可以通过在 .bashrc.zshrc 文件中添加以下行来将其添加到 PYTHONPATH 中:

    export PYTHONPATH=$PYTHONPATH:/path/to/apps

  2. 确保 apps 目录包含一个 __init__.py 文件。如果没有,请创建一个空的 __init__.py 文件。

  3. 确保 apps/app1 目录包含一个 urls.py 文件。如果没有,请创建一个空的 urls.py 文件。

  4. 确保 apps/app1 目录包含一个 views.py 文件。如果没有,请创建一个空的 views.py 文件。

  5. mysite/settings.py 文件中,确保 INSTALLED_APPS 设置正确。应该如下所示:

    INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    '

如果这些步骤都完成了,并且问题依然存在,调试模式下的错误日志可以提供更详细的信息来帮助解决问题。

相关推荐
0白露43 分钟前
Apifox Helper 与 Swagger3 区别
开发语言
Tanecious.2 小时前
机器视觉--python基础语法
开发语言·python
叠叠乐2 小时前
rust Send Sync 以及对象安全和对象不安全
开发语言·安全·rust
·薯条大王2 小时前
MySQL联合查询
数据库·mysql
Tttian6223 小时前
Python办公自动化(3)对Excel的操作
开发语言·python·excel
morris1314 小时前
【redis】redis实现分布式锁
数据库·redis·缓存·分布式锁
hycccccch4 小时前
Canal+RabbitMQ实现MySQL数据增量同步
java·数据库·后端·rabbitmq
独好紫罗兰4 小时前
洛谷题单2-P5713 【深基3.例5】洛谷团队系统-python-流程图重构
开发语言·python·算法
这个懒人5 小时前
深入解析Translog机制:Elasticsearch的数据守护者
数据库·elasticsearch·nosql·translog
每次的天空5 小时前
Android学习总结之算法篇四(字符串)
android·学习·算法