Alfie Zhang
Alfie Zhang

Reputation: 61

I have installed pandas but cannot import in VScode

My lap is MacBookPro2017, macOS Mojave 10.14.16

I used to coding in Jupyter and everything goes well.

However when I want to do the same thing in VScode, it said that

"ImportError: No module named pandas"

I am sure I have installed pandas by

pip3 install pandas

I do following things:

1.Search for similar problems on SO, and someone says there may be more than one version python, the pip and pandas may under another path. Then I find there are python2.7 and python3.7 in my lap:

AlfiedeMacBook-Pro:~ alfie$ which python3
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
AlfiedeMacBook-Pro:~ alfie$ which python
/usr/bin/python
AlfiedeMacBook-Pro:~ alfie$ which pip
/Library/Frameworks/Python.framework/Versions/3.7/bin/pip
AlfiedeMacBook-Pro:~ alfie$ which pip3
/Library/Frameworks/Python.framework/Versions/3.7/bin/pip3
AlfiedeMacBook-Pro:~ alfie$ which pandas
AlfiedeMacBook-Pro:~ alfie$ pandas --version
-bash: pandas: command not found
AlfiedeMacBook-Pro:~ alfie$ pip --version
pip 19.2.2 from /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip (python 3.7)

And if I type:

AlfiedeMacBook-Pro:~ alfie$ pip3 install pandas
Requirement already satisfied: pandas in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (0.25.1)
Requirement already satisfied: pytz>=2017.2 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pandas) (2019.2)
Requirement already satisfied: python-dateutil>=2.6.1 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pandas) (2.8.0)
Requirement already satisfied: numpy>=1.13.3 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pandas) (1.17.0)
Requirement already satisfied: six>=1.5 in ./Library/Python/3.7/lib/python/site-packages (from python-dateutil>=2.6.1->pandas) (1.12.0)

So I think I have pandas in the true path(?)

2.Then I saw some one use sys to append the path, so I write it in front of my code:

import sys
sys.path.append('/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages')
import numpy as np
import pandas as pd

Then I got:

ImportError: this version of pandas is incompatible with numpy < 1.13.3

your numpy version is 1.8.0rc1.

Please upgrade numpy to >= 1.13.3 to use this pandas version

But my numpy version is 1.17, I have upgraded.

So I just want know what I should do to make the pandas can be used in VScode?

Upvotes: 6

Views: 12937

Answers (4)

For beginner: The answer kinda was given above, but I took a long time to find the exact solution.

  1. First thing, that this really happen because of the Environment that will be the responsible to interpret your Python code and properly run it (you can run even in the wrong environment, but it'll not take the result you want)
  2. To fix it in the best way:
  3. Find the version of your Python, typing python --version if it doesn't work type `python3 --version``
  4. Then after see the version - I'll consider as Python 3 - you'll type python3 -m venv venv this will create a virtual environment what is the recommended way to deal with your projects. A important reminder, it'll create the environment in the current folder that you're with your project
  5. Finally, you can activate it by typing . venv/bin/activate will activate your project. For some systems you don't even need to do it, but for me was necessary
  6. Then you go to your terminal in VS Code, open python and find where this environment is executing the follow commands: python3 import sys print(sys.executable)
  7. Copy the folder path. Then click in the lower left corner of your VS Code (it'll be written 'interpreter' or 'Python 2.x.x' or 'Python 3.x.x' (you got, right), then choose it, upper above will ask for you to choose a path to your interpreter and you paste the path you copied in the path 6.
  8. Finally now, install all the paths normally with pip install module

I hope it'll work for you peeps! I finally got my program working from here.

Upvotes: 3

industArk
industArk

Reputation: 369

Having that problem while being in a properly set up environment, the first you should do is to close all vscode windows and then start it fresh.

I've stumbled on that today and it has worked.

Upvotes: 0

sjd
sjd

Reputation: 1401

I also faced the same issue. My Python version in VS Code was same as Terminal. Same environment. Same sys.executable path. But the PATH in os.environ found in command prompt/spyder Console/Jupyter terminal was different compared to the PATH I got from VS Code terminal.

VS Code starts PATH like this from os.environ['PATH']:

'PATH': 'C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;...'`

And terminal starts PATH like this from os.environ['PATH']:

'PATH': 'C:\\Users\\userid\\Appdata\\Local\\Continuum\\Anaconda3;...'

So when I replaced the PATH variable to start with my anaconda path, Import pandas worked fine.

Though Anaconda Path was present in VS Code PATH variables also, it was at the end. So I guess it was changed by VS Code in startup.

Upvotes: 1

Martin Thoma
Martin Thoma

Reputation: 136379

I had the same issue. The problem was that VS Code was using a different environment (shown in the lower left corner). Switching the environment there solved the issue.

Upvotes: 11

Related Questions