导入包:
from unique_upload import unique_file_upload
适用于Python 2.7但在Python 3.2及以上版本中失败:
ImportError: cannot import name unique_file_upload
项目结构是:
test/
__init__.py
test_unique_upload # <-- calling from unique_upload import unique_file_upload here
unique_upload/
__init__.py
unique_upload
unique_upload/__init__.py
包含:
__version__ = '0.2.0'
from unique_upload import unique_file_upload
完整堆栈跟踪:
ImportError: Failed to import test module: test_unqiue_upload
Traceback (most recent call last):
File "/opt/python/3.5.0/lib/python3.5/unittest/loader.py", line 428, in _find_test_path
module = self._get_module_from_name(name)
File "/opt/python/3.5.0/lib/python3.5/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
File "/home/travis/build/agconti/django-unique-upload/test/test_unqiue_upload.py", line 4, in <module>
from unique_upload import unique_file_upload
File "/home/travis/build/agconti/django-unique-upload/unique_upload/__init__.py", line 2, in <module>
from unique_upload import unique_file_upload
ImportError: cannot import name 'unique_file_upload'
知道为什么会这样吗?
答案 0 :(得分:2)
您似乎面临着Python不兼容的变化之一:Relative imports,又名PEP 328。
这应该有效:
from .unique_upload import unique_file_upload