Autoenv runs even in a subfolder

I use autoenv to automatically enable virtualenv . The top folder of the Python project has an .env file with the following contents

 source venv/bin/activate 

This command is executed whenever cd refers to any subfolder of the project. Then throws

 -bash: venv/bin/activate: No such file or directory 

Failed because it is trying to execute activate on a subfolder. Why is it running even in a subfolder? How to solve the problem?

+8
python virtualenv
source share
2 answers

There was this question today. The current answer does not take into account the fact that the environment is activated every time you cd to a subfolder or back to the root folder. Solved it with a .env script:

 venv=venv currentvenv="" if [[ $VIRTUAL_ENV != "" ]] then # Strip out the path and just leave the env name currentvenv="${VIRTUAL_ENV##*/}" fi if [[ "$currentvenv" != "$venv" ]] then echo "Switching to environment: $venv" workon $venv #else # echo "Already on environment $venv" fi 

Replace venv name of your environment. You can uncomment the else block to make sure that it does not try to activate the environment each time, given that the desired environment is already activated.

Note. If you are not using virtualenvwrapper , you must replace the workon command workon any command that you use to activate your virtual environment. I recommend using virtualenvwrapper though.

+6
source share

In your root workspace a .env containing:

 test (command -v deactivate) && deactivate 

and in each of your respective project folders:

 workon venv_of_project 

As this person points out, this means that the cd around in the project turns the workspace on and off, but at least it's simple and very clear what is happening.

0
source share

All Articles