Use pipenv to create a bug-free virtual environment
Tags: python, django, pip
Published: 2022-08-26 10:59:00

Dependency bugs can be quite annoying. You have to keep upgrading and downgrading dependency versions in case of a conflict. This takes a lot of time away from your development process. Why not use **pipenv** to avoid this?
 
**Pipenv** is a comprehensive programming tool that is now replacing pip and virtualenv. It's used to automatically set up and manage python virtual environments.
 
In this article, you will learn what pipenv is, and how to set up and use it to manage your python environment.

Prerequisites:
- You have python3 installed (preferably python3.7- python 3.9)
- You have pip3 installed
- You have prior knowledge of setting up  applications using python django framework
- You must be familiar with python environment set-up using pip

## What is pipenv
 
**Pipenv** is a dependency manager for python project environments. **Pipenv** enables easy and quick setup of virtual environments. It efficiently handles the addition and removal of dependency packages. **Pipenv** combines the power of **pip**,**pipfile** and **virtualenv** to reduce dependency clash by a big percentage.
 
Some of the advantages of using **pipenv** are:
- Installs packages without versions, therefore reducing conflict when handling different versions of dependencies.
- Combines pip and virtual environment, so you no longer use them separately.
- Eliminates requirements.txt -which can be hard to manage. Instead, **pipenv** creates Pipfile to track dependencies and Pipfile.lock to produce deterministic builds.

- Enables quick and easy setup of virtual environments.
Now that you have seen the benefits of using **pipenv** to set up our virtual environment, how about we do it practically?
 
Follow the following steps to install and create your virtual environment using **pipenv**:
## 1. Install pipenv
Install **pipenv** using any of these three  commands:
```
$ sudo apt install pipenv
$ pip install pipenv
$ pip install --user pipenv
```
A successful installation will show the **pipenv** version on the command line as shown below:


![succesfull pipenv installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l3ni4k543v3zzx5e0ksd.png)
Congrats! you have successfully installed pipenv. Next, you will activate the virtual environment.
## 2. Activate the virtual environment
Since **pipenv** manages dependencies inside each project, you need to activate your environment inside your project folder. For example:
 
```
$ cd pipenv-test
```
 
Once in the folder, you can activate your virtual environment using the following command:
 
```
$ pipenv install requests
```
 

**Pipenv install requests** enables **pipenv** to fetch components of the request library such as the Pipfile and the Pipfile. lock that manages the virtual environment.
 
A successful installation of requests is  as illustrated below:


![pipenv installs requests](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/haf72343b38ex8ey5pws.png)

The Pipfile stores all installed dependencies. Open the  Pipfile on your code editor.  It will look similar to the image shown below:
 


![pipfile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/chjud1fnkprc3yasxskl.png)

Next, you enter the **pipenv** shell where you run the activated virtual environment.
 
## 3. Activate pipenv shell
Run **pipenv** shell to enter the **pipenv** virtual environment. This will start up the virtual environment under which our application will run. It will appear as illustrated below:
 
```
$ pipenv shell
```
 
Do you notice the changes on your command line after running **pipenv** shell? A virtual environment created in the name of your project appears in brackets.


![pipenv creates virtual environment](https://cdn.hashnode.com/res/hashnode/image/upload/v1661440855680/qefaIS8e5.png align="left")
 
This indicates your environment is live. Now you can go ahead and install your dependencies. Remember for every dependency you install, it appears in the Pipefile.
 
 
## Conclusion
 
Now that you have seen how to install and set up **pipenv** to manage your virtual environments, go ahead and realize its benefits. This will reduce the amount of time spent dealing with dependency clashes. For more information on **pipenv** and how to handle error messages please visit [pipenv.pypa](https://pipenv.pypa.io/en/latest/#install-pipenv-today).
 
Happy coding!