101

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 run it, then 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

This works:

from mymodule import myclass

But I get no auto completion when I type this in and there is a message: "unresolved reference: mymodule" and "unresolved reference: myclass". And in my other project, which I am working on, I get the error: "ImportError: No module named 'mymodule'.

What can I do?

5
  • python.org/dev/peps/pep-0366 Commented Nov 20, 2015 at 23:26
  • 26
    I came across the same problem. Apparently running script inside a package is considered as a bad practice, and you cant use relative import in that case. Since your main.py is inside the package app, using relative import will cause error. Use relative import only in modules and run the scripts outside the package. Commented Mar 20, 2016 at 17:14
  • 6
    Use python -m package.module instead of python package/module.py. Commented Sep 14, 2016 at 6:53
  • This is the answer that solves the problem. Commented Aug 29, 2017 at 8:32
  • Got this in PyCharm when I accidentally ran the current file I was editing instead of my main.py launcher. Commented Dec 2, 2017 at 17:08

4 Answers 4

56

I had the same problem and I solved it by using an absolute import instead of a relative one.

for example in your case, you may 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 for use as the main module of a Python application must always use absolute imports.

Edit: If you encounter this error ImportError: No module named 'app.app'; 'app' is not a package, remember to add the __init__.py file in your app directory so that the interpreter can see it as a package. It is fine if the file is empty.

Sign up to request clarification or add additional context in comments.

8 Comments

If do this I get another error, a ImportError: No module named 'app.app'; 'app' is not a package: gist.github.com/anonymous/ac0b7b3c36b0a60be6273394d1ddbdfb
@fiatjaf please can you share the structure of your code with us ?
Thank you, @Erman. Just did it in a comment on that gist.
Have you declare your init.py file ? Please make to declare your all your packages inside.
For more infornation about init.py file, check this link. docs.python.org/3/tutorial/modules.html#packages
|
19

I usually use this workaround:

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

Which means your IDE should pick up the right code location and the python interpreter will manage to run your code.

5 Comments

except Exception catches any exception, which is not what you want.
But then do I want SystemError or ImportError?
You want ImportError
I just wanted to outline that OP, confusingly, has a SystemError
If you want to catch both, catch both (except SystemError, ImportError); don't catch all errors.
8

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

from mymodule import myclass

if you want to call main.py on other folder, use:

from .mymodule import myclass

for example:

├── 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 of you is how to call app.main.

2 Comments

Yes, that is the question. How do we do it?
I think the first part is the opposite of the second part of your answer.
2

If you go one level up in running the script in the command line of your bash shell, the issue will be resolved. To do this, use cd .. command to change the working directory in which your script will be running. The result should look like this:

[username@localhost myProgram]$

rather than this:

[username@localhost app]$

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

python3 mymodule.py

Change it to this:

python3 app/mymodule.py

This process can be repeated once again one level up depending on the structure of your Tree diagram. Please also include the compilation command line that is giving you that mentioned error message.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.