Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set PYTHONPATH, when virtualenv is activated

This moment, I am using pyenv + venv to manage Python versions and specific libraries. My project layout looks like this:

  1. dir1
    • subdir11
    • subdir12
    • subdir13
  2. dir2
    • subdir21
    • subdir22
    • subdir23

My goal is, once venv is activated dir1 and dir2 to be inside sys.path. Basically, I solve this task but I really do not like solution. I added custom PYTHONPATH inside venv under bin/activate file and everything works fine. This is not so flexible from deployment point of view. Does venv provide some hooks for this ? For example: set customer environment variables once venv is activated or run some function or something like this. I do not like idea, to modify bin/activate file on every machine.
P.S I know about packaging stuff and setuptools, but for this project I can not use it.

Any ideas ?

Thanks !

like image 393
pyargskwargs Avatar asked Oct 23 '25 04:10

pyargskwargs


1 Answers

Another possibility (which is usually what I use and avoids going into you venv source code) :

Create an activate.sh script where you export path to your modules AND activate venv ie :

source venv/bin/activate
export PYTHONPATH=$PYTHONPATH:$(pwd)

then you can just do source activate.sh instead of source venv/bin/activate and you will have both actions at once.

like image 117
Luis Blanche Avatar answered Oct 25 '25 18:10

Luis Blanche