source: https://docs.python.org/3/library/venv.html
Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world.
It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages. It also generates the ever-important Pipfile.lock, which is used to produce deterministic builds.
Pipenv is primarily meant to provide users and developers of applications with an easy method to setup a working environment.
Some of the features of pipenv are as follows:
pipandvirtualenvare unified so that we do not need to use them separately.- Dependencies are managed using a
PipfileandPipfile.lockis used to separate abstract dependency declarations from the last tested combination. - It allows user to declare dependencies and dev-dependencies which is a way better than managing dependencies with
requirements.txt. - It automatically loads
.envfile so that the development workflow is even easier with environment variables.
If we have python and pip installed, it is easier to install pipenv.
$ pip install --user pipenvto upgrade pipenv, we can use the command below:
$ pip install --user --upgrade pipenv # or
$ pip install --user -U pipenvWe can create a new virtual environment using the command below:
$ pipenv installThe command above will create a virtual environment in $HOME/.virtualenvs/project_name folder.
if we want to install pipenv in the working directory, we have 2 options.
- create an empty directory named
.venvand runpipenv install - set the environment variable
PIPENV_VENV_IN_PROJECT=1in your operating system and runpipenv installcommand.
by doing this, pipenv installs a new virtual environment inside .venv directory of the current folder.
We actually do not need to activate virtual environment when using pipenv since it automatically checks whether the virtual environment is created for the project or not and selects the interpreter itself.
If we want to activate the virtual environment anyways, we can run:
$ pipenv shellwhich automatically activates the virtual environment.
We can then deactivate the virtual environment using:
$ exitThe above command deactivates the virtual environment.
unlike venv or virtualenv, we do not need to activate virtual environment to install packages.
We can install packages using the command:
$ pipenv install package_nameor we can specify the version using:
$ pipenv install package_name==versionThe above command installs packages and dependencies and updates both Pipfile and Pipfile.lock file so that we do not need to create or update requirements file.
We can traditionally install packages using pip install package)name command also, but it do not update the Pipfile and lock file so doing this is discouraged when using Pipenv.
We can uninstall packages using the command below:
$ pipenv uninstall package_name
$ pipenv uninstall package_1 package_2 # multiple packages
THe above command removes the package, its dependencies and updates both Pipfile and Pipfile.lock.
Another Amazing feature of pipenv is that it is able to show dependency graph so that you will be able to know the package and their dependencies too.
we can check the dependency graph using the command below:
$ pipenv graphThe command above shows all the dependencies and their dependencies in the console. The example output is as follows:
$ pipenv graph
djangorestframework==3.13.1
- django [required: >=2.2, installed: 4.0.3]
- asgiref [required: >=3.4.1,<4, installed: 3.5.0]
- sqlparse [required: >=0.2.2, installed: 0.4.2]
- tzdata [required: Any, installed: 2022.1]
- pytz [required: Any, installed: 2022.1]
Pillow==9.1.0You can see the package djangorestframework of version 3.13.1 is installed which requires django of version >=2.2 and installed version is 4.0.3.
similarly the dependencies of django are asgiref, sqlparse, tzdata are installed.