When you install Python on macOS or Windows and follow our recommendations, pip installs with the interpreter. On Ubuntu, you have to install it separately with this command:
sudo apt update
sudo apt install python3-pip
Running pip
You can call pip directly with the pip
command, but it is better to use a longer one. It guarantees to bring up the latest installed pip version for the desired version of Python.
So, let us call pip:
python3 -m pip --version
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
When showing its version, pip also tells you where it locates and what version of Python it runs on.
Note the structure of the command we called. This command means «python3
, run the -m
module named pip
as a program with the --version
parameter».
If you later see commands like pip help
in the pip documentation, you can call python3 -m pip help
. The result will be the same.
Installing your first package
Let us try to install our first package. We will take the cowsay, which we will install directly in the user environment.
There are several reasons for installing it there:
- We do not interfere with other users of the system with our packages
- We do not need administrator privileges
- We cannot ruin the operating system by accidentally installing a newer package than the system requires. It is crucial in Linux, where Python handles many system tasks
So, let us install cowsay:
We installed the package and made it available to the interpreter. Now we see what it does. It prints a cow that says a user-defined phrase.
The --user
flag in the pip install
install command tells pip that we want to install the package in the global environment of the current user.
If we do not specify this flag, the pip will install the package in the system-wide environment. Try not to do things that would interfere with other users.
The pip, entry points, and PATH
As we saw above, we can use the cowsay package from the code. But this package also has an entry point.
Entry points are ready-to-execute programs contained in a package. If it has entry points, the pip will create an executable script for each one, which allows you to run the program from the shell conveniently.
You should pay attention to the path to the directory where the pip puts such scripts. For example, it is ~/.local/bin
in Linux. We should add this path to PATH
.
We check the contents of PATH
. If we gave the path correctly, the script for cowsay should work like this:
Entry points are executable files — Python modules we can run as a program. We will look at how to create them later. The pip calls python3 -m module_name
and creates scripts. In the same way, we can run the cowsay script we installed:
Updating the pip
As you may have already guessed, pip itself is also the entry point of the eponymous pip package, so we run it with the command python3 -m pip
.
You also need to update the pip periodically. You can install a fresh pip in the user environment with this command:
python3 -m pip install --user --upgrade pip
The --upgrade
flag will update an already installed package if it can find a newer version in the index.
Recommended materials
Are there any more questions? Ask them in the Discussion section.
The Hexlet support team or other students will answer you.
For full access to the course you need a professional subscription.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.