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!

1

1 Answer 1

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.

4
  • 1
    so change it to just import mof_compiler is ok?
    – Remko
    Commented Mar 31, 2011 at 10:00
  • 2
    @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_compiler
    – Jon Coombs
    Commented Jan 30, 2014 at 18:22
  • 15
    my folder already has __init__.py and I'm having the same error Commented Jul 4, 2015 at 17:14
  • 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.
    – Clément
    Commented May 28, 2017 at 21:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.