This is about PIP vs. PIP3 and how to use each.
- What PIP and PIP3 are
- The differences between PIP and PIP3
- When and how to use PIP and PIP3
- Lots more
So if you want to understand PIP and PIP3, then you are in the right place.
Let’s get started!
Table of Contents
- What Is the Difference Between PIP and PIP3?
- When and How to Use PIP and PIP3?
- Use a Virtual Environment Instead of the Global Interpreter
What Is the Difference Between PIP and PIP3?
Sooner or later, anyone who starts learning Python faces the need to install additional modules.
However, this may not happen quickly, as the standard Python library is very extensive and contains modules for such as:
- Files on disk
- Text data
- Data in CSV format
- Network protocols
Since version 2.7.9 for Python 2 and version 3.4 for Python 3, the standard distribution has included the PIP package manager.
The difference between PIP and PIP3 in short and sweet: PIP is a soft link for a particular installer.
Your system will use one of your Python versions depending on what exactly is first in the system PATH variable. When you run PIP3, you can be sure that the module will be installed in Python 3.
But to understand truly the difference between PIP and PIP3 you need to understand the big picture and see PIP and PIP3 in action—let’s dive right in:
When and How to Use PIP and PIP3?
You can find the packages to download and install in the PyPI (Python Package Index) repository.
Still, you can set up your repositories and install packages from them using the standard PIP tool.
Moreover, you can create your package, publish it on PyPI, and make it available for installation by all Python users.
Typically, in manuals and instructions, package installation is written as:
pip install package_name
pip install numpy
However, you may also see other options, such as pip3 install instead of pip install.
So how do you correctly install packages on your system and what is the difference between PIP and PIP3?
When you type pip in the console, the system looks for an executable file with that name in the current folder and then in the folders specified in the system PATH variable.
If you have multiple Python installations and all of them are in your PATH, you cannot be sure which directory will be searched first.
The Python installation directory has duplicate executables whose names contain the Python version.
For example, for the installed Python 3.9, these will be pip, pip3, and pip3.9. So, you can install the necessary modules for the desired version of Python.
Basically, if you type pip install package_name, you will get exactly the output you intended.
However, if you have Python 2 and Python 3 simultaneously, and you want to install or update some Python 2 module, you will absolutely need to specify the version. Therefore, you can write like this to install the numpy module for Python 2:
pip2 install numpy
You can check which file is referenced by the command pip show pip.
Below is an example of the output of this command on Windows 10 for pip and pip3:
C:\>pip show pip Name: pip Version: 20.2.4 Summary: The PyPA recommended tool for installing Python packages. Home-page: https://pip.pypa.io/ Author: The pip developers Author-email: [email protected] License: MIT Location: c:\program files\python38\lib\site-packages Requires: Required-by:
C:\>pip3 show pip Name: pip Version: 20.2.4 Summary: The PyPA recommended tool for installing Python packages. Home-page: https://pip.pypa.io/ Author: The pip developers Author-email: [email protected] License: MIT Location: c:\program files\python38\lib\site-packages Requires: Required-by:
You can also check on which package installer will run by using pip –version (again, let’s use each PIP and PIP3):
C:\>pip --version pip 20.2.4 from c:\program files\python38\lib\site-packages\pip (python 3.8)
C:\>pip3 --version pip 20.2.4 from c:\program files\python38\lib\site-packages\pip (python 3.8)
In this case, both PIP and PIP3 refer to the same path, but the paths may also be different.
However, using PIP vs. PIP3 gives you more control. You clearly understand for which version of Python you are installing or updating the module.
But what if your system has more than one version of Python 3? For example, you are running Python 3.7, but have installed an additional fresh Python 3.9 to see new features or play around with it. You can still use:
pip3.9 install package_name
However, for maximum control, you can run the installation via:
python -m pip install package_name
This way, you can write the full path to the Python executable and ensure that the package is installed for this particular version.
Use a Virtual Environment Instead of the Global Interpreter
You have learned about installing Python packages into the global interpreter, but doing this often is not recommended.
It is best to master the tools for creating a virtual environment as soon as possible:
In your practice, a situation may arise in which different applications require the same library, but, for example, different versions.
It is tough to resolve this issue without using virtual environments.
Also, there may be a situation in which you must freeze a specific state of the development environment.
One can fix the application’s ability to work on the current versions of the libraries and prohibit their change. It is recommended to not put packages in the global interpreter at all.
Setting up a virtual environment is easy.
Let’s consider creating a virtual environment and installing packages using venv:
python -m venv my_env1
You create a virtual environment named my_env1. The folder my_env1 will be created in the current directory.
Meanwhile, in the my_env1\Scripts directory, two files—activate and deactivate—will be created.
C:\VE\my_env1> scripts\activate (my_env1) C:\VE\my_env1>
After the virtual environment is activated at the beginning of the command line, this environment’s name will appear in brackets.
Now you can install any package in this virtual environment. If you have a requirements.txt file for the program, you should run:
pip install -r requirements.txt
In this case, all package versions specified in requirements.txt will be installed, and you can be sure that the program will work.
In this example, you will install the numpy package in a virtual environment.
Here, you can completely and fearlessly use pip install without specifying a version. The virtual environment is created from a specific version of Python, and this version will be used further:
(my_env1) C:\VE\my_env1>pip install numpy Collecting numpy Using cached https://files.pythonhosted.org/packages/40/db/5060f18b0116f00ee73f8365efc9c95bd5496946290b0e7c97b6ee89dffe/numpy-1.19.4-cp38-cp38-win_amd64.whl Installing collected packages: numpy Successfully installed numpy-1.19.4
The module is installed in the my_env1\Lib\site-packages folder. Now you can use this module in your virtual environment.
Let’s run python shell, import numpy, and print the pi value from this module:
(my_env1) C:\VE\my_env1\Lib> python Python 3.8.2 (tags / v3.8.2: 7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import numpy >>> numpy.pi 3.141592653589793
To deactivate the virtual environment, you must run the deactivate script.
You may not even specify the exact path, as the virtual environment activation script registers the folder of the current virtual environment in the system PATH variable in the first place.