Skip to main content

Installing Python Modules

Note☆ Learning Objectives
  • Install matplotlib to create python plots
  • Install numpy for scientific computing

In this course, we have used a variety of built-in modules like math, turtle, datetime.

We are not limited to these built-in modules: developers from all over the world can create new modules that serve particular needs.

In the final chapter of our course, we will make extensive use of the following external libraries, popular for scientific research:

For these modules, extra steps are needed: they are not built-in to Python, so we have to install them separately.

Importing and installing a module

  1. Ensure that the PyCharm project is using the System’s interpreter

  2. Add a python file to write your code.

  3. The import statement followed by the module name, allows you to import a module into your script. For example:

    import math   #comes built-in with python

    Note: You can use an alias to “nickname” a package and shorten your lines of code by using the as keyword For example:

    import math as m
    
    print(m.pi)

    Note: You can also import a function or a sub-module within a given module

    from math import sin
    
    print(sin(0))
  4. If you are importing a module that wasn’t installed, it will be underlined in red:

    import numpy as np
    from matplotlib import pyplot as plt

wow
  1. If you see an error saying “Unresolved reference”, that means the module is not installed on the computer.

Installing vis Thonny

  1. In thonny, type the following command in the shell:
!pip install matplotlib

wow 2. Wait until a success message appears in the shell:

Successfully installed ...(a bunch of module names)
  1. You should be able to use the module! Example
from matplotlib import pyplot as plt 

plt.plot([1,2,3], [1,2,3])
plt.show()

Installing via PyCharm

  1. Click File > Settings to open the Settings menu:

wow
  1. Select Project > Project interpreter from the left hand side menu

  2. This is all the list of modules that are installed in my project, yours might not contain much for now.

  3. To add a package/module, click the + button at the top of the list

  4. Search for the package you want installed: “matlplotlib” then click Install Package

wow
  1. While you are here, install numpy as well:

wow
  1. Now that the packages are installed, try typing those lines in your python file:

    import numpy as np
    from matlplotlib import pyplot

Installing via command line on Windows (like a pro 😎)

  1. Type cmd in the Windows search bar and click the “Command Prompt” icon.

  2. Type the following command in the command prompt:

pip install --upgrade pip
  1. Install your packages:
pip install matplotlib
pip install numpy

Installing via Terminal on Mac (like a pro 😎)

  1. From Launch page, search for an app called Terminal
  2. Type the following command in Terminal:
pip3 install --upgrade pip
  1. Install your packages with the following commands:
pip3 install matplotlib
pip3 install numpy