Hướng dẫn dùng define awhile python

The standard package manager for Python is

C:\> echo %PATH%
8. It allows you to install and manage packages that aren’t part of the Python standard library. If you’re looking for an introduction to
C:\> echo %PATH%
8, then you’ve come to the right place!

In this tutorial, you’ll learn how to:

  • Set up
    C:\> echo %PATH%
    
    8 in your working environment
  • Fix common errors related to working with
    C:\> echo %PATH%
    
    8
  • Install and uninstall packages with
    C:\> echo %PATH%
    
    8
  • Manage projects’ dependencies using requirements files

You can do a lot with

C:\> echo %PATH%
8, but the Python community is very active and has created some neat alternatives to
C:\> echo %PATH%
8. You’ll learn about those later in this tutorial.

Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.

Getting Started With
C:\> echo %PATH%
8

So, what exactly does

C:\> echo %PATH%
8 do?
C:\> echo %PATH%
8 is a package manager for Python. That means it’s a tool that allows you to install and manage libraries and dependencies that aren’t distributed as part of the standard library. The name pip was introduced by Ian Bicking in 2008:

I’ve finished renaming pyinstall to its new name: pip. The name pip is [an] acronym and declaration: pip installs packages. [Source]

Package management is so important that Python’s installers have included

C:\> echo %PATH%
8 since versions 3.4 and 2.7.9, for Python 3 and Python 2, respectively. Many Python projects use
C:\> echo %PATH%
8, which makes it an essential tool for every Pythonista.

The concept of a package manager might be familiar to you if you’re coming from another programming language. JavaScript uses npm for package management, Ruby uses gem, and the .NET platform uses NuGet. In Python,

C:\> echo %PATH%
8 has become the standard package manager.

Remove ads

Finding
C:\> echo %PATH%
8 on Your System

The Python 3 installer gives you the option to install

C:\> echo %PATH%
8 when installing Python on your system. In fact, the option to install
C:\> echo %PATH%
8 with Python is checked by default, so
C:\> echo %PATH%
8 should be ready for you to use after installing Python.

Note: On some Linux [Unix] systems like Ubuntu,

C:\> echo %PATH%
8 comes in a separate package called
C:\> python -m ensurepip --upgrade
6, which you need to install with
C:\> python -m ensurepip --upgrade
7. It’s not installed by default with the interpreter.

You can verify that

C:\> echo %PATH%
8 is available by looking for the
C:\> python -m ensurepip --upgrade
9 executable on your system. Select your operating system below and use your platform-specific command accordingly:

C:\> where pip3

The

$ python3 -m ensurepip --upgrade
0 command on Windows will show you where you can find the executable of
C:\> python -m ensurepip --upgrade
9. If Windows can’t find an executable named
C:\> python -m ensurepip --upgrade
9, then you can also try looking for
C:\> echo %PATH%
8 without the three [
$ python3 -m ensurepip --upgrade
4] at the end.

$ which pip3

The

$ python3 -m ensurepip --upgrade
5 command on Linux systems and macOS shows you where the
C:\> python -m ensurepip --upgrade
9 binary file is located.

On Windows and Unix systems,

C:\> python -m ensurepip --upgrade
9 may be found in more than one location. This can happen when you have multiple Python versions installed. If you can’t find
C:\> echo %PATH%
8 in any location on your system, then you may consider .

Instead of running your system

C:\> echo %PATH%
8 directly, you can also run it as a Python module. In the next section, you’ll learn how.

Running
C:\> echo %PATH%
8 as a Module

When you run your system

C:\> echo %PATH%
8 directly, the command itself doesn’t reveal which Python version
C:\> echo %PATH%
8 belongs to. This unfortunately means that you could use
C:\> echo %PATH%
8 to install a package into the site-packages of an old Python version without noticing. To prevent this from happening, you can run
C:\> echo %PATH%
8 as a Python module:

$ python3 -m pip

Notice that you use

C:\> python get-pip.py
5 to run
C:\> echo %PATH%
8. The
C:\> python get-pip.py
7 switch tells Python to run a module as an executable of the
C:\> python get-pip.py
8 interpreter. This way, you can ensure that your system default Python 3 version runs the
C:\> echo %PATH%
8 command. If you want to learn more about this way of running
C:\> echo %PATH%
8, then you can read Brett Cannon’s insightful article about the advantages of using
$ which pip3
01.

Sometimes you may want to be more explicit and limit packages to a specific project. In situations like this, you should run

C:\> echo %PATH%
8 inside a virtual environment.

Using
C:\> echo %PATH%
8 in a Python Virtual Environment

To avoid installing packages directly into your system Python installation, you can use a virtual environment. A virtual environment provides an isolated Python interpreter for your project. Any packages that you use inside this environment will be independent of your system interpreter. This means that you can keep your project’s dependencies separate from other projects and the system at large.

Using

C:\> echo %PATH%
8 inside a virtual environment has three main advantages. You can:

  1. Be sure that you’re using the right Python version for the project at hand
  2. Be confident that you’re referring to the correct
    C:\> echo %PATH%
    
    8 instance when running
    C:\> echo %PATH%
    
    8 or
    C:\> python -m ensurepip --upgrade
    
    9
  3. Use a specific package version for your project without affecting other projects

Python 3 has the built-in

$ which pip3
08 module for creating virtual environments. This module helps you create virtual environments with an isolated Python installation. Once you’ve activated the virtual environment, then you can install packages into this environment. The packages that you install into one virtual environment are isolated from all other environments on your system.

You can follow these steps to create a virtual environment and verify that you’re using the

C:\> echo %PATH%
8 module inside the newly created environment:

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]

$ python3 -m venv venv
$ source venv/bin/activate
[venv] $ pip3 --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]
[venv] $ pip --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]

Here you create a virtual environment named

$ which pip3
08 by using Python’s built-in
$ which pip3
08 module. Then you activate it with the
$ which pip3
12 command. The parentheses [
$ which pip3
13] surrounding your
$ which pip3
08 name indicate that you successfully activated the virtual environment.

Finally, you check the version of the

C:\> python -m ensurepip --upgrade
9 and
C:\> echo %PATH%
8 executables inside your activated virtual environment. Both point to the same
C:\> echo %PATH%
8 module, so once your virtual environment is activated, you can use either
C:\> echo %PATH%
8 or
C:\> python -m ensurepip --upgrade
9.

Remove ads

Reinstalling
C:\> echo %PATH%
8 When Errors Occur

When you run the

C:\> echo %PATH%
8 command, you may get an error in some cases. Your specific error message will depend on your operating system:

Operating SystemError MessageWindows

$ which pip3
22
$ which pip3
23Linux
$ which pip3
24macOS
$ which pip3
25

Error messages like these indicate that something went wrong with the installation of

C:\> echo %PATH%
8.

Note: Before you start any troubleshooting when the

C:\> echo %PATH%
8 command doesn’t work, you can try out using the
C:\> python -m ensurepip --upgrade
9 command with the three [
$ python3 -m ensurepip --upgrade
4] at the end.

Getting errors like the ones shown above can be frustrating because

C:\> echo %PATH%
8 is vital for installing and managing external packages. Some common problems with
C:\> echo %PATH%
8 are related to how this tool was installed on your system.

Although the error messages for various systems differ, they all point to the same problem: Your system can’t find

C:\> echo %PATH%
8 in the locations listed in your
$ which pip3
33 variable. On Windows,
$ which pip3
33 is part of the system variables. On macOS and Linux,
$ which pip3
33 is part of the environment variables. You can check the contents of your
$ which pip3
33 variable with this command:

C:\> echo %PATH%

$ echo $PATH

The output of this command will show a list of locations [directories] on your disk where the operating system looks for executable programs. Depending on your system, locations can be separated by a colon [

$ which pip3
37] or a semicolon [
$ which pip3
38].

By default, the directory that contains the

C:\> echo %PATH%
8 executable should be present in
$ which pip3
33 after you install Python or create a virtual environment. However, missing
C:\> echo %PATH%
8 is a common issue. Two can help you install
C:\> echo %PATH%
8 again and add it to your
$ which pip3
33:

  1. The module
  2. The
    $ which pip3
    
    45 script

The

$ which pip3
44 module has been part of the standard library since Python 3.4. It was added to provide a straightforward way for you to reinstall
C:\> echo %PATH%
8 if, for example, you skipped it when installing Python or you uninstalled
C:\> echo %PATH%
8 at some point. Select your operating system below and run
$ which pip3
44 accordingly:

C:\> python -m ensurepip --upgrade

$ python3 -m ensurepip --upgrade

If

C:\> echo %PATH%
8 isn’t installed yet, then this command installs it in your current Python environment. If you’re in an active virtual environment, then the command installs
C:\> echo %PATH%
8 into that environment. Otherwise, it installs
C:\> echo %PATH%
8 globally on your system. The
$ which pip3
53 option ensures that the
C:\> echo %PATH%
8 version is the same as the one declared in
$ which pip3
44.

Note: The

$ which pip3
44 module doesn’t access the internet. The latest version of
C:\> echo %PATH%
8 that
$ which pip3
44 can install is the version that’s bundled in your environment’s Python installation. For example, running
$ which pip3
44 with Python 3.10.0 installs
C:\> echo %PATH%
8 21.2.3. If you want a newer
C:\> echo %PATH%
8 version, then you’d need to first run
$ which pip3
44. Afterward, you can update
C:\> echo %PATH%
8 manually to its latest version.

Another way to fix your

C:\> echo %PATH%
8 installation is to use the
$ which pip3
45 script. The
$ which pip3
45 file contains a full copy of
C:\> echo %PATH%
8 as an encoded ZIP file. You can download
$ which pip3
45 directly from the PyPA bootstrap page. Once you have the script on your machine, then you run the Python script like this:

C:\> python get-pip.py

$ which pip3
0

This script will install the latest version of

C:\> echo %PATH%
8,
$ which pip3
70, and
$ which pip3
71 in your current Python environment. If you only want to install
C:\> echo %PATH%
8, then you can add the
$ which pip3
73 and
$ which pip3
74 options to your command.

If none of the methods above work, then it might be worth trying to download the latest Python version for your current platform. You can follow the Python 3 Installation & Setup Guide to make sure that

C:\> echo %PATH%
8 is appropriately installed and works without errors.

Remove ads

Installing Packages With
C:\> echo %PATH%
8

Python is considered a language. This means that the Python standard library contains an extensive set of packages and modules to help developers with their coding projects.

At the same time, Python has an active community that contributes an even more extensive set of packages that can help you with your development needs. These packages are published to the Python Package Index, also known as PyPI [pronounced Pie Pea Eye].

PyPI hosts an extensive collection of packages, including development frameworks, tools, and libraries. Many of these packages provide friendly interfaces to the Python standard library’s functionality.

Using the Python Package Index [PyPI]

One of the many packages that PyPI hosts is called

$ which pip3
77. The
$ which pip3
77 library helps you to interact with web services by abstracting the complexities of HTTP requests. You can learn all about
$ which pip3
77 on its official documentation site.

When you want to use the

$ which pip3
77 package in your project, you must first install it into your environment. If you don’t want to install it in your system Python site-packages, then you can create a virtual environment first, as shown above.

Once you’ve created the virtual environment and activated it, then your command-line prompt shows the name of the virtual environment inside the parentheses. Any

C:\> echo %PATH%
8 commands that you perform from now on will happen inside your virtual environment.

To install packages,

C:\> echo %PATH%
8 provides an
$ which pip3
83 command. You can run it to install the
$ which pip3
77 package:

$ which pip3
1

$ which pip3
2

In this example, you run

C:\> echo %PATH%
8 with the
$ which pip3
83 command followed by the name of the package that you want to install. The
C:\> echo %PATH%
8 command looks for the package in PyPI, resolves its dependencies, and installs everything in your current Python environment to ensure that
$ which pip3
77 will work.

The

$ which pip3
89 command always looks for the latest version of the package and installs it. It also searches for dependencies listed in the package metadata and installs them to ensure that the package has all the requirements that it needs.

It’s also possible to install multiple packages in a single command:

$ which pip3
3

$ which pip3
4

By chaining the packages

$ which pip3
90 and
$ which pip3
91 in the
$ which pip3
92 command, you install both packages at once. You can add as many packages as you want to the
$ which pip3
92 command. In cases like this, a
$ which pip3
94 file can come in handy. Later in this tutorial, you’ll learn how to use a
$ which pip3
94 file to install many packages at once.

Note: Unless the specific version number of a package is relevant to this tutorial, you’ll notice the version string takes the generic form of

$ which pip3
96. This is a placeholder format and can stand for
$ which pip3
97,
$ which pip3
98, or any other version number. When you follow along, the output in your terminal will display your actual package version numbers.

You can use the

$ which pip3
99 command to display the packages installed in your environment, along with their version numbers:

$ which pip3
5

$ which pip3
6

The

$ python3 -m pip
00 command renders a table that shows all installed packages in your current environment. The output above shows the version of the packages using an
$ which pip3
96 placeholder format. When you run the
$ python3 -m pip
00 command in your environment,
C:\> echo %PATH%
8 displays the specific version number that you’ve installed for each package.

To get more information about a specific package, you can look at the package’s metadata by using the

$ python3 -m pip
04 command in
C:\> echo %PATH%
8:

$ which pip3
7

$ which pip3
8

The output of this command on your system will list the package’s metadata. The

$ python3 -m pip
06 line lists packages, such as
$ python3 -m pip
07,
$ python3 -m pip
08,
$ python3 -m pip
09, and
$ python3 -m pip
10. These were installed because
$ which pip3
77 depends on them to work correctly.

Now that you’ve installed

$ which pip3
77 and its dependencies, you can import it just like any other regular package in your Python code. Start the interactive Python interpreter and import the
$ which pip3
77 package:

>>>

$ which pip3
9

After starting the interactive Python interpreter, you imported the

$ which pip3
77 module. By calling
$ python3 -m pip
15, you verified that you were using the
$ which pip3
77 module within your virtual environment.

Remove ads

Using a Custom Package Index

By default,

C:\> echo %PATH%
8 uses PyPI to look for packages. But
C:\> echo %PATH%
8 also gives you the option to define a custom package index.

Using

C:\> echo %PATH%
8 with a custom index can be helpful when the PyPI domain is blocked on your network or if you want to work with packages that aren’t publicly available. Sometimes system administrators also create their own internal package index to better control which package versions are available to
C:\> echo %PATH%
8 users on the company’s network.

A custom package index must comply with PEP 503 – Simple Repository API to work with

C:\> echo %PATH%
8. You can get an impression of how such an API [Application Programming Interface] looks by visiting the PyPI Simple Index—but be aware that this is a large page with a lot of hard-to-parse content. Any custom index that follows the same API can be targeted with the
$ python3 -m pip
22 option. Instead of typing
$ python3 -m pip
22, you can also use the
$ python3 -m pip
24 shorthand.

For example, to install the

$ which pip3
90 tool from the TestPyPI package index, you can run the following command:

$ python3 -m pip
0

$ python3 -m pip
1

With the

$ python3 -m pip
24 option, you tell
C:\> echo %PATH%
8 to look at a different package index instead of PyPI, the default one. Here, you’re installing
$ which pip3
90 from TestPyPI rather than from PyPI. You can use TestPyPI to fine-tune the publishing process for your Python packages without cluttering the production package index on PyPI.

If you need to use an alternative index permanently, then you can set the

$ python3 -m pip
29 option in the
C:\> echo %PATH%
8 configuration file. This file is called
$ python3 -m pip
31, and you can find its location by running the following command:

$ python3 -m pip
2

$ python3 -m pip
3

With the

$ python3 -m pip
32 command, you can list the active configuration. This command only outputs something when you have custom configurations set. Otherwise, the output is empty. That’s when the additive
$ python3 -m pip
33, or
$ python3 -m pip
34, option can be helpful. When you add
$ python3 -m pip
34,
C:\> echo %PATH%
8 shows you where it looks for the different configuration levels.

If you want to add a

$ python3 -m pip
31 file, then you can choose one of the locations that
$ python3 -m pip
38 listed. A
$ python3 -m pip
31 file with a custom package index looks like this:

$ python3 -m pip
4

When you have a

$ python3 -m pip
31 file like this,
C:\> echo %PATH%
8 will use the defined
$ python3 -m pip
29 to look for packages. With this configuration, you don’t need to use the
$ python3 -m pip
22 option in your
$ which pip3
92 command to specify that you only want packages that can be found in the Simple API of TestPyPI.

Installing Packages From Your GitHub Repositories

You’re not limited to packages hosted on PyPI or other package indexes.

C:\> echo %PATH%
8 also provides the option to install packages from a GitHub repository. But even when a package is hosted on PyPI, like the Real Python directory tree generator, you can opt to install it from its Git repository:

$ python3 -m pip
5

$ python3 -m pip
6

With the

$ python3 -m pip
46 scheme, you can point to a Git repository that contains an installable package. You can verify that you installed the package correctly by running an interactive Python interpreter and importing
$ which pip3
90:

>>>

$ python3 -m pip
7

After starting the interactive Python interpreter, you import the

$ which pip3
90 module. By calling
$ python3 -m pip
49, you verify that you’re using the
$ which pip3
90 module that’s based in your virtual environment.

Note: If you’re using a version control system [VCS] other than Git,

C:\> echo %PATH%
8 has you covered. To learn how to use
C:\> echo %PATH%
8 with Mercurial, Subversion, or Bazaar, check out the VCS Support chapter of the
C:\> echo %PATH%
8 documentation.

Installing packages from a Git repository can be helpful if the package isn’t hosted on PyPI but has a remote Git repository. The remote repository you point

C:\> echo %PATH%
8 to can even be hosted on an internal Git server on your company’s intranet. This can be useful when you’re behind a firewall or have other restrictions for your Python projects.

Remove ads

Installing Packages in Editable Mode to Ease Development

When working on your own package, installing it in an editable mode can make sense. By doing this, you can work on the source code while still using your command line like you would in any other package. A typical workflow is to first clone the repository and then use

C:\> echo %PATH%
8 to install it as an editable package in your environment:

$ python3 -m pip
8

$ python3 -m pip
9

With the commands above, you installed the

$ which pip3
90 package as an editable module. Here’s a step-by-step breakdown of the actions you just performed:

  1. Line 1 cloned the Git repository of the
    $ which pip3
    
    90 package.
  2. Line 2 changed the working directory to
    $ python3 -m pip
    
    58.
  3. Lines 3 and 4 created and activated a virtual environment.
  4. Line 5 installed the content of the current directory as an editable package.

The

$ python3 -m pip
59 option is shorthand for the
$ python3 -m pip
60 option. When you use the
$ python3 -m pip
59 option with
$ which pip3
92, you tell
C:\> echo %PATH%
8 that you want to install the package in editable mode. Instead of using a package name, you use a dot [
$ python3 -m pip
64] to point
C:\> echo %PATH%
8 to the current directory.

If you hadn’t used the

$ python3 -m pip
59 flag,
C:\> echo %PATH%
8 would’ve installed the package normally into your environment’s
$ python3 -m pip
68 folder. When you install a package in editable mode, you’re creating a link in the site-packages to the local project path:

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
0

Using the

$ which pip3
92 command with the
$ python3 -m pip
59 flag is just one of many options that
$ which pip3
92 offers. You can check out in the
C:\> echo %PATH%
8 documentation. There you’ll learn how to install specific versions of a package or point
C:\> echo %PATH%
8 to a different index that’s not PyPI.

In the next section, you’ll learn how requirements files can help with your

C:\> echo %PATH%
8 workflows.

Using Requirements Files

The

$ which pip3
92 command always installs the latest published version of a package, but sometimes your code requires a specific package version to work correctly.

You want to create a specification of the dependencies and versions that you used to develop and test your application so that there are no surprises when you use the application in production.

Pinning Requirements

When you share your Python project with other developers, you may want them to use the same versions of external packages that you’re using. Maybe a specific version of a package contains a new feature that you rely on, or the version of a package that you’re using is incompatible with former versions.

These external dependencies are also called requirements. You’ll often find Python projects that pin their requirements in a file called

$ which pip3
94 or similar. The requirements file format allows you to specify precisely which packages and versions should be installed.

Running

$ python3 -m pip
78 shows that there’s a
$ python3 -m pip
79 command that outputs the installed packages in requirements format. You can use this command, redirecting the output to a file to generate a requirements file:

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
1

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
2

This command creates a

$ which pip3
94 file in your working directory with the following content:

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
3

Remember that

$ which pip3
96 displayed above is a placeholder format for the package versions. Your
$ which pip3
94 file will contain real version numbers.

The

$ python3 -m pip
79 command dumps the name and version of the currently installed packages to standard output. You can redirect the output to a file that you can later use to install your exact requirements into another system. You can name the requirements file whatever you want. However, a widely adopted convention is to name it
$ which pip3
94.

When you want to replicate the environment in another system, you can run

$ which pip3
92, using the
$ python3 -m pip
86 switch to specify the requirements file:

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
4

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
5

In the command above, you tell

C:\> echo %PATH%
8 to install the packages listed in
$ which pip3
94 into your current environment. The package versions will match the version constraints that the
$ which pip3
94 file contains. You can run
$ python3 -m pip
00 to display the packages you just installed, with their version numbers:

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
6

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
7

Now you’re ready to share your project! You can submit

$ which pip3
94 into a version control system like Git and use it to replicate the same environment on other machines. But wait, what happens if new updates are released for these packages?

Remove ads

Fine-Tuning Requirements

The problem with hardcoding your packages’ versions and dependencies is that packages are updated frequently with bug and security fixes. You probably want to leverage those updates as soon as they’re published.

The requirements file format allows you to specify dependency versions using comparison operators that give you some flexibility to ensure packages are updated while still defining the base version of a package.

Open

$ which pip3
94 in your favorite editor and turn the equality operators [
$ python3 -m pip
93] into greater than or equal to operators [
$ python3 -m pip
94], like in the example below:

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
8

You can change the to

$ python3 -m pip
94 to tell
C:\> echo %PATH%
8 to install an exact or greater version that has been published. When you set a new environment by using the
$ which pip3
94 file,
C:\> echo %PATH%
8 looks for the latest version that satisfies the requirement and installs it.

Next, you can upgrade the packages in your requirements file by running the

$ which pip3
83 command with the
$ which pip3
53 switch or the
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
01 shorthand:

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
9

$ python3 -m venv venv
$ source venv/bin/activate
[venv] $ pip3 --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]
[venv] $ pip --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]
0

If a new version is available for a listed package, then the package will be upgraded.

In an ideal world, new versions of packages would be backward compatible and would never introduce new bugs. Unfortunately, new versions can introduce changes that’ll break your application. To fine-tune your requirements, the requirements file syntax supports additional .

Imagine that a new version,

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
02, of
$ which pip3
77 is published but introduces an incompatible change that breaks your application. You can modify the requirements file to prevent
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
02 or higher from being installed:

$ python3 -m venv venv
$ source venv/bin/activate
[venv] $ pip3 --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]
[venv] $ pip --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]
1

Changing the version specifier for the

$ which pip3
77 package ensures that any version greater than or equal to
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
02 doesn’t get installed. The
C:\> echo %PATH%
8 documentation provides extensive information about the requirements file format, and you can consult it to learn more.

Separating Production and Development Dependencies

Not all packages that you install during the development of your applications will be production dependencies. For example, you’ll probably want to test your application, so you need a test framework. A popular framework for testing is

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
08. You want to install it in your development environment, but you don’t want it in your production environment, because it isn’t a production dependency.

You create a second requirements file,

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
09, to list additional tools to set up a development environment:

$ python3 -m venv venv
$ source venv/bin/activate
[venv] $ pip3 --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]
[venv] $ pip --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]
2

Having two requirements files will demand that you use

C:\> echo %PATH%
8 to install both of them,
$ which pip3
94 and
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
09. Fortunately,
C:\> echo %PATH%
8 allows you to specify additional parameters within a requirements file, so you can modify
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
09 to also install the requirements from the production
$ which pip3
94 file:

$ python3 -m venv venv
$ source venv/bin/activate
[venv] $ pip3 --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]
[venv] $ pip --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]
3

Notice that you use the same

$ python3 -m pip
86 switch to install the production
$ which pip3
94 file. Now, in your development environment, you only have to run this single command to install all requirements:

$ python3 -m venv venv
$ source venv/bin/activate
[venv] $ pip3 --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]
[venv] $ pip --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]
4

$ python3 -m venv venv
$ source venv/bin/activate
[venv] $ pip3 --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]
[venv] $ pip --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]
5

Because

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
09 contains the
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
19 line, you’ll install not only
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
08 but also the pinned requirements of
$ which pip3
94. In a production environment, it’s sufficient to install the production requirements only:

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
4

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
5

With this command, you install the requirements listed in

$ which pip3
94. In contrast to your development environment, your production environment won’t have
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
08 installed.

Remove ads

Freezing Requirements for Production

You created the production and development requirement files and added them to source control. These files use flexible version specifiers to ensure that you leverage bug fixes published by your dependencies. You’ve also tested your application and are now ready to deploy it to production.

You know that all the tests pass and the application works with the dependencies that you used in your development process, so you probably want to ensure that you deploy identical versions of dependencies to production.

The current version specifiers don’t guarantee that the identical versions will be deployed to production, so you want to freeze the production requirements before releasing your project.

After you’ve finished development with your current requirements, a workflow to create a new release of your current project can look like this:

StepCommandExplanation1

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
08Run your tests and verify that your code is working properly.2
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
25Upgrade your requirements to versions that match the constraints in your
$ which pip3
94 file.3
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
08Run your tests and consider downgrading any dependency that introduced errors to your code.4
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
28Once the project works correctly, freeze the dependencies into a
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
29 file.

With a workflow like this, the

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
29 file will contain exact version specifiers and can be used to replicate your environment. You’ve ensured that when your users install the packages listed in
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
29 into their own environments, they’ll be using the versions that you intend them to use.

Freezing your requirements is an important step to ensure that your Python project works the same way for your users in their environments as it did in yours.

Uninstalling Packages With
C:\> echo %PATH%
8

Once in a while, you’ll have to uninstall a package. Either you found a better library to replace it, or it’s something that you don’t need. Uninstalling packages can be a bit tricky.

Notice that when you installed

$ which pip3
77, you got
C:\> echo %PATH%
8 to install other dependencies too. The more packages you install, the bigger the chance that multiple packages depend on the same dependency. This is where the
$ python3 -m pip
04 command in
C:\> echo %PATH%
8 comes in handy.

Before you uninstall a package, make sure to run the

$ python3 -m pip
04 command for that package:

$ python3 -m venv venv
$ source venv/bin/activate
[venv] $ pip3 --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]
[venv] $ pip --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]
8

$ python3 -m venv venv
$ source venv/bin/activate
[venv] $ pip3 --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]
[venv] $ pip --version
pip 21.2.3 from .../python3.10/site-packages/pip [python 3.10]
9

Notice the last two fields,

$ python3 -m pip
06 and
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
39. The
$ python3 -m pip
04 command tells you that
$ which pip3
77 requires
$ python3 -m pip
07,
$ python3 -m pip
08,
$ python3 -m pip
09, and
$ python3 -m pip
10. You probably want to uninstall those too. Notice that
$ which pip3
77 isn’t required by any other package. So it’s safe to uninstall it.

You should run the

$ python3 -m pip
04 command against all of the
$ which pip3
77 dependencies to ensure that no other libraries also depend on them. Once you understand the dependency order of the packages that you want to uninstall, then you can remove them using the
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
49 command:

C:\> echo %PATH%
0

C:\> echo %PATH%
1

The

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
49 command shows you the files that will be removed and asks for confirmation. If you’re sure that you want to remove the package because you’ve checked its dependencies and know that nothing else is using it, then you can pass a
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
51 switch to suppress the file list and confirmation dialog:

C:\> echo %PATH%
2

C:\> echo %PATH%
3

Here you uninstall

$ python3 -m pip
10. Using the
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
51 switch, you suppress the confirmation dialog asking you if you want to uninstall this package.

In a single call, you can specify all the packages that you want to uninstall:

C:\> echo %PATH%
4

C:\> echo %PATH%
5

You can pass in multiple packages to the

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
54 command. If you didn’t add any additional switches, then you’d need to confirm uninstalling each package. By passing the
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
51 switch, you can uninstall them all without any confirmation dialog.

You can also uninstall all the packages listed in a requirements file by providing the

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
56 option. This command will prompt a confirmation request for each package, but you can suppress it with the
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
51 switch:

C:\> echo %PATH%
6

C:\> echo %PATH%
7

Remember to always check the dependencies of packages that you want to uninstall. You probably want to uninstall all dependencies, but uninstalling a package used by others will break your working environment. In consequence, your project may not work correctly anymore.

If you’re working in a virtual environment, it can be less work to just create a new virtual environment. Then you can install the packages that you need instead of trying to uninstall the packages that you don’t need. However,

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
54 can be really helpful when you need to uninstall a package from your system Python installation. Using
C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
[venv] C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
[venv] C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip [python 3.10]
54 is a good way to declutter your system if you accidentally install a package system-wide.

Remove ads

Exploring Alternatives to
C:\> echo %PATH%
8

The Python community provides excellent tools and libraries for you to use beyond

C:\> echo %PATH%
8. These include alternatives to
C:\> echo %PATH%
8 that try to simplify and improve package management.

Here are some other package management tools that are available for Python:

ToolDescriptionCondaConda is a package, dependency, and environment manager for many languages, including Python. It comes from Anaconda, which started as a data science package for Python. Consequently, it’s widely used for data science and machine learning applications. Conda operates its own index to host compatible packages.PoetryPoetry will look very familiar to you if you’re coming from JavaScript and npm. Poetry goes beyond package management, helping you build distributions for your applications and libraries and deploying them to PyPI.PipenvPipenv is another package management tool that merges virtual environment and package management in a single tool. Pipenv: A Guide to the New Python Packaging Tool is a great place to start learning about Pipenv and its approach to package management.

Only

C:\> echo %PATH%
8 comes bundled in the standard Python installation. If you want to use any alternatives listed above, then you have to follow the installation guides in their documentation. With so many options, you’re sure to find the right tools for your programming journey!

Conclusion

Many Python projects use the

C:\> echo %PATH%
8 package manager to manage their dependencies. It’s included with the Python installer, and it’s an essential tool for dependency management in Python.

In this tutorial, you learned how to:

  • Set up and run
    C:\> echo %PATH%
    
    8 in your working environment
  • Fix common errors related to working with
    C:\> echo %PATH%
    
    8
  • Install and uninstall packages with
    C:\> echo %PATH%
    
    8
  • Define requirements for your projects and applications
  • Pin dependencies in requirements files

In addition, you’ve learned about the importance of keeping dependencies up to date and alternatives to

C:\> echo %PATH%
8 that can help you manage those dependencies.

By taking a closer look at

C:\> echo %PATH%
8, you’ve explored an essential tool in your Python development workflows. With
C:\> echo %PATH%
8, you can install and manage any additional packages that you find on PyPI. You can use external packages from other developers as requirements and concentrate on the code that makes your project unique.

Mark as Completed

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: A Beginner's Guide to Pip

🐍 Python Tricks 💌

Get a short & sweet Python Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

Send Me Python Tricks »

About Philipp Acsany

Philipp is a Berlin-based software engineer with a graphic design background and a passion for full-stack web development.

» More about Philipp

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

Aldren

Brad

Dan

Geir Arne

Isaac

Joanna

Kate

Leodanis

Master Real-World Python Skills With Unlimited Access to Real Python

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

Master Real-World Python Skills
With Unlimited Access to Real Python

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

What Do You Think?

Rate this article:

Tweet Share Share Email

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. and get answers to common questions in our support portal.

Chủ Đề