2

I have got a problem with relative import while developing an application that contains two packages I implemented, one for functionality and other for GUI.

Here's the actual application "architecture" layout:

 main.py  
 functionality/  
     |__init__.py  
     |functionality.py  
     |config/  
         |__init__.py  
         |conf.py  
 gui/  
     |__init__.py  
     |gui.py

I import the config module inside the functionality.py file and use it without any problem. But when I import the functionality module to the main.py file and run it, I get the following error:

from config import conf
ImportError: No module named 'config'

I have searched for this problem and read several Python books, but I didn't find any solution.

Thank you in advance!

7
  • Possible duplicate of Importing from a relative path in Python Commented Dec 2, 2015 at 17:47
  • Shouldn't it be from .config import conf ? Commented Dec 2, 2015 at 17:48
  • .. if my information serves me right Commented Dec 2, 2015 at 17:50
  • @wim It throws this error: SystemError: Parent module '' not loaded, cannot perform relative import Commented Dec 2, 2015 at 17:51
  • @RNar The double dot before import statement takes you to one upper level in the hierarchy Commented Dec 2, 2015 at 17:52

2 Answers 2

0

As I understand you, you tried to import functionality.functionality, which imports config, and that fails.

That's because when you run main.py, in your module search path there is no modules containing anything like 'config'. Check that out: add

import sys
print (sys.path)

right before you imported functionality. There is root in your search path (where main.py is located) and some default site directories.

Refer to this section: https://docs.python.org/3/tutorial/modules.html#intra-package-references

As it written there, you have to use relative imports (with leading dots) or write full path (functionality.config in your case)

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

3 Comments

You are right, I should use relative imports and that's what works, the full path solution also works. The second solution throws another error: ImportError: No module named 'functionality.config'; 'functionality' is not a package
Yes, you can't directly use a module with a local import.
Also, I should specify that those errors occur when I run functionality.py alone; unlike main.py which runs fine even with importing functionality package.
-2

I found the solution!

I added a leading dot the faulty import statement in functionality.py file:

from .config import conf

It works just fine when running the main.py file!
But let's say that I reversed the problem because now the import doesn't work when I run the functionality.py alone the interpreter throws the same exception

Traceback (most recent call last):
File "/home/youssef/workspace/application/functionality/functionality.py", line 14, in <module>
from .config import conf
SystemError: Parent module '' not loaded, cannot perform relative import

I must also mention that I emptied the __init__.py file that exists in the config package so it doesn't contain any import statement.

I thank you all for your efforts and if I find other solutions, I will edit this answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.