4

I know there are multiple solutions online, but some are for windows, some are environmental variable, etc..

What is the best way?

2 Answers 2

11

Find your site-packages directory and create a new file called myproj.pth

Inside that file, put each directory you want to add, one per line like so:

/home/myuser/svn-repos/myproject
/home/myuser/svn-repos/SomeOtherProject

Python loads *.pth every time it runs, and imports all paths in each of those files.

In Ubuntu, that directory can often be found at /usr/local/lib/python2.6/dist-packages/

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

5 Comments

What if you install python3.1? then the path(es) won't be imported
That is by design. Python 3k is substantially different than python 2.x, and so you need to re-do this sort of configuration. If you're already using Py3k, put your .pth in the Py3k site-packages directory.
Well, what if you were on 2.5 and then you moved to 2.6? My point is, you'll have to copy this file around, where as if you define PYTHONPATH in ~/.bashrc you won't have such an issue
If you define PYTHONPATH in your ~/.bashrc, you have to copy that file around for each user of your system. Ever have anyone else do work on the same machine? Ever do work as www-admin?
Then put it in /usr/local/lib/site-python. That'll stay constant from install to install. I recommended the specific version site path because it keeps python from barfing when you change versions and your code is incompatible.
1

I personally just define PYTHONPATH in ~/.bashrc, as for what's the "best" approach? I think that's hard to answer (or rather, there's no correct answer). It depends.

3 Comments

You might want to take out the waffling and just say PYTHONPATH so we can vote that up.
Well, the "waffling" is part of my answer. I mean, obviously the dude already knows about PYTHONPATH, so just mentioning it alone doesn't add any value.
Adding in .bashrc does not work for ipython qtconsole when it is not run from a terminal. The other answer works though.

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.