Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "Attempted relative import in non-package" even with __init__.py

I'm trying to follow PEP 328, with the following directory structure:

pkg/   __init__.py   components/     core.py     __init__.py   tests/     core_test.py     __init__.py 

In core_test.py I have the following import statement

from ..components.core import GameLoopEvents 

However, when I run, I get the following error:

tests$ python core_test.py  Traceback (most recent call last):   File "core_test.py", line 3, in <module>     from ..components.core import GameLoopEvents ValueError: Attempted relative import in non-package 

Searching around I found "relative path not working even with __init__.py" and "Import a module from a relative path" but they didn't help.

Is there anything I'm missing here?

like image 828
skytreader Avatar asked Jul 18 '12 07:07

skytreader


People also ask

What is relative import in Python?

A relative import specifies the resource to be imported relative to the current location—that is, the location where the import statement is. There are two types of relative imports: implicit and explicit. Implicit relative imports have been deprecated in Python 3, so I won't be covering them here.

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__ .


2 Answers

To elaborate on Ignacio Vazquez-Abrams's answer:

The Python import mechanism works relative to the __name__ of the current file. When you execute a file directly, it doesn't have its usual name, but has "__main__" as its name instead. So relative imports don't work.

You can, as Igancio suggested, execute it using the -m option. If you have a part of your package that is meant to be run as a script, you can also use the __package__ attribute to tell that file what name it's supposed to have in the package hierarchy.

See http://www.python.org/dev/peps/pep-0366/ for details.

like image 149
BrenBarn Avatar answered Sep 29 '22 13:09

BrenBarn


Yes. You're not using it as a package.

python -m pkg.tests.core_test 
like image 29
Ignacio Vazquez-Abrams Avatar answered Sep 29 '22 12:09

Ignacio Vazquez-Abrams