无法导入名称 import_by_path

Posted

技术标签:

【中文标题】无法导入名称 import_by_path【英文标题】:cannot import name import_by_path 【发布时间】:2014-03-04 11:51:09 【问题描述】:

请求方法:GET 请求网址:http://ec2-145-28-57-102.compute-1.amazonaws.com/reports/ Django 版本:1.3.1 异常类型:ImportError 异常值:

异常位置:/usr/lib/python2.6/site-packages/django/contrib/formtools/wizard/storage/init.py 在第 2 行 Python 可执行文件:/usr/bin/python Python版本:2.6.6 Python 路径:

['/var/www/stacks/apps/project/apps', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info', '/var/www/stacks/apps/']

Traceback:
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
  101.                             request.path_info)
File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py" in resolve
  252.                     sub_match = pattern.resolve(new_path)
File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py" in resolve
  250.             for pattern in self.url_patterns:
File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py" in _get_url_patterns
  279.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py" in _get_urlconf_module
  274.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python2.6/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/var/www/stacks/apps/project/apps/reports/urls.py" in <module>
  8. from views import (project_report, project_report_new, CustomReportList, delete_custom_report,
File "/var/www/stacks/apps/project/apps/reports/views.py" in <module>
  15. from forms import PROJECTReportForm, PROJECTReportFormNew, CrossCheckForm 
File "/var/www/stacks/apps/project/apps/reports/forms.py" in <module>
  29. from django.contrib.formtools.wizard.views import SessionWizardView
File "/usr/lib/python2.6/site-packages/django/contrib/formtools/wizard/views.py" in <module>
  13. from django.contrib.formtools.wizard.storage import get_storage
File "/usr/lib/python2.6/site-packages/django/contrib/formtools/wizard/storage/__init__.py" in <module>
  2. from django.utils.module_loading import import_by_path

Exception Type: ImportError at /reports/
Exception Value: cannot import name import_by_path

异常类型:/reports/ 处的 ImportError 异常值:

【问题讨论】:

【参考方案1】:

The docs 声明 import_by_path 在 Django 1.6 中是新的,而问题提到了 Django 1.3.1。也许您安装了两个版本?检查 Python 路径上的所有目录。

【讨论】:

【参考方案2】:

import_by_path 已从 Django 1.7 中弃用 所以这实际上是因为检测 Django 版本的方式存在错误:

    if django.get_version() >= "1.7":
        from django.utils.module_loading import import_string
    else:
        from django.utils.module_loading import import_by_path as import_string

由于词法比较,不适用于 Django >= 1.10。

需要编辑和分叉库或在您的本地环境中您可以更改如下:

编辑站点包/omnibus/management/commands/omnibusd.py

from django.utils.module_loading import import_string as import_by_path

【讨论】:

以上是关于无法导入名称 import_by_path的主要内容,如果未能解决你的问题,请参考以下文章

ImportError:无法导入名称而无法导入类

ImportError:无法导入名称 VarianceThreshold

ImportError:无法导入名称“AFAVSignature”

ImportError:无法导入名称“评估”(来自意外导入评估)

ImportError:无法导入名称“WebClient”

ImportError:无法导入名称发现