Python has by far one of the largest programming communities on Earth, and the rise of AI, Machine Learning, and Deep learning makes it broader and broader. Moreover, the standard packaging tools are all built to be used from the command line. But before that, let’s check the installed Python version.
python --version Python 3.8.2
At the time of this article, the latest version of Python is 3.8, and I have that version.
Yours might be different depending on the time of reading this post. So I have Python3.
Now, if you are using Python 2.x, then you can use the following command to install the packages.
How to install Python module using pip
To install a Python module using pip, use the python -m pip install packagename command in your terminal. The command will install the latest version of the module and its dependencies from the Python Packaging Index.
python -m pip install packagename
I am using Python3, so I can use the following command to install Python modules.
python3 -m pip install packagename
For Windows users, the examples in this tutorial assume that the option to adjust the system PATH environment variable was selected while installing Python.
Install specific Python 3 module versions
You can specify the exact or minimum version directly on the command line.
When using the comparison operators such as >, < or some other special character that gets interpreted by the shell, the package name and the version should be enclosed within double-quotes.
See the following code.
python -m pip install packagename==1.0.4 # specific version python -m pip install "packagename>=1.0.4" # minimum version # For Python3 python3 -m pip install packagename==1.0.4 # specific version python3 -m pip install "packagename>=1.0.4" # minimum version
Typically, if the suitable module is already installed, attempting to install it again will not affect it.
I have already installed pip on my system, but let’s try to install it again and see what we are getting in the command line.
python3 -m ensurepip --default-pip
python3 -m ensurepip --default-pip Looking in links: /var/folders/7m/122nrwsx5451db3nvfh157zh0000gn/T/tmpthkp5ef7 Requirement already satisfied: setuptools in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (41.2.0) Requirement already satisfied: pip in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (20.1)
You can see that, and We are getting messages like Requirement already satisfied.
Upgrading existing modules must be requested explicitly.
We can explicitly request that we have to upgrade any particular module. You can check out a tutorial on how to upgrade pip in Python in this blog.
To upgrade any package in Python, you can use the following syntax.
python -m pip install --upgrade packagename # For Python3 python3 -m pip install --upgrade packagename
How to work with multiple versions of Python installed in parallel?
On Linux, macOSX, and other POSIX systems, use the versioned Python commands combined with a -m switch to run the appropriate copy of pip.
python2 -m pip install packagename # default Python 2 python2.7 -m pip install packagename # specifically Python 2.7 python3 -m pip install packagename # default Python 3 python3.4 -m pip install packagename # specifically Python 3.4 python3.8 -m pip install packagename # specifically Python 3.8
In the first section of this article, we have used default Python3 because right now, only one Python 3.x is installed on my machine, which is Python 3.8.
Appropriately versioned pip commands may also be available.
On Windows, use a py Python launcher with a –m switch.
py -2 -m pip install packagename # default Python 2 py -2.7 -m pip install packagename # specifically Python 2.7 py -3 -m pip install packagename # default Python 3 py -3.4 -m pip install packagename # specifically Python 3.4 py -3.8 -m pip install packagename # specifically Python 3.8
Different ways to install and manage Python modules
Pip is a preferred installer program. Starting with Python 3.4, it is included out of the box with the Python binary installers.
The virtual environment is a semi-isolated Python environment that allows packages to be installed for use by the specific application rather than being installed system-wide.
Python venv is a standard tool for creating virtual environments in your system and has been part of Python since Python 3.3.
Starting with Python 3.4, it defaults to installing pip into all the created virtual environments.
Python virtualenv is a third-party alternative (and predecessor) to venv. The virtualenv allows virtual environments to be used on the versions of Python before 3.4, which either don’t provide venv at all or aren’t able to install pip into created environments automatically.
Install Packages and Modules using pip
If you don’t know what your installed pip’s version is, you can fire the following command to check the version of pip.
pip --version pip 20.1 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)
Now that you have pip, it is easy to install python modules since it does all the work for you. When you find the module you want to use, the documentation or installation instructions will usually include the necessary pip command, such as the following.
See the syntax of the pip command.
pip install package name
Now, let’s install some Python modules using pip.
pip install requests pip install beautifulsoup4
For Mac and Linux, you might need to run pip with sudo, like the following.
sudo pip install requests
Uninstall a Package using PIP in Python
To uninstall a package using pip, type the following command in the command line or terminal.
pip uninstall packagename
In our example, let’s can uninstall the beautifulsoup4 package.
pip uninstall beautifulsoup4
Bonus: Python Package Index
The Python Package Index (PyPI) and the Cheese Shop are the official third-party software repository for Python.
PyPI lets us submit any number of versions of your distribution to the index.
If you alter the metadata for the specific version, you can submit it again, and the index will be updated.
PyPI holds the record for each (name, version) combination submitted.
As end-users, we can search for packages by keywords or filters against their metadata, thus behaving as the index. The PyPI has over 113,000 Python packages that can be accessed through PyPI.
This post shows how to install and manage the packages with different versions of Python.
We have seen the main tools that can help us to install and manage our project modules and don’t overlap with each other, and then we have seen how to install Python modules using pip, and at last, we have seen how to uninstall the Python modules.
That’s it for this tutorial.
1 thought on “How to Install Python Module using Pip”
I personally prefer using pipenv. It’s a little more helpful. For instance it tells you which package got installed as a result of it being a dependency of another package.
So when you want to uninstall a package. pip would only uninstall that package.. Using pipenv you can get rid of the extra packages that were installed (cyclical dependencies) as well.