One of the reasons that Python is so valuable is that there are several packages that we can install that extend the capabilities of Python. For example, if we want MATLAB-like functionality matrices numerical analysis, you can use numpy, optimizers, and differential equation solvers. Further, several other packages like matplotlib help us plotting, while Pygame helps develop a graphical user interface and build diverse games. xlwings, on the other hand, allows us to interface with excel. In addition, we have others like open CV, computer vision, etc.
Python modules allow you to incorporate other people’s code into your own. As a result, you won’t have to reinvent the wheel every time, saving you a lot of time during development. Python has thousands of modules that can help you save time when programming. In addition, Python modules can be installed in two ways: system-wide and in a virtual environment.
A module aids in the logical organization of Python code. When code is separated into modules, it is easier to comprehend and use. In addition, a module, which is a Python object with freely named characteristics, can be bound and referenced.
A module is nothing more than a Python code file. In a module, you can describe functions, groups, and variables. A module can also include executable code.
Multiple programs can import a module for use in their application; therefore, a single code can be utilized by various applications to complete their tasks faster and more reliably.
Introduction to modules
There are numerous code packages or code modules available in Python. You don’t have to reimplement existing code when using a module, and you can use code written by others.
It simplifies development by allowing you to save time on simple operations like scraping a webpage or reading a CSV file.
When you look for a solution to an issue, you’ll frequently encounter modules that you’ve never seen before or that aren’t installed on your computer. You can use these modules after installing them on your machine.
Importing modules at the start of your code allows you to load them. As an example,
import csv import requests
Wheels vs. Source Distributions
pip can install from Source Distributions (sdist) or Wheels, and however, if both are available on PyPI, pip will choose the most compatible wheel. You can modify pip’s default behavior using the –no-binary option, for example.
Wheels are a pre-built distribution type that allows for speedier installation compared to Source Distributions (sdist), especially when a project incorporates compiled extensions.
Instead of rewriting the source distribution in the future, if pip cannot locate a wheel to install, it will construct one locally and cache it for future installs.
With pip, you can install modules and packages. Open a terminal and type pip to install a module system-wide.
The module will be installed if you type the code below.
sudo pip install module-name
In addition, you can choose to install to a specific user account -mostly the active user. Achieving these isolated packages for the given user will require you to run the following command.
python3 -m pip install --user module-name
It will automatically install a Python module. In most cases, you’ll use a virtual environment, or venv, rather than installing modules system-wide. On the other hand, you can run the following command on windows.
py -m pip install --user module-name
You must have a pip installed for this to work. The installation procedure is dependent on the platform you’re using.
Installing pip
On Windows, you can install Python modules. First, check whether pip is installed: To see if pip is installed, use the command prompt in Windows to perform the following command:
pip --version
Note: If the pip isn’t already installed, then you need to install it first. To install the pip package, run the following command: pip install.
pip install
If the output version is not equal to or greater than 19, execute the following command to update pip:
pip install --upgrade pip wheel
Use the following command to install packages from other resources:
pip install -e git+<a href="https://github.com/myrepo.git#egg=packagename">https://github.com/myrepo.git#egg=packagename</a>
Use the following command to upgrade the packages that are already installed:
pip install --upgrade
Use the following command to uninstall a package that has already been installed:
pip uninstall
Installing Python modules on Unix/macOS
Make sure you’ve got pip installed. In your terminal, use the following command to see if pip is installed.
python3 -m pip –version
While pip alone is adequate for installing from pre-built binary files, you should also have up-to-date versions of the setuptools and wheel projects to ensure that you can install from source archives.
Use the following command to update the installed pip and setup tools copies:
python -m pip install --upgrade pip setuptools wheel
Or run the following command if you are on Windows.
py -m pip install --upgrade pip setuptools wheel
To install the module with pip, use the command below.
python3 -m pip install "model-name"
Use the following command to install the module’s specific version:
python3 -m pip install "model-name==2.2"
To install a module’s version between any two numbers, run the following:
python3 -m pip install "model-name>=2,<3"
To install a specific full-length version that is compatible with your computer, run the following command:
python3 -m pip install "model-name ~=2.2.3"
Use the following command to upgrade the project version:
python3 -m pip install --upgrade model-name
To install a required module that is in the text document:
python3 -m pip install -r requirements.txt
To install the directories that are present in the local system using the following command:
python3 -m pip install --no-index --find-links=file:///local/dir/ ProjectName python3 -m pip install --no-index --find-links=/local/dir/ ProjectName python3 -m pip install --no-index --find-links=relative/dir/ProjectName
Installation of Python packages manually
The vast majority of Python packages now supports pip. If your kit isn’t compatible, you’ll have to install it manually.
Before installing any package, make sure you have a Python installation that includes all of the essential files for installing packages by reading the Installation Requirements.
Download the kit and extract it to a local directory to install it. If the kit comes with its own set of installation instructions, follow them; if the package isn’t there, run the following command to install the package manually:
python .py install
Using Setup.Py to Install Python Packages
Open a command or terminal window and type: setup.py to install a package that includes a setup.py file. First, cd to the setup.py directory in the root directory.
python setup.py install
Setup.Py Build Environment
Packages installed with setup.py have build requirements that developers must follow. Some prerequisites, however, are optional.
Make sure you have the most recent version of setuptools installed:
python -m pip install --upgrade setuptools
Setup should include the install requires keyword arguments.
install_requires=[''], # Optional keyword
The setuptools setup.py keyword install_requires is used to define minimal package needs. Consider the following example:
install_requires=[''], # Optional keyword
For a setup, complete the package build prerequisites. PyPA (Python Packaging Authority) outlines py-based installation in their ‘Sample Project.’
Sample Project is a template package that includes a setup.py file for manual package installation. For tweaking the script and the whole package build environment, the file is annotated using comments. The sample project can be found at [https://github.com/pypa/sampleproject ].
The setuptools package is used in the Sample Project: “A setuptools based setup module.” Setup.py [ https://github.com/pypa/sampleproject/blob/master/setup.py
setup.py is the build script for setuptools-based packages.
This tutorial will walk you through the process of downloading and installing Python modules. There are various ways to install external modules, but for the sake of this course, we’ll use pip, which is available for both Mac/Linux and Windows. Pip is installed by default in Python 3.8 and newer. However, anyone using an older version of Python will equally benefit from this tutorial because the processes are still quite common.
Modules Introduction
One of the best things about Python is the abundance of excellent code libraries that are publicly and freely available. These libraries can save you a lot of time coding or make a task (such as creating a CSV file or scraping a webpage) much more manageable.
When searching for solutions to problems, you’ll frequently come across sample code that employs code libraries you’ve never heard of. Don’t be scared off by these! You can use these libraries after they’ve been installed on your computer by importing them at the start of your code; you can import as many libraries as you like, for example,
import csv import requests import kmlwriter import pprint
It can be intimidating for new Python users to download and install external modules for the first time. There are a variety of methods for installing Python modules, which adds to the complication. This article covers one of the simplest and most used methods.
The idea is to get the software on your computer that automatically downloads and installs Python modules.
We’ll use a tool called pip for this.
Note that starting with Python 3.9, pip will be included in the standard installation. There are many reasons why you may not yet have this version, and if you don’t, following instructions should assist you.
Instructions for Mac and Linux mac-and-Linux-instructions
We can obtain a python script to install pip for us, according to the pip documentation. Furthermore, we can install pip via the command line on a Mac or Linux using the curl command, which downloads the pip installation Perl script.
curl -O https://bootstrap.pypa.io/get-pip.py
You must run the get-pip.py file with the Python interpreter after downloading it. However, if you use Python to run the script, it will fail.
python get-pip.py
The script will very certainly fail because it lacks the necessary rights to update specific folders on your filesystem, which are specified by default to prevent random programs from changing vital files and infecting you with viruses. You can use the sudo command in front of the python command in this case—and in all circumstances where you need to allow a script that you trust to write to your system folders—like this.
sudo python get-pip.py
Instructions for Windows
Like with the other platforms, the quickest approach to installing pip is using the get-pip.py python application, which you can obtain here. You might be afraid of the vast mess of code that awaits you when you open this link. Please don’t be that way. Save this page with the default name of get-pip.py in your browser.
It’s a good idea to save this file in your python directory so you know where to look for it later.
After you’ve saved the file, you’ll need to run it in one of two methods. If you prefer to use your own Python interpreter, right-click on the file get-pip.py and select “open with,” then choose your own Python interpreter.
If you prefer to install pip via the command line on Windows, go to the directory where you saved Python and get-pip.py. We’ll suppose this directory is called python27 in this example, so we’ll type C:>cd python27. To install pip, navigate to this directory and run the command.
python get-pip.py
Installing-python modules in Python
It’s simple to install python modules now that you have pip because it handles all the work for you. When you identify a module you wish to use, the documentation or installation instructions will usually contain the pip command you’ll need, such as
pip install requests pip install beautifulsoup4 pip install simplekml
Remember that you may need to execute pip with sudo for the same reasons as above (on Mac or Linux systems, but not Windows).
sudo pip install requests
You may find it helpful to use the -m flag to assist Python in finding the pip module, especially on Windows.
python -m pip install XXX
Installing Python modules in a virtual environment
We can create a different virtual environment from the operating system. It enables you to use the same Python modules as your other engineers in the team.
Use the following command to create a virtual environment:
virtualenv codevirtualenv
After opening codevirtualenv, you will find three directories now: bin, include, and lib.
To turn on the virtual environment, run the following command.
source codevirtualenv/bin/activate
Then we can install any module, in any version, without having to worry about the operating system. We’ll be able to use the same version of modules as other developers this way.
pip will only install for this environment.
pip install
Or you can choose to install a specific version of the given Python module by running the following command.
python -m pip install model-name==2.0.5
Alternatively, upgrade the module by running the following command,
python -m pip install --upgrade model-name
When you are finally ready to exit the virtual environment write, run the following command:
deactivate
When you use the —user option with Python -m pip install, a package will be installed only for the current user, not for all users on the system.
Install Python packages for scientific purposes
A lot of scientific Python packages have complicated binary dependencies and are currently challenging to install through pip. It will often be more accessible for users to install these packages using other means than attempting to do so with pip at this time.
Working with many Python versions installed at the same time
Use the versioned Python commands with the -m flag to run the appropriate copy of pip on Linux, Mac OS X, and other POSIX systems. Below are examples of how you can go about this.
python2 -m pip install SomePackage # default Python 2 python2.7 -m pip install SomePackage # specifically Python 2.7 python3 -m pip install SomePackage # default Python 3 python3.9 -m pip install SomePackage # specifically Python 3.9
Use the py Python launcher on Windows in conjunction with the -m switch as follows.
py -2 -m pip install SomePackage # default Python 2 py -2.7 -m pip install SomePackage # specifically Python 2.7 py -3 -m pip install SomePackage # default Python 3 py -3.9 -m pip install SomePackage # specifically Python 3.9
Pip commands with the appropriate version numbers may also be available.
Additional approaches for Installing modules in Python
Using the requirements files
When you have a list of modules in a requirements file, for instance, requirements.txt, Python has a way of installing such a list of requirements. Run the following command to achieve this,
in Linux or macOS
python3 -m pip install -r requirements.txt
in Windows
py -m pip install -r requirements.txt
VCS Installation
It is possible to do a module installation from VCS. Mostly, the latter is in an editable form. Below are the variations for installations on Unix and windows
in Unix/ macOS
python3 -m pip install -e git+https://git.repo/some_pkg.git#egg=SomeProject # from git python3 -m pip install -e hg+https://hg.repo/some_pkg#egg=SomeProject # from mercurial python3 -m pip install -e svn+svn://svn.repo/some_pkg/trunk/#egg=SomeProject # from svn python3 -m pip install -e git+https://git.repo/some_pkg.git@feature#egg=SomeProject # from a branch
in Windows
py -m pip install -e git+https://git.repo/some_pkg.git#egg=SomeProject # from git py -m pip install -e hg+https://hg.repo/some_pkg#egg=SomeProject # from mercurial py -m pip install -e svn+svn://svn.repo/some_pkg/trunk/#egg=SomeProject # from svn py -m pip install -e git+https://git.repo/some_pkg.git@feature#egg=SomeProject # from a branch
Using other indexes for installation
An alternate index can come in handy when installing modules. You are in a position to search for an additional index while installing besides using PyPI. An example of index installation is as follows.
In Unix/macOS
python3 -m pip install --index-url http://my.package.repo/simple/ model-name
In Windows
py -m pip install --index-url http://my.package.repo/simple/ model-name
Using a local src tree for installation
Installing from local src in development mode, i.e., the project seems to be installed but may still be edited from the src tree.
In Unix/macOS
python3 -m pip install -e
In Windows
py -m pip install -e
Additionally, you can install from the src as follows
Installing from src in Unix/macOS
python3 -m pip install
Installing from src in Windows
py -m pip install
Local archives Installation
You can additionally install a given source archive file as follows in Unix and Windows Operating Systems.
In Unix/ macOS
python3 -m pip install ./downloads/model-name-1.0.4.tar.gz
In Windows
py -m pip install ./downloads/model-name-1.0.4.tar.gz
Further, it is possible to do a local directory installation with archives without necessarily checking the PyPI.
In Unix/macOS
python3 -m pip install --no-index --find-links=file:///local/dir/ model-name python3 -m pip install --no-index --find-links=/local/dir/ model-name python3 -m pip install --no-index --find-links=relative/dir/ model-name
In Windows
py -m pip install --no-index --find-links=file:///local/dir/ model-name py -m pip install --no-index --find-links=/local/dir/ model-name py -m pip install --no-index --find-links=relative/dir/ model-name
Installing from a different location
Create a helper application that delivers the data in a PEP 503 compliant index format. Use the –extra-index-url flag to direct pip to use that index when installing other data sources (for example, Amazon S3 storage).
./s3helper --port=4488 python -m pip install --extra-index-url http://localhost:4488 model-name
Prereleases Installation
Along with stable versions, you’ll find pre-release and development versions. Pip searches for stable versions by default.
In Unix/macOS
python3 -m pip install --pre model-name
In Windows
py -m pip install --pre model-name
Setuptools “Extras” Installation
At this point, the Setuptools extras can be installed.
In Unix /macOS
python3 -m pip install SomePackage[PDF] python3 -m pip install SomePackage[PDF]==3.0 python3 -m pip install -e .[PDF] # editable project in current directory
In Windows
py -m pip install SomePackage[PDF] py -m pip install SomePackage[PDF]==3.0 py -m pip install -e .[PDF] # editable project in the current directory
Example: Installing matloplib using pip
First, open the terminal if you are on Unix or macOS and run the following command.
python
Then try to import maplotlib. If the process is successful, then it is already installed. However, if errors are importing, then it is probably not there and requires re-installation.
import matplotlib
Installing matplotlib
To uninstall matplotlib, run the following command on the terminal as shown below.
pip install matplotlib
Finally, if you choose to uninstall matplotlib, you can efficiently run the following command on the terminal or the command-line interface.
pip uninstall matplotlib
Conclusion
Python is a major open-source development project with a vibrant community of contributors and users that make their products available to other Python developers under open source license terms.
It enables Python users to efficiently exchange and interact, taking advantage of the solutions that others have already generated for common (and sometimes even uncommon!) problems and maybe introducing their solutions to the pool of answers.