What is pipenv ?

Yash Patel
Jun 11 · 6 min read
  • In this blog we are going to see pipenv, pipenv is new way for us to combine package management with virtual environment, and it is also hignly recommended packaging tool from python.org
  • Basically pipenv is combination of pip and virtualenv

First lets see pip and virtualenv

  1. PIP → pip is a way for us to install addition packages that allows us add a tons functionality that doesn’t exist in python standard library

Example → If you want to do some image manipulation then python might not have the best tools available right out of the box but you can use pip to install pillow library and use that work with images.

  • So i guess a lot of people have probably seen pip before but few of them have used virtual environment now lots of people.

2. VIRTUALENV → Basically a virtual environment is way for us to have a specific environment for each application or project that we are building.

Example → You could have one application that run on python-3.9 and django-3.0 and another another project on python-3.8 and django-2.8 so each of those projects have there own version of python and there own version of packages which is usually how wanted to do it, So some people put off learning virtualenv and just try to run every project using there global installation of python and all the packages installed on one place but it you do it that way then as soon as you update the next version of python or update your packages then it could break over your application that where running just fine, So its best to spilt those up using virtualenv

→ Pipenv tool

  • So first we will install pipenv using pip using command pip install pipenv
  • I have already installed this package so it will show you requirement already satisfied in screenshot
  • So now you can create your project folder any where you want i will create on Desktop
  • Now navigate to that project folder or empty folder with your command line
  • Now if you will install package using pip only then you need to create virtualenv and manually activate it and then install packages. But in pipenv this is all built in together now
  • So instead of creating virtualenv and all we can directly start installing packages with help of pipenv.
  • Lest say that i want to install django package with help of pipenv so for that command is pipenv install django

Now lest take a look on the screenshot that what this command did, i will write this step by step

  1. First pipenv checked for virtualenv in the folder we created and if it will jot find any virtualenv then it will create new one and install the package as you can see in second line
  2. Then has five location for the pipfile, we will see this in a minute.
  3. Then it says that it is using whoch python version and then it goes through and create virtualenv for us.
  4. After creating virtualenv it has shown us the location of that env. This virtualenv created by pipenv is regular env that you might be using and you can activate that env manually if you like.
  5. Pipenv gives us simple command to interact with that virtualenv rather then doing manually
  6. After the virtualenv location message you can see message creating pipfile for this project, so this pile is going to be the file that describes out env this file is similar to requirements.txt file.
  7. After installing it says that it has added request to pipfile pachage.
  8. Now it says that pipfile.lock was not found so it create automatically. it is similar to pipfile there are some key difference in it. (We are not suppose to edit this file by our self because this file is being controlled by pipenv command)
  9. Lastly it says that we can activate our new environment using following command pipenv shell and we want to run command inside that then we can use command pipenv run .

pipfile

  • I have open this file in my visual studio code. (you can use any editor)
pipfile
  • The format that this file is using is toml which is designed minimal format that mostly contains keys & values within the sections.
  • So there are 4 section in this file, lets look one by one
  • [[source]] section → This section simply says that from where pipenv is downloading the packages.
  • [packges] section → It lists the packages that we have installed, here we have django = ‘*’ so star means we haven’t specity any version for that package when we where installing (you can see on the screenshot where we installed dajngo using pipenv).
  • [requires] section → This simply describes that which version of python we are using.

Pipfile.lock

  • I have open pipfile.lock in visual studio code. (you can use any editor)

This file is very big so don’t worry, because this is a file that we are not meant to change. this is auto generated file that has more detailed information about our current environment.

So you will scroll down and see that it also has other packages excluding django so those packages are the package that gets installed with django. and this file hase exact version of the package version which are installed.

Activating virtualenv

  • Open your command line and write command pipenv shell
Activated virtualenv
  • Now when your virtualenv is activated then you will see the name of the virtualenv at the starting of your command line.

Deactivate virtualenv

  • To deactivate our virtualenv there is different command to deactivate virtualenv which is created my pipenv so that command is exit .
exit from virtualenv.

How to run command in virtualenv without activaing ?

  • So to run command in in virtualenv without activating the we can run using command pipenv run .
created django project using pipenv run command
  • In above screenshot i have run the python shell in virtualenv and shown the path of the virtualenv just co confirm that it is using virtualenv or not.

How to install requirements.txt in pipenv ?

  • Command to install requirements.txt file is pipenv install -r reqirements.txt .
Installed requirements.txt
  • Now you will see pipfile the it will b updated
pip file updated after installing requirements.txt

How to create requirements.txt using pipenv ?

  • So in pipenv we have command similer to pip freeze and that command is pipenv lock -r and this command will list the packages and you can copy and paste to requirements.txt

Hope this blog was informative to you, Thank you
- Yash Patel ❤️

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Yash Patel

Written by

I am Full Stack Developer

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.