OeufsDePie / FAQt

FAQt (Frequently Asked Questions about Qt)
0 stars 0 forks source link

Dealing with Python imports (packages, modules, classes, ...) #2

Open mpizenberg opened 9 years ago

mpizenberg commented 9 years ago

Since I decided to make 4 files with each one class instead of 1 file with all classes inside, I had trouble with the imports. I finally got the answers and I can give you some tips here.

First I got the error :

ImportError: cannot import name bidule

This was because I had a file name init.py instead of __init__.py in my package folder (@KtorZ you should update all your Python packages).

Second, when importing from outside of the package, I got the error :

ImportError: No module named truc

This time it was because inside some file, I imported truc with the command import truc but the python path did not included the directory path in which truc was since I originally imported the files from outside the package. In order to correct that error you should add the following lines in your __init__.py file :

import sys
import os

current_folder = os.path.dirname(os.path.abspath(__file__))
sys.path.append(current_folder)

One last remark : circular imports are not allowed in Python.