Why I think pipenv is better than venv! And how to get started with it.
Arth
Posted on June 18, 2020
Yeah, you read that right. I actually think that pipenv is better than venv for there are multiple reasons and a whole lot of thought behind it.
Tl;Dr: Use pipenv for better compatibility.
What do I mean?
The other day I was setting up my project on my new machine so I did the usual :
- Opened my Terminal
- Created an Env directory in the root
- made a venv in it for particular use cases, like a django venv for the new django projects where I don't need to worry about using django2.x and an ML venv where I installed the latest frameworks used for ML including Tf2.x
- went to my root directory, created the projects directory, cloned my web development project, opened it in my vscode and selected my interpreter.
These steps seem usual but what followed was grim, the ghastly error named unresolved-import
and no suggestions when I type :). It's something no one likes to look at. I looked up on StackOverflow and other places on how to fix it after I wasn't able to fix it on my own even after selecting the interpreter multiple times. After putting more thought to it, I realized that the Mac comes with 3.7.4 Python by default and I had just updated it to 3.8.3 but well you might think what's the big deal about it, right?
Nothing. But as for venv
, it's quite a big deal. When I initially created my venv
using the command python3 -m venv django
, the system default was Python 3.7.4 but later, it updated to Python 3.8.3. However, venv
doesn't know that yet. I had to update it using python3 -mvenv --upgrade path/to/venv
but my installed third party packages were gone (they were in path/to/venv/lib/python3.7, but Python 3.8 was looking in path/to/venv/lib/python3.8), so I had to reinstall them.
It doesn't end here though, IF I had gone out of my way and made the venv
using python3.7.4 ...
instead of python3 ...
, I'd have no choice ( or a way more complicated one than it would've been worth it cause this is just sheer unproductivity ) to even upgrade my venv
so that sucks, quite a lot actually.
Well, after finally coming to the realization that I had to move on from venv
that by the way has no backward compatibility to something that offers more and consumes less time involving me in fixing it routinely. That something turned out to be 'pipenv' which is in my opinion one of the best dependency management systems that also dismisses the need for a requirements.txt
file ( which I still prefer to have though ).
Pipenv offers you the best of pip ( or pip3 ) and virtualenv at once.
In essence it is a tool for creating a virtual environment, a utility for installing packages, managing virtual environments (like virtualenvwrapper or pyenv) and has all the commands associated with the libraries used.
And, it has backward compatibility so it works even with Python2.x projects.
Using it
Now you might wonder how do you use it with your projects, it's actually fairly simple.
- Make sure pipenv is installed else, install it with
pip3(or pip) install pipenv
. - Create a project.
- cd into the directory.
- run pipenv shell.
It will create a virtual environment for your project and a pipfile for dependency management.
Now how do you install packages with pipenv?
Just run pipenv install **package_name**
How do you see what packages are installed?
Go to your Pipfile or run pipenv graph
.
In conclusion, use pipenv, be smart! xD
For more of this content, look at my profile I guess.
Posted on June 18, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.