3

I have a folder with a few subfolders as here:

my-project/
  input/
    data.csv
  src/
    script.py

I want to read data from my-project/input/data.csv within script.py, so I have:

import pandas as pd
data = pd.read_csv('../input/data.csv')

However, my workspace is my-project so when I run script.py it returns:

Exception has occurred: FileNotFoundError [Errno 2] No such file or directory: '../input/data.csv

which is understandable as input is within my-project, not at the same level. However, referring with .. really feels like the correct way to refer to data.csv as we do it from script.py. But maybe I'm wrong?

In case this is a reasonable way to refer to my data file - how can I setup the VSCode to be able to run the script without returning the error? I think there should be a way to add the subfolder to searching path, without needing to open the subfolder as a workspace, but I had a bad luck trying to find it.

@Edit: Please note that I'm aware of the concept of relative/absolute paths. This questions is more about VSCode settings. I am also willing to accept the answer: "No, referring with ../input/data.csv is the dumb thing to do in this settings. You should refer with input/data.csv instead, because..." (it's contradictory with my current understanding, but I could be entirely wrong and I'd be happy to learn a different point of view)

jakes
  • 1,964
  • 3
  • 18
  • 50
  • https://stackoverflow.com/a/3430395/9198357 . Maybe this solution can help you out with current working directory and the running script. You can print the path to see how it works. Try to change the working directory to "my-project/src". – Jacky Feb 26 '21 at 08:33
  • Does this answer your question? [FileNotFoundError: \[Errno 2\] No such file or directory](https://stackoverflow.com/questions/22282760/filenotfounderror-errno-2-no-such-file-or-directory) – Davinder Singh Feb 26 '21 at 08:35
  • It's not. Please check the @Edit at the end of the post. – jakes Feb 26 '21 at 08:49

5 Answers5

2

I believe this is simpler than you thought, let do it together!

  1. Creating empty folder

  2. Openning it with VSCode

The used extensions ...

  1. Extensions

I believe the below steps are not so hard!

  1. Essential steps

Switch the default interpreter to the created virtual environment

  1. Virtual environment

Create a simple launch.json, with simple choice python script

  1. Creating launch.json

  2. Launch.json file sample

Guess what now! All we have to do now is select a script.py file in the editor then ....... RUN!

  1. After running a script

You can see the result in the terminal.

let's talk a bit...

The generated launch.json file will force us to select the **src.script.py" in the editor before we click the start button every time we want to launch the program, if you like so, I can suggest a more proper way

In step 6, you can choose Module instead of Python file, after that the editor will prompt you a field asking for the module name, our input must be src.script.

We will get our launch.json file like this ...

New launch.json

And now, we can start the program from where we want, which means if the opened file in the editor is "src/data.json" as an example, going to the debugger section and click start will always start the src/script.py file.

bguernouti
  • 162
  • 7
1

by setting launch.json

"cwd":"${fileDirname}"

// or use "cwd":"${workspaceFolder}/src" to specifically assign /src as your current working directory

then all the relative path starts from the py file you are running (in your case will be my-project/src), you should be able to use:

data = pd.read_csv('../input/data.csv')

the launch.json variables can be referenced here: https://code.visualstudio.com/docs/editor/variables-reference


here's my sample env for your reference:

file structure:

my-project/
  .vscode/
    launch.json
  input/
    xxxxx.txt
  src/
    main.py

launch.json:

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

main.py:

with open('../input/xxxxx.txt', 'r') as file_input:
    res = file_input.read()
    print(res)
Sean Hsieh
  • 305
  • 2
  • 13
  • I like this approach. However - correct me if I'm wrong - the launch.json is only applicable when running code in debugging mode, and not with 'Run Python in Terminal', right? – jakes Mar 03 '21 at 07:33
  • You are right. Let’s say if you want to run `/src/script.py`, the relative path still starts from `/src` when you have this launch.json, however the relative path starts from `the folder you call the py file` when you run in Terminal. use `os.getcwd()` to make a small test in vscode and in terminal you will see the difference. – Sean Hsieh Mar 03 '21 at 08:10
0

If you run the script in your workspace then input/data.csv should work. The location is not based on where your script is, but from where you run it.

To get the current working directory you can use

import os
print(os.getcwd())

and then go from there.

Edit: you can change the working directory in your launch.json as described here: https://stackoverflow.com/a/55072246/14246131

FelixF
  • 21
  • 5
  • I should have probably point out that I'm aware of that. Still, referring with `../input/data.csv` feels like the way to go from readability point of view and I feel that there should be an option to adjust settings to make it work. – jakes Feb 26 '21 at 08:27
0

I would typically do the following -- figure out the directory where my current .py file is and use a relative path from there

import os
py_folder = os.path.dirname(os.path.abspath(__file__))
input_folder = os.path.join(py_folder, '../input')
data = pd.read_csv(os.path.join(input_folder', 'data.csv'))

this way it does not matter from which directory you run your .py, and it works for any development environment ie not VSCode specific

piterbarg
  • 8,089
  • 2
  • 6
  • 22
0

You are probably referring to the file like ../input/data.csv in relation to the path of your script.py

But that is not the correct way to go about it you should be adding the file path in relation to from where you are executing the script.py, which I assume is from the root of project folder most probably. Especially if you are using the run python command from VS code.

Hence why the path input/data.csv should work in that case.

Ritwik G
  • 406
  • 2
  • 8