Setup: pandas and Notebook Support

Python doesn't come with pandas, so every data project lists its packages in a requirements.txt (pandas, matplotlib, openpyxl, ipykernel and pytest), installs them into its own virtual environment with pip, checks pandas.__version__, and adds VS Code's Jupyter extension for notebooks.

Novice · Python · Updated

By

After this lesson you can

  • Install a data project's packages into a virtual environment with pip install -r requirements.txt, and say what each of the five packages is for
  • Check an install by printing pandas.__version__, and read ModuleNotFoundError as a package missing from the Python that ran the code

Key takeaways

  • Python doesn't come with pandas. ModuleNotFoundError No module named 'pandas' means the Python that ran your code doesn't have it installed.
  • A data project's requirements.txt pins pandas for tables, matplotlib for charts, openpyxl for Excel files, ipykernel for notebooks and pytest for tests.
  • With the project's virtual environment on, pip install -r requirements.txt installs all five, and the packages each of them needs.
  • Printing pandas.version proves the install worked, and shows which version you have.
  • VS Code opens and runs notebooks with its Jupyter extension, by Microsoft, installed once from the Extensions view.

The first time you open a data notebook, from a class, a coworker or a tutorial, its first line is almost always import pandas as pd. On a fresh computer, that line stops with an error. This lesson sets up what every data project in this course needs, once per project, in a few commands: the packages for tables, charts, Excel files, notebooks and tests, and VS Code's support for notebooks.

It assumes Python, VS Code and Git are installed and you can make a virtual environment, as Setup on Windows: Python, VS Code and Git (or Setup on a Mac: Python, VS Code and Git) and Virtual Environments and pip show.

A new virtual environment has no pandas

Make a folder for this chapter's practice notebooks, bean-street, inside your python-course folder, open it in VS Code (File > Open Folder), and make its virtual environment in VS Code's terminal. On Windows (PowerShell):

python -m venv .venv
.venv\Scripts\Activate.ps1

On a Mac (zsh):

python3 -m venv .venv
source .venv/bin/activate

The prompt starts with (.venv) once it's on, and python is the environment's own, on a Mac too. Python comes with a large standard library, for files, dates, math and more, but not with pandas. Ask the new environment for it (the same line works in PowerShell and in zsh):

python -c "import pandas; print(pandas.__version__)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import pandas; print(pandas.__version__)
    ^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'pandas'

python -c reads "run this line of code". Read the error from its last line up: ModuleNotFoundError means Python looked for a package named pandas and didn't find one. pandas is a third-party package, published by other people on PyPI, so each project installs it into its own virtual environment. Remember the words the Python that ran the code: pandas can be installed in one Python and missing from another, which is why a notebook has you choose its Python, as Your First Notebook in VS Code shows.

A data project's requirements.txt

In the bean-street folder, make a new file named requirements.txt with these five lines, each pinned to an exact version with ==, read "exactly version":

pandas==3.1.1
matplotlib==3.11.2
openpyxl==3.1.5
ipykernel==7.4.0
pytest==9.2.0
Package What it's for
pandas Tables: loading, cleaning, filtering, grouping and summing data. It brings NumPy, the fast math it's built on.
matplotlib Charts: line, bar, scatter and more
openpyxl Reading and writing Excel .xlsx files; pandas uses it behind the scenes
ipykernel Lets VS Code run a notebook's cells with this folder's Python, the one in .venv
pytest Runs tests that check your code

Every exercise in this course comes with a requirements.txt like this one, listing only the packages it uses.

Install them with pip

With the virtual environment on, install everything on the list. -r reads "from the file":

pip install -r requirements.txt

pip downloads each package from PyPI, along with the packages each one needs: matplotlib needs a font tool and an image library, ipykernel needs IPython and the pieces that let VS Code talk to it, and so on. It prints a line for each one, and after a minute or two it ends with a long line that starts Successfully installed and lists more than 40 packages. Five lines in your file, more than 40 packages installed: that's normal.

Check that it worked

Run the same line that failed before, with the virtual environment on:

python -c "import pandas; print(pandas.__version__)"
3.1.1

pandas.__version__ (read "pandas dot dunder version"; dunder is short for the double underscores) is the version the import found. If you see 3.1.1, pandas is installed in this project's Python. If you still see ModuleNotFoundError, the virtual environment isn't on: check that the prompt starts with (.venv), activate it again, and run the install once more.

VS Code's Jupyter extension

VS Code opens notebooks with its Jupyter extension, by Microsoft. Install it once, for every project: open the Extensions view (Ctrl+Shift+X, or Cmd+Shift+X on a Mac), search for Jupyter, and click Install on Jupyter by Microsoft. If you open a notebook first, VS Code offers to install it for you.

VS Code's Extensions view with Jupyter typed in the search box, and the Jupyter extension by Microsoft installed

The extension is VS Code's half of notebook support; ipykernel, in the project's virtual environment, is Python's half. A notebook needs both, so every new data project starts with these steps: a folder, a virtual environment, a requirements.txt, and pip install -r requirements.txt.

Check yourself

2 questions to try on the lesson page.

So what

One short file and one command give any computer the same data toolkit, and pandas.__version__ tells you in a second whether the Python you're using really has it.

Key terms