Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I currently do this:


How can I make it so that the PYTHONPATH can include everything subdirectory?

PYTHONPATH = /home/$USER/....and-all-subdirectories
share|improve this question
up vote 11 down vote accepted

That's not how PYTHONPATH works; PYTHONPATH treats its search path differently from shell PATH. Let's say I do this:

$ mkdir /home/jsmith/python
$ cd /home/jsmith/python
$ touch

This will work, in Python (sys.path will include the current directory):

$ cd /
$ PYTHONPATH=/home/jsmith/python python2.6

Python 2.6.1 (r261:67515, Jul  7 2009, 23:51:51)
>>> import a, b   # Works
>>> quit()

However, subdirectories are treated as packages when is present in the directory, and are ignored by PYTHONPATH otherwise:

$ mkdir /home/jsmith/python/pkg
$ cd /home/jsmith/python/pkg
$ touch
$ cd /
$ PYTHONPATH=/home/jsmith/python python2.6

Python 2.6.1 (r261:67515, Jul  7 2009, 23:51:51)
>>> import a, b   # Works
>>> import c
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named c

To get at something in that subdirectory, this would work:

>>> from pkg import c   # Works
>>> import pkg.c        # Works

To roll a solution where every subdirectory in your PYTHONPATH is added, you need to explicitly add every folder to PYTHONPATH or sys.path programmatically. This behavior is intentional, and behaves nothing like shell PATH. Given the interpreter's support for packages in this regard, surely there's a better way to accomplish what you're after?

share|improve this answer
Man, I wish each site would check your other accounts before denying you posting privileges. It sucks starting over with rep, especially with things like the one-URL limit...(I had more references for you, OP) – Jed Smith Nov 1 '09 at 1:08
Thanks very much Jed. Appreciate it – Alex Nov 1 '09 at 1:35
@Alex: You're welcome. :) – Jed Smith Nov 1 '09 at 1:40

That's not how environment PATH variables work - you give it the top-level directory and it's up to the application to recurse the directory tree if it needs to.

share|improve this answer
So, if I have a python file under /home/$USER/ Can I import this? – Alex Oct 31 '09 at 23:42
Sure, why couldn't you? – EEAA Oct 31 '09 at 23:45

It's possible to add subdirectories of a directory to your PYTHONPATH variable using the shell, of course. I currently use something similar to the following in my .bashrc:

export PYTHONPATH="$(find $HOME/ -maxdepth 2 -type d | sed '/\/\./d' | tr '\n' ':' | sed 's/:$//')"

This would include all subdirectories of your user folder to a depth of 2 in the tree. The find command locates the directories ('-type d'), and the following sed and tr commands format the output in the usual way of PATH variables.

Leaving off '-maxdepth 2' would include all subdirectories of your home folder, which is probably quite a lot to search. Perhaps this should only be done in your $HOME/repository/python-stuff directory.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.