16

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!

Community
  • 1
  • 1
Remko
  • 7,214
  • 2
  • 32
  • 52
  • 3
    Duplicates: http://stackoverflow.com/questions/5226893/understanding-a-chain-of-imports-in-python http://stackoverflow.com/questions/1112618/import-python-package-from-local-directory-into-interpreter http://stackoverflow.com/questions/1585756/python-do-relative-imports-mean-you-cant-execute-a-subpackage-by-itself – Lennart Regebro Mar 31 '11 at 11:36

1 Answers1

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.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • 1
    so change it to just import mof_compiler is ok? – Remko Mar 31 '11 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 Jan 30 '14 at 18:22
  • 15
    my folder already has `__init__.py` and I'm having the same error – bubakazouba Jul 04 '15 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 May 28 '17 at 21:07