Pipenv and all in one tool
This post is part of the Python's Reptile Env series
- Pipenv and all in one tool
- Why Do I Disinstall Pipenv and Use Only Poetry?
- Why Do I Disinstall Poetry and Use Only Uv
⚠️ This post is obsolete. ⚠️
I suggest to go to the newest post from the Python’s Reptile Env series
I am a big fan of podcasts. I enjoy listening to them while I’m commuting or at the gym. One of my favorites is by Kenneth Reitz , the author of several Python modules, including Requests, which every Pythonista knows and uses. His latest project that caught my interest is PipEnv, the “sacred marriage of pipfile, pip, and virtualenv.”
Let’s start with the basics: to create a Python project, you usually go through 3 mandatory phases:
- You create a virtualenv to work in, so that installed packages don’t conflict with those in the system, providing a clean environment.
- You select the modules needed for the project and install them in the virtualenv using pip.
- You generate a requirements.txt file from the virtualenv that indicates what is installed.
This requires you to constantly keep the virtualenv updated and clean, keep the requirements.txt file synchronized, and pin the minimum versions (or the exact version) of the modules in the requirements.txt.
PipEnv does all of this for you with a few simple commands.
For example?
Let’s say I need to create a Python script that fetches RSS feeds and saves them locally.
- I create a folder for the project to keep everything I need organized:
mkdir python_project- I choose which Python version to use, whether the current one or the legacy version:
pipenv --three # current Python 3.x version
# alternatively
pipenv --two # legacy 2.7.x- I start writing code and installing the necessary packages for the project:
pipenv install requests
pipenv install flaskThese commands, along with the initial setup, allow me to create a Pipfile that describes the modules I’ve installed. The best thing about this Pipfile is that it allows for multiple “environments” within the same file. This means I can have “testing”, “travis”, “dev”, and “prod” all described in a single file and managed automatically.
Now, suppose I realize I no longer need flask because I’ve rewritten everything to make the code more readable without it… What do I do?
pipenv uninstall flaskThis command removes flask from the Pipfile, keeping it “clean” and always up to date.
Once the Pipfile is defined, you need to create the Pipfile.lock—an automatically generated version based on the “current installation” to perfectly reproduce the environment.
pipenv lockThis pins the installed modules with their version, hash, and other data for both the specified packages and their dependencies, providing all the information needed to reproduce that exact environment.
Is that all?
No, the system initially creates a virtualenv for the project and populates it according to the terminal commands. This virtual environment is accessible via the command:
pipenv shellThis opens a shell within the project’s virtualenv and allows you to execute commands inside that specific environment.
Furthermore, PipEnv allows you to convert existing requirements.txt files into a Pipfile if they are not already present, and to update all packages using:
pipenv updateConclusion
Personally, I hope this system—or at least the Pipfile—becomes the standard for Python application development and replaces requirements.txt files, which I find particularly impractical and too sparse, even if they do exactly what they were designed for.
This post is part of the Python's Reptile Env series
- Pipenv and all in one tool
- Why Do I Disinstall Pipenv and Use Only Poetry?
- Why Do I Disinstall Poetry and Use Only Uv
Reference this post
Please reference this post with a link to this page. I prefer to be called Fundor333 (he/him) or Fundor333' Blog.
Comments
To reply to this post, you can send a Webmention or you can toot me at [email protected]