1

So I have being working on an open source project for a couple months and was just recently notified that end users should not have to set a pythonpath to use the software. I had a bunch of classes that import from classes in outer directories within my project.

Now the two options I see are:

  1. to get rid of my beautiful package structure and place 25 something files in the same directory. So all imports are done within the same package.

  2. Figure out a way to automatically update their PYTHONPATH when they download the software which will always be an SVN checkout.

I could easily be missing something obvious but I already tried using os to change directories then import then change back and that is not going to work.

2
  • why not package project using standard setuptools? Commented Sep 22, 2015 at 21:04
  • My recent instructions were that users should be able to just download the source files and run a main.py. So I can't force them to install setuptools or make a project for that matter. Commented Sep 22, 2015 at 21:50

1 Answer 1

1

You want to use sys.path to manipulate the path at runtime.

import sys
sys.path.insert(0, "path/to/directory")

or

import sys
sys.path.append("path/to/directory")

The first version will prepend to PYTHONPATH, which will make the interpreter search there first. The second version will append it, making it searched last. You want to make sure that you put this code before any imports that use the modified path.

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

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.