user2327814
user2327814

Reputation: 543

Appending to PYTHONPATH from SVN

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.

Upvotes: 1

Views: 109

Answers (1)

nathanielobrown
nathanielobrown

Reputation: 1012

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.

Upvotes: 1

Related Questions