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

1

17

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!

Driftage answered 31/3, 2011 at 9:46 Comment(1)
Duplicates: #5227393 #1113118 #1586256Gyroscope
D
13

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.

Disassociate answered 31/3, 2011 at 9:56 Comment(4)
so change it to just import mof_compiler is ok?Driftage
@Remko: But I believe the answer to your question is No, since implicit relative imports are now strongly discouraged. I believe the explicit equivalent (which is still relative) is this: from . import mof_compilerTimber
my folder already has __init__.py and I'm having the same errorOttie
This answer is incomplete: mkdir /tmp/pymodtest; cd /tmp/pymodtest; touch __init__.py a.py; echo "from . import a" > b.py; python2 b.py yields the same error, though clearly a __package__ file is present.Warrantable

© 2022 - 2024 — McMap. All rights reserved.