I've got a python3 script, script.py, and in it I want to instantiate a class Foobar which is defined in clazz.py. However, when I try to import, I get:
$ python3 script.py
...
SystemError: Parent module '' not loaded, cannot perform relative import
Here is my file structure:
python_import/
├── __init__.py
├── clazz.py
└── script.py
clazz.py:
class Foobar():
def __init__(self):
print("initialized a foobar")
script.py:
from .clazz import Foobar
foobar = Foobar()
It runs fine if I get rid of the .
in the import
; however, if I do this, my IDE (Intellij IDEA) red-underlines the import and won't autocomplete anything. I believe including the .
is correct in python3, and Intellij seems to like it, so why won't my program run unless I remove it?
I have read http://www.diveintopython3.net/porting-code-to-python-3-with-2to3.html#import, http://python.readthedocs.org/en/latest/reference/import.html, How to import the class within the same directory or sub directory?, Relative imports in Python 3 and Relative import in Python 3 not working.
I suspect it may have something to do with the virtualenv but a) I don't understand why the working directory wouldn't be part of the PYTHONPATH and b) I'm not really sure how to change it in virtualenv - Intellij set it up for me.