Python Virtual Environment / Dependency Management: Pipenv
Briefly Call It A Combination Of Pip and Virtualenv.
Last updated
Briefly Call It A Combination Of Pip and Virtualenv.
Last updated
Pipenv Provides Additional Features Compared To Venv. I Strongly Recommend That You Install Before You Start Using Pipenv.
Pipenv, Can Be Installed In Python 3.7 and Above.
Create a New Folder and Switch To This Folder.
Create A Python File In Folder (Example: main.py).
Open Terminal Inside Folder (CMD):
Determine Local Python Version With Pyenv.
If Pipenv is Not Installed In The Local Python Version, Install It With pip install pipenv
Command.
Create A Virtual Environment With Pipenv: pipenv install --python python_local_version
To Activate The Virtual Environment (Virtualenv) Of The Project: pipenv shell
To Find Out Where Your Virtual Environment (Virtualenv): pipenv --venv
Pipenv Presents Two New Files: Pipfile and Pipfile.lock
The Pipfile Syntax is TOML and File is Partitioned. Example Pipfile File:
[packages]: Required Packages.
[dev-packages]: Development Packages.
[requires]: Other Requirements, Such As A Specific Python Version.
When Install A Package With Pipenv, It is Ideal To Specify The Version. With The Specified Version, Pipenv Will Automatically Add The Package To The Pipfile. When You Install A Package Without Specifying The Version, Pipenv Will Mark The Version Of the Package As *
In The Pipfile. This Means That The Latest Version Of The Package Will Be Installed. Therefore, When You Install A Package Without Specifying Its Version, It is Important To Update The Pipfile Using The Output Of pip list
or pipenv --graph
There Should Not Be Any Sub-Dependencies Of The Packages You Installed In The Pipfile. Pipenv Does Not Add Sub-Dependencies Of Packages Already Installed In The Pipfile. Sub-Dependencies are Kept In The Pipfile.lock File, Which Allows For A Deterministic Structure. The Syntax Of This File is JSON.
Pipenv’s Dependency Resolution Approach
Pipenv Will Attempt To Install Sub-Dependencies That Meet All The Requirements Of Your Core Dependencies. However, If There are Conflicting Dependencies (Example: Package_a Needs Package_c>=1.0, But Package_b Needs Package_c<1.0), Pipenv Will Not Be Able To Create A Pipfile.lock and Will Give An Error.
pipenv --graph Command Will Print A Tree-Like Structure Showing Your Dependencies. Here is An Example:
From This Output You Can See The Top-Level Dependencies (Flask, Numpy, Pytest) That We Have Already Installed and The Packages That They Depend On.
You Can Also Reverse The Tree:
pipenv graph --reverse
The Inverted Tree May Be More Useful When Trying To Find Overlapping Sub-Dependencies.
When We Want To Send The Project To The Production Environment, We Should Make Sure That There is No Problem In The Pipfile File and Update The Pipfile.lock File With The pipenv lock
Command. Then We Should Create requirements.txt File Using Pipfile and Pipfile.lock File. Use The pipenv requirements > requirements.txt
Command For This.
Only Pipfile and requirements.txt File Should Be Sent To The Production Environment. If There is A Change In Our Project Later (Pipfile), We Should Update The pipfile.lock File With The pipenv lock Command and Create A requirements.txt File Again. We Can Send The Pipfile and requirements.txt File To The Production Environment Again To Enable Users To Install The Current Environment.
So How Will Users Install The Desired Environment Using The Pipfile and Requirements.txt Files? The Same Steps As In Pipenv Installation Steps Should Be Followed, The Only Difference is That The Pipfile and Requirements.txt Files are Already In The Project Folder. When The User executes pipenv install --python python_local_version
Pipenv Will Automatically Install The Packages With Their Sub-Dependencies and Create A Pipfile.lock File.
As A Separate Note, In The Pipfile File, We Said That We Add The Packages That We Use While Developing The Project and Do Not Have Any Effect On The Operation Of The Project To The [dev-packages] Section In The Pipfile File. If We are Going To Send The Project To The Production Environment, We Must Remove These Dev Packages From The Pipfile. If Someone Else Takes The Project To Develop It, These Dev Packages May Remain In The Pipfile, and To Install The Dev Packages Along With The Normal Packages: pipenv install --python python_local_version --dev
pipenv install
numpy
pipenv install
flask==0.12.1
pipenv install
pytest
--dev
The --dev Argument Will Place The Package At The [dev-packages] Location In The Pipfile.
pipenv uninstall numpy
pipenv uninstall --all
This Command is Used To Completely Delete All Installed Packages From Your Virtual Environment.
You Can Replace --all With --all-dev To Remove Only Dev Packages.
To Install Local Package In Virtual Environment: pipenv install -e .
To Find Out Where Your Project Pathway Is: pipenv --where
To Find Out Where The Python Interpreter is Located: pipenv --py
You Can Run Commands In The Virtual Environment Without Entering The Virtual Environment With pipenv shell Command By Using The pipenv run
<command>
command.
Quit Virtual Environment Command: exit
To Remove Virtual Environment: pipenv --rm
Check For Vulnerabilities (and PEP 508 Requirements) In Virtual Environment: pipenv check
To Get Information About Pipenv Commands: pipenv
or pipenv -h
To Learn Version Of Python Used: python -V
To Learn Pip Version Used: pip -V
Pipenv Github Repo: