I'm writing a package that is a small application. It is distributed with portable Python, so code has no access to Windows settings.
The program has this structure:
mypr
__init__.py
gui
__init.__.py
prlib
__init__.py
I am trying to do this from mypr.gui import Gui in prlib but, of course, mypr is not defined. I could add it to win path, edit pth file or append sys.path with the current directory, but: this will be a portable application and directory will change.
So, how can I be sure that Python will always have mypr in the path, both during development and deployment? Without changing *.pth or editing Windows PATH. Is there some reasonable hack during runtime to link mypr?
Or, is it possible to import a package "relatively"? I tried from .gui import Gui, but no luck.
1 Answer 1
If you just want to be able to access the 'gui' module in the directory above 'prlib' you can achieve that by:
import os
import sys
sys.path.append(os.path.join(os.dirname(__file__), '../'))
Then you should be able to import 'gui' via:
from gui import Gui