. Advertisement .
..3..
. Advertisement .
..4..
I’m building a new program, but when I run it, an error pops up. The error displayed is as follows:
Traceback (most recent call last):
File "D:/Users/Myname/Documents/PycharmProjects/myProgram/app/main.py", line 1, in <module>
from .mymodule import myclass
SystemError: Parent module '' not loaded, cannot perform relative import
I have tried several workarounds, but they still do not get the desired results. If you have come across this situation and have a solution for the “systemerror parent module not loaded cannot perform relative import” problem, pls let me know. Here is what I do:
myProgram
└── app
├── __init__.py
├── main.py
└── mymodule.py
class myclass(object):
def __init__(self):
pass
def myfunc(self):
print("Hello!")
from .mymodule import myclass
print("Test")
testclass = myclass()
testclass.myfunc()
from mymodule import myclass
Thanks!
The cause: The Systemerror parent module is not loaded, so when python couldn’t import a particular module and its fuction or class from another module, it also cannot use relative import.
Solution: This suggestion will help you resolve your problem:
It means that your IDE will choose the right code site and your code will be managed to run through the python interpreter.
The same problem was mine, and I solved it using an absolute import rather than a relative.
For example, in your case you might write this:
The documentation explains this.
Edit: To make the interpreter recognize the
__init__.py
file as a package, you should add it to yourapp
directory. It’s fine if the file doesn’t exist.