- 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
- 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
- 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
- Then has five location for the pipfile, we will see this in a minute.
- Then it says that it is using whoch python version and then it goes through and create virtualenv for us.
- 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.
- Pipenv gives us simple command to interact with that virtualenv rather then doing manually
- 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.
- After installing it says that it has added request to pipfile pachage.
- 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)
- Lastly it says that we can activate our new environment using following command
pipenv shelland we want to run command inside that then we can use command
- I have open this file in my visual studio code. (you can use any editor)
- 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.
- 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.
- Open your command line and write command
- Now when your virtualenv is activated then you will see the name of the virtualenv at the starting of your command line.
- To deactivate our virtualenv there is different command to deactivate virtualenv which is created my
pipenvso that command is
How to run command in virtualenv without activaing ?
- So to run command in in virtualenv without activating the we can run using 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.
- Now you will see pipfile the it will b updated
How to create requirements.txt using pipenv ?
- So in pipenv we have command similer to
pip freezeand that command is
pipenv lock -rand 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 ❤️