簡體   English   中英

Python導入可在2.7中使用,但在3.2+中將失敗

[英]Python import works in 2.7 but fails in 3.2+

導入軟件包:

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'

知道為什么會這樣嗎?

看來您正在面對Python的一項不兼容的更改: 相對導入 (aka PEP 328)。

這應該工作:

from .unique_upload import unique_file_upload

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM