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:
python -m pip install pandas numpyIf 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:
py -m pip install pandas numpyRun 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 npimport 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:
python -m pip install pandas numpypython my_script.pyor, on Windows if needed:
py -m pip install pandas numpypy my_script.pyYou can also ask Python to print the exact interpreter it is using:
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:
python -m pip install pandasimport pandas as pdBut 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 name | Import name | Common alias |
|---|---|---|
numpy | numpy | np |
pandas | pandas | pd |
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:
python -m pip install pandaspy my_script.pyThe package may have installed successfully for python, but the script ran with py. Or the reverse:
py -m pip install pandaspython my_script.pyThe habit is simple: use the same command family for installation and running.
python -m pip install pandas numpypython check_libraries.pyor:
py -m pip install pandas numpypy check_libraries.pyThis 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.
Try these checks in order:
- Run
python --versionorpy --versionand confirm Python works. - Run
python -m pip --versionorpy -m pip --version. - Run
python -c "import sys; print(sys.executable)"(or the equivalentpython3/pycommand) and check that it names the study-log project environment. - Use the same Python command for installation and running scripts.
- 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 npimport 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 pdexcept 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 --versionorpy --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
- Why do
mathandrandomnot needpip install? - Why is
python -m pip install pandassafer than just sayingpip install pandas? - What does
ModuleNotFoundErrorusually mean? - 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.