Pipenv vs Virtualenv environment in Python

Pipenv vs Virtualenv environment in Python

Python has different ways of creating a virtual environment. In this post, I will discuss how to use Pipenv and Virtualenv. The main reason behind using the virtual environment in our project is, to make our project protected from any kind of future updates that may lead to some disruption. Also during deployment, we could use the same versions of packages we used while building the app to avoid any kind of update issues.

In simple words, a virtual environment is used to isolate application-specific dependencies from a shared Python installation.

Virtualenv

Virtualenv is one of the ways of creating a virtual environment in Python. Those who work on Django, Python's web framework, use it often for there projects. It is still a default way of creating a virtual environment used by most of the developers. You have to have pip installed in your system for this. Here is how you create a new virtual environment. Run

pip install virtualenv

once Virtualenv is installed, you can use it as a command to create a virtual environment followed by the name of your virtual environment.

virtualenv myenv

Myenv is the name of the virtual environment. This will create a folder myvenv in the directory.

Now to activate the virtual environment, we have to get into myenv folder, and then select activate from Scripts.

myvenv/Scripts/activate

This would activate the myvenv virtual environment in the directory. To deactivate, simply type deactivate.

To install various packages. Run pip install package.

pip install django

Inside our virtual environment, we need to create a requirements.txt file, to keep a record of all the installed packages in the environment.

pip freeze > requirements.txt

In requirements.txt, you can see all installed packages with their versions. To install packages from the requirements.txt. Run

pip install -r requirements.txt

Here in Virtualenv, we have three steps to follow.

First, create a virtual environment, then install packages and then, maintaining requirements.txt.

Virtualenv is shown as a recommended tool on the Python.org

PIPENV

pipenv combines package and environment management support into a single tool. To install pipenv, install pip and Run

pip install pipenv

To activate the virtual environment. Run.

pipenv shell

this launches a sub-shell and creates a Pipfile. To install new packages, Run.

pipenv install package

and pipenv will automatically add the packages to the Pipfile. So if I run pipenv install camelcase, Under packages Pipfile will have camelcase in it.

To uninstall a package

pipenv uninstall package

Now you will also see a new file: Pipfile.lock. This is to specify, which specific version of these packages from the Pipfile, should be used.

To list the packages in your environment, Run.

pipenv lock -r

And to exit the environment, type exit.

Pipenv allows developers to create separate sections for our environments such as production and test-development. For the production environment, Pipfile has [dev-packages] and all the packages installed for production are found under it. To install the packages under dev-packages:

pipenv install package –dev

To see the securities issue. Run

pipenv check

To see project dependencies with further dependencies. Run

pipenv graph

So these were the two most used methods to create a virtual environment in Python. According to my experience, both of these tools solve the problems and help us create a virtual environment easily. You can pick any. I use virtualenv.