Using Libraries

Installing Third-Party Libraries


Learning Objectives

  • You can explain why third-party libraries need to be installed.
  • You can install pandas and NumPy with python -m pip.
  • You can verify that an installed package can be imported.
  • You can distinguish package names used for installation from module names used in code.

The modules in the previous chapters come with Python. You can import math, random, datetime, csv, and json after installing Python itself.

Pandas and NumPy are different. They are third-party packages, which means they are distributed separately from Python. Before your program can import them, they need to be installed into the Python environment you are using.

Open the study-log project originally created in Reading and Writing Files and most recently extended in Structuring Your Data. Use PyCharm’s built-in terminal. PyCharm created a small project environment when you made that project; installing from the built-in terminal puts the libraries alongside the Python interpreter that runs your study log.

The installation command for this part is:

Terminal window
python -m pip install pandas numpy

If the command you used when creating the study-log project was python3 or py, use that same command here. For example, on Windows you might use:

Terminal window
py -m pip install pandas numpy

Run the command in PyCharm’s built-in terminal for the study-log project. The -m pip part tells the selected Python interpreter to run the pip package installer. This form is preferred over plain pip install ... because it makes the connection to the Python interpreter explicit.

Why use python -m pip?

0 / 5 points

Why does python -m pip install pandas help prevent interpreter mismatches?

Verify the Installation

After installing, run a tiny program:

import numpy as np
import pandas as pd
print(np.array([1, 2, 3]).sum())
print(pd.DataFrame([{"subject": "math", "minutes": 45}]))

You do not need to understand NumPy arrays or pandas DataFrames yet. This is a smoke test: the goal is only to check that Python can import both packages and run one tiny operation from each.

If this runs, the packages are installed in the Python environment that ran the script.

If you see ModuleNotFoundError, slow down before reinstalling randomly. The most common cause is that the package was installed for one Python interpreter, but the script is being run by another. Use the same command style for both:

Terminal window
python -m pip install pandas numpy
python my_script.py

or, on Windows if needed:

Terminal window
py -m pip install pandas numpy
py my_script.py

You can also ask Python to print the exact interpreter it is using:

Terminal window
python -c "import sys; print(sys.executable)"

Run that command in the terminal where you installed the packages and compare it with the interpreter shown in PyCharm’s project settings. If you normally use python3 or py, substitute that command for python.

Package Names and Import Names

The name you install and the name you import are often the same:

Terminal window
python -m pip install pandas
import pandas as pd

But this is not guaranteed for every package in the Python world. Some packages install under one name and are imported under another. Treat installation commands and import statements as two related but separate facts.

For this course:

Install nameImport nameCommon alias
numpynumpynp
pandaspandaspd

Package and import names

0 / 5 points

Which pair is correct for this course?

One Interpreter at a Time

The most confusing installation bugs happen when a computer has more than one Python interpreter. One might be the system Python. One might have come from python.org. One might live inside an editor. One might be inside WSL on Windows.

The symptom looks like this:

Terminal window
python -m pip install pandas
py my_script.py

The package may have installed successfully for python, but the script ran with py. Or the reverse:

Terminal window
py -m pip install pandas
python my_script.py

The habit is simple: use the same command family for installation and running.

Terminal window
python -m pip install pandas numpy
python check_libraries.py

or:

Terminal window
py -m pip install pandas numpy
py check_libraries.py

This does not solve every environment problem, but it removes the most common one.

Where Packages Go

When you install a package, it goes into a Python environment. An environment is the collection of packages available to a particular Python interpreter.

This course keeps using the project environment PyCharm created for the study log. That environment is a virtual environment: a small, self-contained Python setup for this project. You do not need to create another one. Open the same project and run installation and program commands in its built-in terminal.

If installation fails

Try these checks in order:

  1. Run python --version or py --version and confirm Python works.
  2. Run python -m pip --version or py -m pip --version.
  3. Run python -c "import sys; print(sys.executable)" (or the equivalent python3/py command) and check that it names the study-log project environment.
  4. Use the same Python command for installation and running scripts.
  5. Copy the full error message and search for the first line that names the real problem.

Do not install packages from random websites. Use pip.

Build an import check file

0 / 5 points

Arrange the lines for a tiny check_libraries.py file. The file should import NumPy and pandas, run one small operation from each package, and print the results.

Use this order: first import NumPy as np, then import pandas as pd; next run the NumPy calculation; finally build and print the pandas DataFrame.

Import Checks Belong in Tiny Files

When setup is uncertain, make a new file called something like check_libraries.py:

import numpy as np
import pandas as pd
print("NumPy works:", np.array([10, 20, 30]).mean())
print("pandas works:")
print(pd.DataFrame([{"subject": "math", "minutes": 45}]))

Again, the goal is not to study DataFrames yet. The goal is to keep the installation check tiny enough that a failure has only a few possible causes.

Run only that file. If it fails, the problem is installation or environment setup, not your study log. Once the check works, return to the project.

Explain an import error

0 / 10 points

A student runs a study log script and sees:

ModuleNotFoundError: No module named 'pandas'

In a short explanation (about 3-5 sentences), explain what this error usually means and what small check the student should do before changing the study log code.

Do Not Hide Setup Problems in Project Code

Avoid this pattern:

try:
import pandas as pd
except ModuleNotFoundError:
print("pandas is missing")

That may look helpful, but for this course it hides the real fix. The study log now requires pandas. If pandas is missing, the environment needs to be fixed. The project code should not quietly continue without the library or pretend that the summary can still be produced.

It is fine to write a tiny check file that prints a friendly message. It is also fine for the real project to fail loudly on a missing required package. A missing package is not a normal study-log input problem like an empty session list. It is a setup problem.

When asking for help, include:

  • the command you used to install,
  • the command you used to run the script,
  • the full error message,
  • the output of python --version or py --version,
  • and the output of python -c "import sys; print(sys.executable)" (with your usual Python command).

Those five facts usually reveal whether the issue is installation, interpreter mismatch, or something else.

Check Your Understanding

  1. Why do math and random not need pip install?
  2. Why is python -m pip install pandas safer than just saying pip install pandas?
  3. What does ModuleNotFoundError usually mean?
  4. Why should setup checks be done in a tiny separate file?

AI Study Aid

Create a chapter diagram

We're looking into to what extent AI-generated diagrams could help with studying.

Use this study aid to generate an AI-generated visual summary of the material you just studied. Each diagram style emphasizes the content in a different way, so you can choose the focus that feels most useful.

Using the diagram generator is voluntary and does not affect your course progress. We encourage you to try it out and see if it helps, but it's totally up to you! Your answers help us understand how to make better study aids in the future.

Diagrams tried: 0