The problem
I have been using Python for a while, from small pet projects to professional sass services. I like writing Python but one thing that bugs me the most is virtual environment. I get it you keep your packages separate between your projects so that updating one thing here doesn’t break something else somewhere but initializing the environment every time i switch between projects is the worst hassle. It’s easy to forget to do so especially when you are trying to do an urgent fix that the client is thinking for.
The bad solution
Looking for a solution to do this I wrote a small bash function to overwrite
the cd
function which would change the directory and initialize a .profile
file in the directory if present. But then I thought if this is so easy to do,
then surely someone would’ve done it and would likely have added a ton more
features into the same. That’s how I found direnv
.
The perfect solution
direnv
allows you to have a .envrc
script in your folder that gets run to
create a shell environment as per your requirements.
While on surface it sounds simple, the amount of overheads the devs have built
into it is amazing. You can use direnv
specific commands to achieve complex
tasks.
Eg. Following is the requirement:
- I want to initialize an environment of python3.8.6 in my directory.
- I have a work folder on my drive that I keep all my work projects in,
lets say
/home/username/work
. I have few environment variables that are common to all projects and I also have project specific ones.
Now with the script I wrote I would have to go through the hassle of setting up
a new python environment for every new project. Hardcode its path in my
.envrc
and then I’ll have to copy paste the common environment variables and
so on.
My setup
With direnv it becomes as simple as
|
|
where source_up
would automatically source my parent directory which would
contain common env vars, layout pyenv
will create me a virtualenv
with the
given python version and export will allow me to set project specific env vars.
Installation instructions for direnv
can be found here
Theres a lot more to do with direnv
which you can read about here
Read more about direnv at official webpage