Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PYTHONPATH environment variable...how do I make every subdirectory afterwards?

I currently do this:

PYTHONPATH=/home/$USER:/home/$USER/respository:/home/$USER/repository/python-stuff

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

PYTHONPATH = /home/$USER/....and-all-subdirectories
like image 500
TIMEX Avatar asked Oct 15 '25 23:10

TIMEX


1 Answers

It's generally a bad idea to have on sys.path two paths one of which is the parent of the other -- assuming the sub-directory one contains an __init__.py file to mark it as a package, of course. If only the parent directory is in the path (and $PYTHONPATH is part of what sys.path is initialized with), modules in the subdirectory can be imported from the package, i.e., through just one filesystem path, avoiding the risk of a single module being imported under many distinct guises.

So why don't you just put __init__.py files in all subdirectories that need it, and use package imports?

While I think your request is a bad idea, it's certainly doable -- the Unix find command can easily list all subdirectories of a directory, one per line (find . -type d), and you can easily glue the lines together e.g. by piping find's output to tr '\n' :.

like image 54
Alex Martelli Avatar answered Oct 17 '25 13:10

Alex Martelli