Reading Time: 4 minutes

Python is a general-purpose programming language designed for various uses. For example, websites, industrial robotics, and even games all use the same core technology.

Python’s syntax is easy to learn, focusing on readability and reducing the maintenance of your created programs. In addition, it supports packages and modules, promoting modular programming and the reuse of code.

In this article, we will be discussing how to install a Python package. We will discuss Python packages, the requirements needed to install a software title, and the many other options available when setting up a new Python program.

What is a Python Package?

A Python package is a collection of modules located within a structured directory that provides a mechanism to run a piece of software. In this context, we reference a Python package as a type of distribution or a bundle of installed software on a system.

Additionally, we must differentiate between a module and a package. A module is a single file imported into Python instead of groups of modules bundled into a program. A type of package also exists where the Python source code is referenced and included in an entity. This is not the type of package we are referring to here.

Requirements

Python

Before installing packages in Python, we must first ensure the installation of Python on the system. We can verify this by running the following command in a terminal.

root@host [~]# python --version

Pip

Pip is the default package manager for Python, used for installing and managing Python software from the command line. You can check the version you have installed using the following command.

root@host [~]# python -m pip --version
pip 19.3.1 from /usr/lib/python2.7/site-packages/pip (python 2.7)

root@host [~]# python -m ensurepip --default-pip

Python Package Index (PyPi)

Python Package Index (PyPi) is a software repository that contains applications used by the Python programming language. PyPi supports installing software from distribution files and local projects. It can also utilize version control features.

In addition, PyPi also uses requirement specifiers to better define specific versions and variants and supports the url_req form specs. Currently, PyPI houses more than 200,000 Python projects.

Install options for PyPi include the following commands.

root@host [~]# pip install software
root@host [~]# pip install software == 1.5
root@host [~]# pip install software >=1.3,<3.0 
root@host [~]# pip install software[foo, bar] 
root@host [~]# pip install software ~=1.4.5  

Setuptools

Setuptools is a stable and fully featured Python library intended to assist with the packaging of Python projects.

Wheel

Wheel is a Python library used as an extension of setuptools intended for working with wheels or .whl files.

To install the wheels and setuptools Python libraries, we need to run the following command.

root@host [~]# python -m pip install --upgrade pip setuptools wheel 

Venv (Optional)

Venv is a Python module used to build and manage isolated, lightweight virtual environments to use differing Python versions and modules.

root@host [~]# python3 -m venv
root@host [~]# source /bin/activate

Install Mediums

Python has multiple methods and options available for installing software. For example, you can install packages for Python via the following methods.

  • From the PyPI repository:
 root@host:~# pip install package1
  • From Version Control System (VCS):
root@host:~# pip install -e git+https://git.repo/package1
  • From other Indexes: (sources other than PyPi):
 root@host:~# pip install --index-url http://git.repo/package1
  • From a local src tree:
root@host:~# pip install -e /path/package1
  • From a local archive (/mydrive/downloads/project.1.2.3):
root@host:~# pip install /path/package1
  • From other sources (e.g., Amazon S3):
root@host:~# /s3helper --port=9999
root@host:~# pip install --extra-index-url http://localhost:9999
  • From Pre-releases (when installing a beta version, Python defaults to the stable version):
root@host:~# pip install --pre package1

Python Packages in a Virtual Environment

As a quick overview, installing Python packages can be accomplished using this three-step process.

Step 1. Create a virtual environment.

python3 -m venv .myvenv

Step 2. Activate the virtual environment.

source .myvenv/bin/activate

Step 3. Install your Python packages.

python3 -m pip install package1

Using this method, we contain our installed packages for Python in a virtual environment that does not make any system-wide changes. If you want to implement a Python package system-wide, skip Steps 1 and 2.

Using venv or virtualenv

You used the venv command to create the virtual environment in the steps above, as seen below.

root@host [~]# python3 -m venv .myvenv
root@host [~]# source .myvenv/bin/activate

As an alternative, you can use virtualenv to accomplish the same task.

root@host [~]# virtualenv .myvenv
root@host [~]# source .myvenv/bin/activate

Installing from PyPi

To install a package from PyPi, you run the following command.

root@host [~]# pip install "myproject"

To install a specific version, run the following command.

root@host [~]# pip install "myproject==1.4"

You can run this command to install a package greater than or equal to one version and less than another.

root@host [~]# pip install "myproject>=1,<3"

To install a version that is compatible with a specific version, run this command.

root@host [~]# pip install "myproject~=1.2.3" 

Upgrading packages

To upgrade a package to the latest installed version from PyPi, run the following command.

root@host [~]# pip install --upgrade "myproject"

Conclusion

We have gone through how to install a Python package. Setting up a Python package can be as simple as using a single command. It can also be complex, needing to accomplish tasks in a precise manner.

Python’s documentation is quite extensive and provides a wealth of insights and experience. Excellent resources are available in multiple locations across the web because of the large user community base.

Get the Right Hosting for Your Python Projects

Liquid Web has some of the most cutting-edge server setups available in the industry. We pride ourselves on being able to recommend the best solution for every situation. Contact us to speak with one of our knowledgeable hosting advisors to get the best-fit hosting solution today!

kb-banner-lw-hosting
Avatar for Ronald Caldwell

About the Author: Ronald Caldwell

Ron is a Technical Writer at Liquid Web working with the Marketing team. He has 9+ years of experience in Technology. He obtained an Associate of Science in Computer Science from Prairie State College in 2015. He is happily married to his high school sweetheart and lives in Michigan with her and their children.

Latest Articles

Blocking IP or whitelisting IP addresses with UFW

Read Article

CentOS Linux 7 end of life migrations

Read Article

Use ChatGPT to diagnose and resolve server issues

Read Article

What is SDDC VMware?

Read Article

Best authentication practices for email senders

Read Article