Search code examples
pythonpandasvisual-studio-codeimporterror

Pandas not found in Visual Studio Code even though it is definitely in the env


I am running Visual Studio Code on two machines, both set up essentially the same way. One is an iMac, the other a MacBook Air. In one of my projects, on the iMac, it doesn't recognize that pandas is installed, even though it is in the environment.

Here is the VSC python interpreter selection:

enter image description here

And here you can see that:

  1. it is not able to import pandas
  2. I'm in the env
  3. pip3 freeze shows pandas
  4. pip3 install indicates that pandas is already present

enter image description here

I've tried uninstalling pandas and reinstalling it, but that doesn't help. I've tried making sure it's in the non-env python install (it is), but that doesn't help.

I have other projects on the same computer set up essentially the same way, and pandas works fine. I also have the same project on my laptop, with the same setup, and it is working there too. I'm kinda stumped. I guess I'll be working on it on the laptop for now, but I'd appreciate any insight people may have...

Edits

  • if I run the code directly in the terminal, using the venv, it works fine
  • pip3 shows pandas 1.1.1 in the terminal; still haven't figured out whether I can get VSC to show me what it's seeing.
  • I figured out how to call pip from inside Python, and when I call this code from inside VSC it doesn't show pandas, but if I call it (python3 test.py) from the terminal, it does. So, clearly, I have to figure out why VSC isn't actually using the right environment.
import pip

pip.main(["freeze"])
  • this is getting even stranger; at @jillcheng's suggestion, I looked in the actual env directory and it doesn't have the package installed. This jives with the problem and the above test (running pip freeze from inside py code): enter image description here
  • it does NOT, however, match up with the fact that if I activate the environment from the terminal window, it shows the package, as shown in the earlier screenshot.

Thank you! Dylan


Solution

  • According to your description, it is command that you could refer to the following steps:

    1. Enter the current virtual environment in VSCode.

    2. Enter 'pip --version ' to check if the pip currently used is from the current environment: (This ensures that using pip to install pandas module will be put into the current virtual environment.)

    enter image description here

    1. List of pips before installing pandas module: (There is no module pandas in the list.)

    enter image description here

    After installation:(I use the command 'pip install pandas'(windows10), mac: pip3 install pandas)

    enter image description here

    1. If it still has wavy lines, try reloading vscode.(Ctrl+Shift+p, Developer: Reload windows ). The module pandas can be used:

      enter image description here

    2. Check the installation package:

      If the pip list shows the pands module, but we still can't use it, we can find the current virtual environment folder and check whether there is pandas installation package:

      enter image description here