Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

ImportError on python 3, worked fine on python 2.7

I'm getting an error when importing my code_parsing package with Python 3.2.

Directory code_parsing is within PYTHONPATH and contains the following files (some others too, but irrelvant here)

code_parsing/__init__.py
code_parsing/ada.py

__init__.py contains:

from ada import *

When importing my module I get this error:

>>> import code_parsing
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "K:CODEpythonlibcode_parsing\__init__.py", line 1, in <module>
    from ada import *
ImportError: No module named ada

With python 2.7 on the same machine with the same environment it works fine.

Some precisions:

  • Others import in the same directory using the same directory/__init__.py structure work fine.
  • ada.py is a pure python file, no special compiled/cython/.pyd stuff.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Python 3 uses absolute imports. Any unqualified name is imported as a top-level module.

You don't have a top-level ada module. You have a code_parsing.ada module instead so the following will work:

from code_parsing.ada import *

or use an explicit 'local package' reference:

from .ada import *

You can force the same behaviour in Python 2 with:

from __future__ import absolute_import

See PEP 328 – Imports: Multi-Line and Absolute/Relative for details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...