SystemError: parent module '' not loaded, cannot perform relative import

I have the following directory:

myProgram └── app β”œβ”€β”€ __init__.py β”œβ”€β”€ main.py └── mymodule.py 

mymodule.py:

 class myclass(object): def __init__(self): pass def myfunc(self): print("Hello!") 

main.py:

 from .mymodule import myclass print("Test") testclass = myclass() testclass.myfunc() 

But when I compile it, I get this error:

 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 

It works:

 from mymodule import myclass 

But I don’t get automatic completion when I type this message and the message appears: "unresolved link: mymodule" and "unresolved link: myclass" And in my other project I'm working on, I get the error message: "ImportError: there is no module with named "mymodule"

What can I do?

+44
python
Nov 20 '15 at 23:23
source share
4 answers

I had the same problem and decided to use absolute import instead of relative one.

for example, in your case, you will write something like this:

 from app.mymodule import myclass 

You can see in the documentation .

Note that relative imports are based on the name of the current module. Since the name of the main module is always " __main__ ", modules intended to be used as the main module of a Python application should always use absolute import.

+26
Aug 23 '16 at 3:14
source share

if you just run main.py under the app , just import e.g.

 from mymodule import myclass 

if you want to call main.py in another folder, use:

 from .mymodule import myclass 



eg:

 β”œβ”€β”€ app β”‚  β”œβ”€β”€ __init__.py β”‚  β”œβ”€β”€ main.py β”‚  β”œβ”€β”€ mymodule.py β”œβ”€β”€ __init__.py └── run.py 

main.py

 from .mymodule import myclass 

run.py

 from app import main print(main.myclass) 

So, I think the main question about you is the call to app.main .

+3
Sep 14 '16 at 6:51
source share

I usually use this workaround:

 try: from .mymodule import myclass except Exception: #ImportError from mymodule import myclass 

This means that your IDE must pick up the correct code location, and the python interpreter will be able to run your code.

+2
Jul 05 '16 at 8:49
source share

If you go up one level when you run the script on the bash shell command line, the problem will be solved. To do this, use the cd .. to change the working directory in which your script will run. The result should look like this:

 [username@localhost myProgram]$ 

instead of this:

 [username@localhost app]$ 

Once you are there, instead of running the script in the following format:

 python3 mymodule.py 

Change it like this:

 python3 app/mymodule.py 

This process can be repeated one level up again depending on the structure of your tree diagram. Also specify a compilation command line that gives you the specified error message.

0
Sep 14 '16 at 4:45
source share



All Articles