Guide to Install Modules in Python

Python is a popular open-source development project, with a large active supporting community of contributors and users, which makes their software available for other Python developers to use under open source license terms.

This allows Python users to share and collaborate, which benefits from the solutions others have already created for common as well as rare problems.

Python has a large number of libraries that, you might want to install, update or delete at some time. This guide covers the installation of the Python modules using pip and virtual environment.

Installing via PIP

pip is the package installer for Python. it is the preferred installer program. Starting with Python 3.4, it is included by default with the Python binary installers. Python comes with an inbuilt package management system pip. you can install, update or delete any official package using pip.

Virtual Environment

You can install a particular package in a virtual environment. It is a semi-isolated Python environment that allows packages to be installed for use by a particular application, rather than being installed system-wide.

To create a virtual environment you can use the standard tool venv from Python 3.4, pip is automatically installed into all created virtual environments.

However, if you are using Python's earlier versions to 3.4, you have to use virtualenv to create a virtual environment, which is a third-party alternative to venv. which does not install pip automatically into the created virtual environment.

Python Package Index (PyPI)

The Python Package Index is a repository of software for the Python programming language. It helps you find and install software developed and shared by the Python community. Packages authors use PyPI to distribute their software.

You can install packages via the command line, the following command will install the latest version of a module and its dependencies from the Python Package Index.

python -m pip install ModuleName

How to install a module with a specific version?

We can also specify an exact or minimum version directly on the command line using comparator operators such as >, < or some other special character which get interpreted by the shell, the package name and the version should be enclosed within double-quotes.

python -m pip install "SomePackage == 1.0.4"   # specific version
python -m pip install "SomePackage >= 1.0.4"   # minimum version

If a specific module is already installed, and if you try to install it again will have no effect.

How to install packages to the user site?

You can use the following command for installing modules for a particular user. It is recommended using a user install, sending the —user flag to pip. pip installs packages for the local user and does not write to the system directories.

python -m pip install --user SomePackages

## example for installing more than one package for user
python -m pip install --user numpy scipy matplotlib pandas

How to upgrade installed modules?

The following command is used to upgrade the existing modules.

python -m pip install --upgrade SomePackage

How to uninstall a package?

You can uninstall a particular package using the following command

python -m pip uninstall SomePackage

This is a quick guide that explains the installation, upgrades, and uninstallation of modules in brief. I hope this article helps you to understand the process of installing the modules. You can read about installing modules in more detail from python documentation here

Thank you for reading.

Did you find this article valuable?

Support Madhuri's blog by becoming a sponsor. Any amount is appreciated!