(VSCode) pandas.read_xlsx works in ipynb but not py

83 views Asked by At

I basically had only 1 line of code, that is:

data = pd.read_excel('Physical properties.xlsx', index_col=1, header=3)

this line works if it was in ipynb format, but produces [Errno 2] No such file or directory error if was run in py format. Both file exist in the same folder as the excel file, both has the exact same code (because I copy-pasted it). It does work if I put it as r'directory', but I'm wondering why it didnt work as just namefile.xlsx

It does work if I put it as r'directory', but I'm wondering why it didnt work as just namefile.xlsx

1

There are 1 answers

0
MingJie-MSFT On

You can use Debug mode in VSCode. Vscode uses the workspace as the root directory to search for files, so you need to make additional settings.

You have to add "cwd": "${fileDirname}" to your launch.json. Then click as the following figure:

enter image description here

This is the directory structure I use to reproduce the problem:

-Workspace
--.vscode
---settings.json
---launch.json
--sub1
---data.csv
---test.py

test.py

import pandas as pd

data = pd.read_csv("data.csv")

launch.json

"version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
      "cwd": "${fileDirname}"
    }
  ]