Reputation: 17679
Consider the following example package:
example/
├── bar.py
├── foo.py
└── __init__.py
foo.py
contains just one line of code: from . import bar
.
If I execute python foo.py
from inside the example
package root, I get:
SystemError: Parent module '' not loaded, cannot perform relative import
What am I doing wrong?
Upvotes: 3
Views: 58
Reputation: 224857
When you’re running python foo.py
, foo.py
is not part of the example
module. Create __main__.py
to run the relevant part of foo.py
(it shouldn’t run any code at the top level, generally), change to the parent directory, and try python -m example
.
For example, foo.py
:
def hello():
print('Hello, world!')
__main__.py
:
from . import foo
foo.hello()
Upvotes: 2