Automatically activate venv for each project.

Okay, so for almost all my life I was a true fan of Conda and managing environments using that.
But I could never wrap my head around having an entirely different installation of python when I wanted to deal with the same python version but just a different set of packages!

Enter, autoenv.

What this package does is, as soon as you navigate into your folder via terminal, it activates the environment for you!
You wanna export env variables, activate this env and more? You can set autoenv to activate the moment you cd into your project folder.

Disclaimer: This does not work with conda installed python for some reason. So I decided to scrape my whole installation of conda python entirely.

For someone who needs specific packages and doesn't want to clutter up thing while being able to manage storage constraints of the. 128 gb storage on my Mac, I couldn't be any more happier. Ever since I used autoenv, things have just been a breeze.

Okay enough with the "how awesome it is", lets get on with how to actually do this shit.

Backing up current environments

First off, take a backup of all your environments that you have created.
I did this manually since I didn't have many conda envs.

Activate each env and run the following command

$ pip freeze > envName.txt

This will give you a backup to migrate to autoenv environments.

PSA. This guide is not for you if you absolutely need different python versions to run different packages. It might be possible using the same pyvenv but at the time of this writing, I am not aware of that.

Run that command for all the environments you have and uninstall your conda installation.

Fresh installation of python.

Download and do a clean installation of python from

I chose, the latest available at the time. 3.6.x

Setting up pyvenv for the project.

With the installation done, we can now proceed to moving on to the actual autoenv part. Oh wait, after we actually make a venv for our project. To do that,

Navigate to your project folder and run the following commands.

$ pyvenv env
$ source env/bin/activate
$ pip install -r requirements.txt
$ deactivate

What we did above was
1. create an venv called "env"
2. Activate the venv
3. Installing packages into venv
4. deactivate activated env.

Before deactivating you might want to make sure your app/script works just as expected with the env activated cause if not, you would need to fix that before the autoenv comes in.

Once all of that is done, proceed to setting up autoenv.

Setting up autoenv

To do this, make sure your environment is deactivated and you are on the default environment. Now install autoenv inside your default python installation.

$ pip install autoenv

Update your .bashrc or .bash_profile file depending on your os (Generally bashrc for linux, bash_profile for Mac.) by running the following command

$ echo "source `which`" >> ~/.bashrc
$ source ~/.bashrc

Ensure that on running the second command, no errors are produced.  If there are errors run

$ which

If it doesn't return anything, you probably haven't set the right path to python bin. If thats the case, Export path to python and then run the "echo" command from above.

Once the second command shows no errors, you're good to go.
After one last step

$ touch .env

Edit this .env file and add the following line into it.
source env/bin/activate
Add more lines if you would like to set environment variables as well as soon as you navigate into your project. All terminal commands can be entered in this .env file which gets run as soon as you enter your environment.

How autoenv works is, if a .env file is detected, it runs all the lines inside that file as soon as a user changes directory to the project folder.

Pretty neat eh?

Well, it saved me the trouble of having to manually activate environments and setting env variables just for the project. Adding it to .bashrc would mean it causes trouble for other projects and hence, autoenv.

I hope this helps someone, cause I can't emphasize it enough on how easy things have been ever since  autoenv came into my life :)

That pretty much wraps it up for this post.


Popular Posts