9

I want to read an Excel CSV file, and after researching, I realized I need to import pandas as pd. Is there a way to install it into the Visual Studio Code?

I have tried typing import pandas as pd, but it shows a red line. I'm still new to Python.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
programmingnoob
  • 109
  • 1
  • 1
  • 4
  • This must be a mega duplicate. Where is the canonical question? – Peter Mortensen Jul 04 '23 at 21:04
  • What target environment? [Linux](https://en.wikipedia.org/wiki/Linux)? Windows? For example, 'pip' may not provide enough of an abstraction to work in all places. – Peter Mortensen Jul 04 '23 at 21:10
  • OK, the OP has left the building: *"Last seen more than 1 year ago"* – Peter Mortensen Jul 04 '23 at 21:10
  • @Peter I don't imagine there is a canonical question since this is well-covered in the docs: [Data Science in VS Code tutorial](https://code.visualstudio.com/docs/datascience/data-science-tutorial) (recommends Miniconda), or [Pandas Installation](https://pandas.pydata.org/docs/getting_started/install.html) (recommends Anaconda) followed by [Python in Visual Studio Code](https://code.visualstudio.com/docs/languages/python#_install-python-and-the-python-extension) for how to select the interpreter – wjandrea Jul 04 '23 at 21:17
  • @wjandrea: ChatGPT recommends 'pip' (like some of the answers here)... (even when attempted restricted to [ArcoLinux](https://arcolinux.info/)). Just another example of its failures, I suppose. – Peter Mortensen Jul 04 '23 at 21:42
  • I faced the same problem. I opened a terminal in MAcos. I ran pip install pandas but I still get error my VS Code – DuyguK Jul 05 '23 at 09:34
  • @DuyguK There's an existing question about that: [Unable to import a module that is definitely installed](/q/14295680/4518341). That said, pip isn't the only solution; read through the other answers here or the official tutorial or the other links I mentioned. – wjandrea Jul 05 '23 at 17:32

7 Answers7

13

I think the previous answers are very well put already. I am just going to add to that.

Windows:

  1. open cmd

  2. type python -m pip install pandas

  3. restart your Visual Studio Code

Linux or macOS:

  1. open a terminal

  2. type pip install pandas

  3. restart your Visual Studio Code

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • it works very well for win10 – Melisa Mar 09 '23 at 09:31
  • `pip` isn't necessarily installed on Linux, e.g. Ubuntu. Instead, I'd recommend Miniconda or Anaconda, which is also what the [Data Science in VS Code tutorial](https://code.visualstudio.com/docs/datascience/data-science-tutorial) recommends (for any OS). – wjandrea Jul 04 '23 at 21:21
  • `pip install pandas` [will not work](https://stackoverflow.com/questions/67946868/how-do-i-install-pandas-into-visual-studio-code#comment135081825_67947278) on an [Arch Linux](https://en.wikipedia.org/wiki/Arch_Linux)-based system. – Peter Mortensen Jul 04 '23 at 21:47
8

As pandas is a Python library, you can install it using pip - the Python's package management system. If you are using Python 2 >=2.7.9 or Python 3 >=3.4, pip is already installed with your Python. Ensure that the Python executable's location has been added to PATH.

Then, to install pandas, just simply do:

pip install pandas
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • hello, so I would just need to go to the extension part of visual studio, type in pip and install it? btw, thank you for the response. rly appreciate it:) – programmingnoob Jun 12 '21 at 18:18
  • 1
    Glad to help! You should open & run the command in `Terminal` if you are using `MacOs`. Or use `Command Prompt` if you are on `Windows`. – meobilivang Jun 14 '21 at 01:17
  • No, this is result on an [Arch Linux](https://en.wikipedia.org/wiki/Arch_Linux)-based system ([ArcoLinux](https://arcolinux.info/)): ***"error: externally-managed-environment.*** *This environment is externally managed. To install Python packages system-wide, try 'pacman -S python-xyz', where xyz is the package you are trying to install. If you wish to install a non-Arch-packaged Python package, create a virtual environment using 'python -m venv path/to/venv'. ..."* – Peter Mortensen Jul 04 '23 at 20:54
  • cont' - *"Then use path/to/venv/bin/python and path/to/venv/bin/pip. If you wish to install a non-Arch packaged Python application, it may be easiest to use 'pipx install xyz', which will manage avirtual environment for you. Make sure you have python-pipx. installed via pacman."* – Peter Mortensen Jul 04 '23 at 20:55
  • On some system (e.g., some versions of [Ubuntu](https://en.wikipedia.org/wiki/Ubuntu_%28operating_system%29)), the name of the executable may be `pip3`, not `pip`. – Peter Mortensen Jul 04 '23 at 20:57
  • `pacman -S python-pandas` worked on ArcoLinux. It installed Pandas 1.5.3. – Peter Mortensen Jul 04 '23 at 21:42
4

For anyone else in a similar situation, I'd recommend following along with this Visual Studio Code official tutorial: Data Science in Visual Studio Code tutorial

It guides you to use Conda instead of Pip, and set up a Python environment, along with installing various packages like Pandas, Jupyter, etc.

For example, after installing the Python extension for Visual Studio Code and Miniconda or Anaconda:

conda create -n myenv python=3.9 pandas
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
atlas_scoffed
  • 3,542
  • 30
  • 46
3

You can install using pip:

pip install pandas

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
George Imerlishvili
  • 1,816
  • 2
  • 12
  • 20
  • No, this is the result on an [Arch Linux](https://en.wikipedia.org/wiki/Arch_Linux)-based system ([ArcoLinux](https://arcolinux.info/)): ***"error: externally-managed-environment.*** *This environment is externally managed. To install Python packages system-wide, try 'pacman -S python-xyz', where xyz is the package you are trying to install. If you wish to install a non-Arch-packaged Python package, create a virtual environment using 'python -m venv path/to/venv'. ..."* – Peter Mortensen Jul 04 '23 at 21:14
  • cont' - *"Then use path/to/venv/bin/python and path/to/venv/bin/pip. If you wish to install a non-Arch packaged Python application, it may be easiest to use 'pipx install xyz', which will manage avirtual environment for you. Make sure you have python-pipx. installed via pacman."* – Peter Mortensen Jul 04 '23 at 21:14
  • On some system (e.g., some versions of [Ubuntu](https://en.wikipedia.org/wiki/Ubuntu_%28operating_system%29)), the name of the executable may be `pip3`, not `pip`. – Peter Mortensen Jul 04 '23 at 21:14
  • `pacman -S python-pandas` worked on ArcoLinux. It installed Pandas 1.5.3. – Peter Mortensen Jul 04 '23 at 21:41
1

In the terminal on Visual Studio Code, check and make sure the Python interpreter is installed:

py -3 --version

Then you can install libraries with:

py -m pip install *packagename*

This was a simple solution I came up with since the others weren't working on my system.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aaronrod
  • 11
  • 1
0

I also had the same question. As a newbie, I did not understand the answer. Perhaps these notes will help others in the same boat.

You need to type this into command prompt (not visual studio or python): pip install pandas

Before you do that, you must "Ensure that Python has been added to PATH". This did not make sense to me, but there are pages on this if you Google.

Also useful to know: CMD and Terminal = Command Prompt (please correct me if that's not true).

wjandrea
  • 28,235
  • 9
  • 60
  • 81
KymBo
  • 21
  • 2
  • *"CMD and Terminal = Command Prompt"* -- Yeah, close enough for this purpose, but technically a terminal is different. BTW you might hear "shell" used interchangeably too. – wjandrea Jul 04 '23 at 21:32
-1

You need to start off by installing Anaconda in order to create an environment for Pandas; you can manage this environment with Anaconda. Go to your terminal then run conda create -n myenv python=3.9 pandas jupyter seaborn scikit-learn keras tensorflow. It will create environments for all of the libraries mentioned above. PS : this is an old post, please check python's latest version

After that click on your Kernel1 (top right) and chose the environment that is associated with Anaconda

aziz bali
  • 1
  • 1