Chengzhihe: how to start using Pipenv?

In this blog post, I will discuss how to get started Pipenv A Python packaging tool. This blog post follows the Ubuntu workflow and can be easily copied in MacOS and Windows. Let's start.

What is? Pipenv

Pipenv is Python's Python packaging tool, which is used for Pip,Venv And requirements Txt upgrade. Pipenv is a good way to combine package management with virtual environment.

Why do we need package management and virtual environments?

According to Wikipedia,

A package manager or package management system is a set of software tools that automatically perform the process of installing, upgrading, configuring, and deleting computer programs of a computer operating system in a consistent manner.

Package manager automates the process of installing, uninstalling and maintaining packages. This helps developers easily manage project dependencies.

sure Here Read more about package manager.

Now let's talk about virtual environments,

A virtual environment is a self-contained directory tree that contains Python installations for a particular Python version, as well as many other packages.

The virtual environment enables us to have a specific and unique python installation for each project. This prevents us from overloading the global python installation and enables us to use different versions of python for each project.
The Python virtual environment also helps isolate the dependencies of each project and prevents code interruption if any project is specifically configured as a Python version.
You can here Learn more about virtual environments.

Now that we know what package managers are and why they are needed, let's start installing Pipenv.

How do I install Pipenv?

To install pipenv, open a terminal window and run the following command:

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#201e2f"><span style="color:#f8f8f2"><code>$ pip install pipenv</code></span></span></span></span>

How do I create a virtual environment using Pipenv?

Navigate to the directory where you want to create the virtual environment and open a terminal window and type the following command.

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#201e2f"><span style="color:#f8f8f2"><code>$ mkdir my_project
$ cd my_project/
$ pipenv install</code></span></span></span></span>

How do I start a virtual environment using Pipenv?

To start the virtual environment, type the following command in the directory.

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#201e2f"><span style="color:#f8f8f2"><code>$ pipenv shell</code></span></span></span></span>

You will see a project name in parentheses, indicating that we have successfully entered the required Python virtual environment.

To exit the virtual environment, we can enter,

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#201e2f"><span style="color:#f8f8f2"><code>$ <span style="color:#e6db74">exit</span></code></span></span></span></span>

How do I check which Python installation is being used?

To check the python installation in use, we can use the following three methods,

Method 1:

When the python shell is active, type the following command,

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#201e2f"><span style="color:#f8f8f2"><code>$ <span style="color:#e6db74">which</span> python</code></span></span></span></span>

This returns the path to the python environment currently in use.

Method 2:

Enter this in the active python shell,

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#201e2f"><span style="color:#f8f8f2"><code>$ import sys
$ sys.executable</code></span></span></span></span>

This will return the python installation path in use.

Method 3:

To find the path to the executable without activating the shell, we can use the following command:

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#201e2f"><span style="color:#f8f8f2"><code>$ pipenv --venv</code></span></span></span></span>

How do I install packages using Pipenv?

Type the following code to install a package using Pipenv.

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#201e2f"><span style="color:#f8f8f2"><code>$ pipenv <span style="color:#f92672">install</span> <<span style="color:#f92672">package</span>-<span style="color:#f92672">name</span>></code></span></span></span></span>

How do I run Python commands without activating a virtual environment in the current environment?

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#201e2f"><span style="color:#f8f8f2"><code>$ pipenv run python</code></span></span></span></span>

To run the file, use the following command:

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#201e2f"><span style="color:#f8f8f2"><code>$ pipenv run  python <span style="color:#f8f8f2 !important"><<span style="color:#f92672">file-name</span>></span></code></span></span></span></span>

How to use requirements in Pipenv Txt file?

Requirements to use pip Txt to install dependencies and packages, use the following command:

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#201e2f"><span style="color:#f8f8f2"><code>$ pipenv <span style="color:#f92672">install</span> -r <<span style="color:#f92672">path</span>-<span style="color:#f92672">of</span>-requirements.txt></code></span></span></span></span>

How to use Pipenv to create a requirements txt?

The following commands can be used to generate requirements Txt contents:

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#201e2f"><span style="color:#f8f8f2"><code>$ pipenv lock -r </code></span></span></span></span>

To create a requirements Txt, we can redirect this output to our requirements txt :

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#201e2f"><span style="color:#f8f8f2"><code>$ pipenv lock -r > requirements.txt</code></span></span></span></span>

How do I uninstall packages using Pipenv?

The following commands can be used to uninstall packages using pipenv:

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#201e2f"><span style="color:#f8f8f2"><code>$ pipenv <span style="color:#f92672">uninstall</span> <<span style="color:#f92672">package</span>-<span style="color:#f92672">name</span>></code></span></span></span></span>

To uninstall all packages, use the - all flag.

How do I delete a virtual environment using Pipenv?

The following commands can be used to safely delete packages using pipenv:

<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#201e2f"><span style="color:#f8f8f2"><code>$ pipenv -rm</code></span></span></span></span>

Other points Pipenv needs to know:

  1. By default, pipenv is in ~ / local/share/virtualenvs/.

  2. To install packages that should not be included in the production version, we can -- dev use the flag at the end of the install command.

  3. To check for security vulnerabilities in a virtual environment, we can use the following command:
    $ pipenv check.

  4. You can track all dependencies of a project using the following command:
    $ pipenv graph.

Keywords: Python

Added by syth04 on Sat, 18 Dec 2021 13:24:38 +0200