Pipenv is a tool that creates and manages a virtual environment for your projects and adds or removes packages from your Pipfile as you install or uninstall them. It also generates the Pipfile.lock, which we use to produce deterministic builds and create a snapshot of our working environment. Pipenv provides us with an easy method to set up a working environment. You no longer need to use pip and virtualenv separately. They work together.
Before we start using Pipenv, we will need to install it. I assume you have installed Python and pip on your machine. So, open the terminal and run
pip install --user pipenv. And pip will install Pipenv within your home directory. You might need to replace pip with pip3 depending on the Python version you have installed on your computer.
Configure PATH Environment Variable
You might want to add the user base’s binary directory to your PATH environmental variable for your convenience. If you don’t, you will receive “command not found” by running the pipenv command directly.
To do that, on Windows, open the Command Prompt and run
py -m site --user-site, and this will return the path to the user site-packages directory.
Replace “site-packages” with “Scripts” in this path to receive a string for adding to the PATH variable. And then run
setx PATH "%PATH%;C:\Users\[YOUR_USER_NAME]\AppData\Roaming\Python\Python38\Scripts"
On macOS or Linux:
On macOS or Linux, open the terminal and run
python -m site --user-base. Add “bin” to this path to receive a string for adding to your ~/.bashrc file.
Edit ~/.bashrc file and add
And finally, run this command to make the changes take effect.
Create a New Virtual Environment
To create a new virtual environment, using a specific Python version, use the –python flag. Navigate to the directory containing your Python project and run
pipenv --python 3.8.5, which is the installed version of Python on my computer.
Alternatively, you can run
pipenv --three to initialise a Python 3 virtual environment or
pipenv --two for Python 2.
By running this, Pipenv will create a new virtual environment for your project with the Python version you specified as the required Python version for this project. And it will tell me where I can find the newly created virtual environment.
[[source]] url = "https://pypi.org/simple" verify_ssl = true name = "pypi" [requires] python_version = "3.8"
Activate the Newly Created Virtual Environment
You might want to verify the Python version installed in your virtual environment. To do so, you first need to activate that virtual environment by running
We will then run
python --version, and this will tell me the installed version of Python. You can also deactivate this shell using the exit command.
Installing Your Project Dependencies
Now you can install the third-party packages you need. To install a Python package for your project, use the install keyword. For example, Here, we are going to install requests version 2.24.
pipenv install requests==2.24
[[source]] url = "https://pypi.org/simple" verify_ssl = true name = "pypi" [packages] requests = "==2.24" [requires] python_version = "3.8"
To install the latest version of a package, not a specific version, you could omit the version from this command.
Note: Install command is fully compatible with pip install syntax, for which you can find the full documentation here.
You can similarly remove this package by running
pipenv uninstall requests.
Installing Your Project Development Dependencies
Let’s say you also want to lint your code, and you want to use pylint for doing this. You don’t need pylint in production, so you can specify that this dependency is only for development using the –dev flag.
pipenv install --dev pylint
And Pipenv will add the pylint under [dev-packages] within Pipfile. This section separates dependencies needed only for development from ones needed for the code to work.
[[source]] url = "https://pypi.org/simple" verify_ssl = true name = "pypi" [packages] requests = "==2.24" [dev-packages] pylint = "*" [requires] python_version = "3.8"
Update Outdated Dependencies
To update a specific package or all the packages in your project’s dependencies, you first need to find out whether there is any new release for the packages in your dependencies. Run
pipenv update --outdated, and Pipenv will return a list of outdated packages.
Now you can update all the packages by running
pipenv update or
pipenv update requests to update the requests package only.
Push Your Project to Production
Let’s say you’re ready to push your project to production. To do that, you need to lock your current working environment to ensure you will have the same production environment. Run
Now you are in the production environment and want to replicate the exact environment you had in development. Let me remove the current virtual environment to simulate this situation by running
I’m then going to install the last successful environment. Run
pipenv install --ignore-pipfile. And this tells Pipenv to ignore the Pipfile for installation and use what is in the Pipfile.lock. And it will create the same environment as you had when you ran