1

I am trying to run the code I have made for some plots and I can fully run it on terminal and spyder (I want to switch from spyder to VS code complete for data analysis) but I keep receiving an error saying my CSV file is not found, while if I run this directly on my terminal or spyder I don't get such error

So if I try to run my code using the run cell from VS Code I get this error:

import pandas as pd...
import pandas as pd...
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
 in 
      4 
      5 
----> 6 LNA_w2Path_PAC_AND_PSP = pd.read_csv('../../Results/CSV/LNA_w2Path_PAC_AND_PSP.csv')
      7 LNA_w2Path_PAC_AND_PSP.columns = LNA_w2Path_PAC_AND_PSP.columns.str.strip().str.lower().str.replace(' ', '_').str.replace('(', '').str.replace(')', '').str.replace('/', '').str.replace('=','_').str.replace(';','')
      8 plt.figure()

~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
    700                     skip_blank_lines=skip_blank_lines)
    701 
--> 702         return _read(filepath_or_buffer, kwds)

Sorry for the not wrapped code here, apparently markdown doesn't support this. The code I am trying to run is:

#%% #for jupyter notebook 

import pandas as pd
from matplotlib import pyplot as plt
import numpy as np


LNA_w2Path_PAC_AND_PSP = pd.read_csv('../../Results/CSV/LNA_w2Path_PAC_AND_PSP.csv')
LNA_w2Path_PAC_AND_PSP.columns = LNA_w2Path_PAC_AND_PSP.columns.str.strip().str.lower().str.replace(' ', '_').str.replace('(', '').str.replace(')', '').str.replace('/', '').str.replace('=','_').str.replace(';','')
plt.figure()
plt.plot(LNA_w2Path_PAC_AND_PSP.net18net049_h_0__pac_db20vv_harmonic_0_x/1E9, LNA_w2Path_PAC_AND_PSP.net18net049_h_0__pac_db20vv_harmonic_0_y, linewidth=2.0)
plt.ylabel("$\mathrm{Harmonic \ response \ (dB)}$")
plt.xlabel("$\mathrm{Frequency \ (GHz)}$")
plt.title("Harmonic response of LNA+2-Path Filter")
plt.grid(True, which="both")
plt.show()

If I simply run a python3 myfile.py it works fine.

EDIT

My .json file looks like this:

{
    "git.autofetch": true,
    "python.pythonPath": "/home/theis/anaconda3",
    "window.zoomLevel": 2,
    "editor.find.addExtraSpaceOnTop": false,
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "python.jediEnabled": false,
    "workbench.colorTheme": "Dracula Soft",
    "python.linting.pylintEnabled": false,
    "python.linting.enabled": false,
    "languageTool.language": "en-US",
    "julia.enableTelemetry": true,
    "python.terminal.executeInFileDir": true
}

EDIT2:

So I added "cwd": "${fileDirname}" to my launch.json and tried to run this code in both debugger and using the jupyter notebook extension:

#%%

import os
print("Hello World!")
print(os.getcwd())

The debugger returns:

Hello World!
/home/theis/code/N_path_intership/PlottingCode/python

and the jupyter notebook extension returns:

Hello World!
/home/theis/code/N_path_intership/PlottingCode
2
  • 1
    Probably that the current working directory of your debugger is different from the one of your default running terminal Commented Apr 19, 2019 at 7:24
  • How I could set so both are the same? Commented Apr 19, 2019 at 10:18

2 Answers 2

1

In the debug menu, click on the little setting icon to open launch.json. Your file should look like that:

{
    "version": "0.2.0",
    "configurations": [
    {
            "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
    }, 

    //... other settings, but I modified the "Current File" setting above ...
}

You can add a cwd key (which means current working directory) and set it to what you want:

{
    "version": "0.2.0",
    "configurations": [
    {
            "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd": "${fileDirname}/<WhateverYouWant>"
    }, 

    //... other settings, but I modified the "Current File" setting above ...
}

It should solve your problem with the right path.

Sign up to request clarification or add additional context in comments.

1 Comment

Should I truly add something after the fileDirname i.e: "${fileDirname}/myfolder" because as far I understood the fileDirname already does the job of getting the path by itself. I have added "cwd": "${fileDirname}" without any success.
0

Maybe this answer can help you: https://stackoverflow.com/a/49275867/9628974

It's about changing the VSCode settings to execute python in the file's firectory.

1 Comment

This was set already (I have added the .json file in the EDIT)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.