pythonvisual-studio-codejupyter-notebook

Why do I get "ModuleNotFoundError: No module named 'pandas' " in Jupyter when I already installed pandas before setting up the .venv?


I'm trying to learn how to use VSCode, after installing Jupyter and trying to add pandas. When I installed VSCode along with Node I got a second version of Python, 3.12.6. Within VSCode I installed Jupyter and trying to follow a tutorial tried importing pandas, but got the 'No Module Found' error.

From what I've read trying to understand the problem, I don't think it's because Jupyter doesn't know which version of Python to use as I've set up a .venv in this Python 3.12.6, the same version that had installed pandas. I used pip show pandas to confirm. It's pandas 2.2.3.

So, do I have to install pandas again within Jupyter? I'm doing it within an .ipynb file which shows '.venv (Python 3.12.6)', so as far as I understand I'm using the right kernel.

What am I missing?


Solution

  • Since you installed Pandas before setting up the virtual environment, it was installed in the global environment and is not available to the virtual environment. There are three way to solve this:

    1. Install Pandas again into the virtual environment.

      This is the best option if you're starting a project that depends on Pandas, because it gives you control over dependency versioning, i.e, if the project relies on features in specific versions of Pandas, those are part of the project and not the OS, where Pandas could get upgraded or even downgraded if you switch OSes.

      • In Jupyter (IPython), you can use %pip install pandas to install within the current kernel (current environment).

        If you were using conda, you would use %conda install pandas

      • Outside Jupyter, you can activate the virtual environment then use pip install pandas (or conda install pandas for a conda env).

        You may want to enable python.terminal.activateEnvironment in VSCode to have it automatically activate the environment in the internal terminal.

    2. Use the global environment.

      If you don't really care about the Pandas version and don't want to futz with a virtual environment, this is fine.

    3. Let the virtual environment access the global environment.

      This sort of defeats the purpose of a virtual environment, so I would only do this as a last resort.

    Related