In this article, we will be discussing what a python package is, what requirements are 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 defined as a collection of modules located within a structured directory that provides a mechanism to run a piece of software. In this context, we are referencing a python package as a type of “distribution” or a bundle of software that can be installed on a system. Additionally, we must differentiate between a module and a package. A module is a single file that is imported into python, as opposed to a group of modules bundled into a program. There also exists a type of package where the python source code is referenced and included in an entity. This is not the type of package we are referring to here.
When installing a package, we must first ensure that Python is installed on the system. We can verify this by running the following command in a terminal.
root@host [~]# python --version
Pip is the default package manager for python. It is used to install and manage python software from the command line.
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
PyPi or the “Python Package Index” is a software repository that contains applications used by the python programming language. PyPi supports installing software from distribution files, local projects, and can utilize version control features. It also uses “requirement specifiers” to better define specific versions, variants, and also supports the url_req-form specs. Currently, PyPI houses more than 200,000 python projects. Install options include:
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 is a stable and fully featured python library intended to assist with the packaging of Python projects.
Wheel is a Python library used as an extension of setuptools intended for working with wheel files. To install these two libraries, we need to run the following command.
root@host [~]# python -m pip install --upgrade pip setuptools wheel
root@host [~]# python3 -m venv root@host [~]# source /bin/activate
Python has multiple methods and options available for installing software. Packages can be installed via the following methods:
- From the PyPI repository:
root@host:~# pip install package1
- From VCS: (Version Control System)
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
In a Virtual Environment
As a quick overview, installing Python packages can be accomplished using this quick three-step process.
Step 1. Create a virtual environment
python3 -m venv .myvenv
Step 2. Activate the virtual environment
Step 3. Install your package
python3 -m pip install package1
Using this method, we contain our installed package to a virtual environment that does not make any system-wide changes. Should we actually want to implement a Python package system-wide, we dispense with Steps 1 and 2.
root@host [~]# python3 -m venv .myvenv root@host [~]# source .myvenv/bin/activate
root@host [~]# virtualenv .myvenv root@host [~]# source .myvenv/bin/activate
Installing from PyPI
To install a package from PyPi, we run the following command.
root@host [~]# pip install "myproject"
To install a specific version, we run:
root@host [~]# pip install "myproject==1.4"
To install a package that is greater than or equal to one version and less than another, we can run:
root@host [~]# pip install "myproject>=1,<3"
To install a version that is “compatible” with a specific version, run:
root@host [~]# pip install "myproject~=1.2.3"
In this scenario, this indicates that Python will install any version “==1.4.*” or a version that’s also “>=1.4.2”.
To upgrade a package to the latest version from PyPI that is already installed, we run:
root@host [~]# pip install --upgrade "myproject"
That’s it! Setting up a Python package can be as simple as using a single command or as complex as needed to accomplish this task in a very precise manner. Python’s documentation is quite extensive and provides a wealth of insights and experience. Additionally, because the user community base is so large, excellent resources are available in multiple locations across the web.
Want To Join An Elite Group Of Experts?
Call Now To Find Out If You Are Eligible!
Liquid Web has some of the most cutting edge server setups available in the industry. We pride ourselves on being able to recommend a platinum solution for every situation. Not only that, our technicians continue to provide solid technical answers to all of your questions 24/7/365 year after year. Want to experience the best we have to offer?
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.