37

I have a module named extended.py which contains the following line:

from .basic import BasicModule

and the file basic.py resides in the same directory as does __init__.py. However, when I try to run it as:

python extended.py

I get the error:

ValueError: Attempted relative import in non-package

Also adding the line:

from __future__ import absolute_import

does not solve the problem. Maybe I am too tired to see the obvious - but I don't see the problem here.

NikT
  • 1,590
  • 2
  • 16
  • 29
Alex
  • 41,580
  • 88
  • 260
  • 469
  • 1
    possible duplicate of [Attempted relative import in non-package even with \_\_init\_\_.py](http://stackoverflow.com/questions/11536764/attempted-relative-import-in-non-package-even-with-init-py) – BrenBarn Feb 02 '13 at 17:33
  • how about `python -m parent_directory.extended`? – SparkAndShine Mar 23 '16 at 16:25
  • Possible duplicate of [How to do relative imports in Python?](http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python) – Ani Menon May 03 '16 at 10:27

1 Answers1

59

Relative imports only work for packages, but when you importing in extended.py you are running a top-level module instead.

The current directory may hold a __init__.py file but that doesn't make exended.py part of a package yet.

For something to be considered a package, you need to import the directory name instead. The following would work:

main.py

packagename\
    __init__.py
    basic.py
    extended.py

then in main.py put:

import packagename.extended

and only then is extended part of a package and do relative imports work.

The relative import now has something to be relative to, the packagename parent.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 3
    But how am I able to run `unittests` in that case? Do I hav to specify a python argument (-m) for these cases of imports? – Alex Feb 02 '13 at 17:15
  • 1
    For anything not defined in a package, don't use relative imports, use absolute imports instead. A unittest script is not part of the package, it is a external script. – Martijn Pieters Feb 02 '13 at 17:17
  • I always thought one should put the unittests inside the same module? Is there a good-practices or a PEP on the unitest with python? – Alex Feb 02 '13 at 17:18
  • 2
    Sure, bundle the tests with the package, but perhaps not run the tests as scripts *directly*. Use another tool to discover tests instead, such as `nose`. – Martijn Pieters Feb 02 '13 at 17:21
  • Ok that is fair enough. Thanks for your help. – Alex Feb 02 '13 at 17:21
  • If you use setuptools, you can use that too; see [`test` - build a package and run a test suite](http://packages.python.org/distribute/setuptools.html#test-build-package-and-run-a-unittest-suite) for more info. The tests are then imported *from* the package and relative imports will work correctly. – Martijn Pieters Feb 02 '13 at 17:23
  • I'm confused -- the OP never suggested importing extended.py -- he wants to import basic.py when extended.py is executed. Also, what is main.py? the OP never mentioned that file – Paul Mar 09 '17 at 18:01
  • @Paul: I've clarified the first sentence, that was indeed confusing. – Martijn Pieters Mar 09 '17 at 21:21
  • @Paul: `main.py` is a new file my answer introduces, outside of the package. – Martijn Pieters Mar 09 '17 at 21:22
  • got it. thanks! I was having the same problem and now its fixed. – Paul Mar 09 '17 at 23:40