An error is a problem in python which occurs while compiling a program code. As a result, the program behaves unexpectedly. When python fails to understand a given code, it leads to an error. There are several types of errors in python, such as SyntaxError, RuntimeError, IndentationError, etc. In this article, we shall be looking into one such error – Systemerror parent module not loaded cannot perform relative import.
What is a system error?
A system error is an error in python raised when the error is related to the system, i.e., an internal error. The associated value is a string value that specifies where the problem occurred. Here, the parent module not loaded cannot perform relative import is also a type of System Error. System errors can be raised when the file or module imported is either missing or corrupted. Because of this, the instruction will not be recognized by the computer program.
Reason behind Systemerror parent module not loaded cannot perform relative import
The Systemerror parent module not loaded cannot perform relative import is raised when python fails either to import a particular module from another module or import a particular module’s function or class from another module.
Let us consider a directory named app. The ‘app’ directory contains sub-directory mydir. The mydir contains two files: module1.py and main.py.
Module1.py has the following piece of code:
class a(object): def __init__(): #some code def func_a(): #some code
In main.py, we will try to import the class ‘a’ from ‘module1.py’. We will do that to create an object of class a from ‘main.py’.
To do that, we shall apply the following code in ‘main.py’:
from .module1 import a obj = a() a.func_a()
But, the above piece of code may cause the following error:
SystemError: Parent module ” not loaded, cannot perform relative import
Now, we shall look into ways of how to remove the system error.
Also, Read | [Solved] IOError errno 2 no such file or directory
Solving the SystemError with absolute imports
To solve the above system error, we can simply do an absolute import rather than a relative import.
‘from .module1 import a’ is actually a relative import. Relative imports are lead by dots. This way, we know about the parent package and the current package associated with the relative import.
By using relative import, the location of the package or the module which has to be imported is mentioned with respect to the current location. The location is specified relative to the location where the import statement is present.
But instead, we can make use of absolute import. With absolute import, we can make use of absolute path while importing a module or a package.
We make use of the complete path from the project’s root folder till the module’s location. Even if the location of the current directory where the import statement is changes, with absolute imports the import statement will still remain valid.
Because the main module’s name is always “__main__,” modules intended to be used as the application’s main module must always use absolute imports.
We shall use the following code in our case to use absolute imports:
from app.module1 import a
Now, we can use the object of class ‘a’ and call the class’s functions.
Solving the SystemError with if else
Using an if else statement can avoid the above error. In the above example, we can have separate import statements for two different scenarios. One, if the ‘main.py’ is present in the same directory as the ‘app’ and another scenario if the ‘main.py’ is present in a separate folder.
if __name__ == '__main__': from module1 import a else: from .module1 import a
Here, if the module is present in the same directory, then you can execute the simple import statement. The else part shall be executed when the module is outside the directory. In this way, the code will work irrespective of the fact that from which directory is it being called.
Solving the SystemError with exception handling
We can also use exceptional handling instead of the above if else case to solve the problem. We can use a try-except block.
It will try to execute the ‘from .module1 import a’ statement first. If the importing fails, then it will throw an exception which the except block shall catch. In the except block, we shall execute ‘from module1 import a’.
try: from .module1 import a except Exception: from module1 import a
Also, Read | [Solved] OSError errno22 invalid argument
SystemError parent module not loaded cannot perform relative import FAQ
Here are a few questions we hear often when solving systemerror parent module not loaded cannot perform relative import.
If you provide mypy a directory, it type-checks the files in it, presuming that the directory is not a package. To solve the error, you need to add an empty __init__.pyi to the directory.
We use sys.path.append() to manually append the parent path to the module search path. To add path to sys.path.append(), we can use the try except block.
sys.path.append(sys.path + '/..')
This way, we set sys.path as the current file’s path.
This sums up System Error Parent Module Not Loaded Cannot Perform Relative Import. If you have any doubts, let us know in the comments below.
Until next time, Keep Learning!