Python modules allow you to use code of others in your code. This saves you a lot of development time, you don’t have to reinvent the wheel each time.

There are thousands of modules available for Python, which save you time when developing. There are two ways to install Python modules: system wide and using a virtual environment.

Related Course: Complete Python Programming Course & Exercises

Introduction modules

In Python you have amny code packages or code modules that are available. By using a module you don’t have to implement existing code again and you can use code made by other people.

This makes developing much easier, as you can save time with basic tasks like scraping a webpage or reading a CSV file.

If you search for a problem, you often find modules that you haven’t seen before or that simply aren’t available on your computer. You can install these modules on your computer and then use them.

You can load modules by importing them at the beginning of your code. For instance

import csv
import requests

Install module

You can install modules or packages with the Python package manager (pip). To install a module system wide, open a terminal and use the pip command. If you type the code below it will install the module.

sudo pip install module-name

That will install a Python module automatically. Generally you do not install modules system wide, but use a virtual environment or venv.

In order for this to work, you need to have pip installed. The installation process depends on your platform.

pip on Linux

You can get the pip package manager by following two commands. It will download an installation script with curl and then install it using Python.

curl -O https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py

If you use Ubuntu or Debian Linux you may also be able use the systems package manager:

sudo apt install python3-pip
sudo apt install python-pip

pip on Windows

Make sure you have Python 3.4 or newer, because it ships with pip. If pip is not available and you use Python > 3.4+, just run the command:

py -3 -m ensurepip

You can check your version with the command

python --version

pip on macOS

To install pip on Apple Mac OS X, open up a terminal.
Then download the get-pip.py installation file with curl

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

You can then run the installation script:

python get-pip.py

That’s it, pip is now installed.

older versions of Python

For older versions of Python, it’s recommended that you upgrade instead of work with a legacy version of Python. If you insist, download the install script and place it in your Python directory. Then run it with

python get-pip.py

To do this, you need an administrator command prompt.

Virtualenv

We can create a virtual environemnt, that is seperate from the operating system. This allows you to use the same Python modules as other developers in your team.

Create a virtual environment with the line:

virtualenv foo

Then open /foo/

cd /foo/

You now have 3 directories: bin, include and lib.
Move up a directory.
To activate the virtual environment type:

source foo/bin/activate

we can then install any module, any version we want - without affecting the operating system. This way we can have the same version of modules as other developers.

Note: Pip will now install for this environment only.
pip install <your-module>

To exit the virtual environment write:

deactivate

Download exercises