1

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.

ThinkingStiff
65.4k31 gold badges148 silver badges241 bronze badges
asked Jul 3, 2011 at 10:59

1 Answer 1

2

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
answered Jul 3, 2011 at 13:07
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.