Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempted relative import in non-package (after 2to3)

After converting to Python 3.x using 2to3 (see my previous question), I get this error during the build:

  File "setup.py", line 28, in <module>
    from . import mof_compiler
ValueError: Attempted relative import in non-package

The code:

from . import mof_compiler
mof_compiler._build()

But I don’t know why this is wrong, since mof_compiler is in the same dir as setup.py. Please help!

like image 651
Remko Avatar asked Mar 31 '11 09:03

Remko


People also ask

How do you use relative import in Python?

Relative imports use dot(.) notation to specify a location. Single dot specifies that the module is in the current directory, two dots indicate that module is in its parent directory of the current location and three dots indicate that it is in grandparent directory and so on.

What is __ package __?

That is, if a module is in a package, __package__ is set to the package name to enable explicit relative imports. Specifically: When the module is a package, its __package__ value should be set to its __name__ .


1 Answers

Since there is no __init__.py, the working directory is a non-package.

You don't need a relative import.

Or.

You need an __init__.py to make a package.

like image 54
S.Lott Avatar answered Oct 01 '22 16:10

S.Lott