VS代码和python调试器返回错误,但可以在终端中运行
我正在尝试运行我为一些绘图编写的代码,我可以在terminal和spyder上完全运行它(我想从spyder切换到VS code complete进行数据分析),但我一直收到一个错误,说找不到我的CSV文件,而如果我直接在我的terminal或spyder上运行它,我不会得到这样的错误 因此,如果我尝试使用VS代码中的“运行”单元格运行代码,则会出现以下错误:VS代码和python调试器返回错误,但可以在终端中运行,python,python-3.x,visual-studio-code,Python,Python 3.x,Visual Studio Code,我正在尝试运行我为一些绘图编写的代码,我可以在terminal和spyder上完全运行它(我想从spyder切换到VS code complete进行数据分析),但我一直收到一个错误,说找不到我的CSV文件,而如果我直接在我的terminal或spyder上运行它,我不会得到这样的错误 因此,如果我尝试使用VS代码中的“运行”单元格运行代码,则会出现以下错误: import pandas as pd... import pandas as pd... ----------------------
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)
很抱歉这里没有包装代码,显然markdown不支持这一点。
我尝试运行的代码是:
jupyter笔记本电脑的#%%
作为pd进口熊猫
从matplotlib导入pyplot作为plt
将numpy作为np导入
LNA_w2Path_PAC_和_PSP=pd.read_csv(“../../Results/csv/LNA_w2Path_PAC_和_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
plt.图()
plt.绘图(LNA_w2Path_PAC_和PSP.net18net049_h_0_PAC_db20vv_harmonic_0_x/1E9、LNA_w2Path_PAC_和PSP.net18net049_h_PAC_db20vv_harmonic_0_y,线宽=2.0)
plt.ylabel(“$\mathrm{Harmonic\response\(dB)}$”)
plt.xlabel(“$\mathrm{Frequency\(GHz)}$”)
plt.标题(“LNA+2路滤波器的谐波响应”)
plt.grid(True,which=“两者”)
plt.show()
如果我只是运行一个python3myfile.py
,它就可以正常工作
编辑
我的.json
文件如下所示:
{
"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:
因此,我在我的launch.json
中添加了“cwd”:“${fileDirname}”
,并尝试在调试器和jupyter笔记本扩展中运行此代码:
#%%
导入操作系统
打印(“你好,世界!”)
打印(os.getcwd())
调试器返回:
你好,世界!
/home/theis/code/N\u path\u intership/plotingcode/python
jupyter笔记本电脑扩展将返回:
你好,世界!
/home/theis/code/N\u路径\u芯片间/绘图代码
也许这个答案可以帮助您:
这是关于更改VSCode设置以在文件的FireFactory中执行python。也许这个答案可以帮助您:
它是关于更改VSCode设置以在文件的FireDirectory中执行python。在调试菜单中,单击小设置图标打开
launch.json
。您的文件应如下所示:
{
"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 ...
}
您可以添加cwd
键(表示当前工作目录)并将其设置为所需:
{
"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 ...
}
{
“版本”:“0.2.0”,
“配置”:[
{
“名称”:“Python:当前文件(集成终端)”,
“类型”:“python”,
“请求”:“启动”,
“程序”:“${file}”,
“控制台”:“集成终端”,
“cwd”:“${fileDirname}/”
},
//…其他设置,但我修改了上面的“当前文件”设置。。。
}
它应该可以用正确的路径解决您的问题。在“调试”菜单中,单击小设置图标打开
launch.json
。您的文件应该如下所示:
{
"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 ...
}
您可以添加cwd
键(表示当前工作目录)并将其设置为所需:
{
"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 ...
}
{
“版本”:“0.2.0”,
“配置”:[
{
“名称”:“Python:当前文件(集成终端)”,
“类型”:“python”,
“请求”:“启动”,
“程序”:“${file}”,
“控制台”:“集成终端”,
“cwd”:“${fileDirname}/”
},
//…其他设置,但我修改了上面的“当前文件”设置。。。
}
它应该可以用正确的路径解决您的问题。可能调试器的当前工作目录与默认运行终端的工作目录不同,我如何设置两者相同?可能调试器的当前工作目录与默认运行终端的工作目录不同,我如何设置两者相同两者都是相同的?这已经设置好了(我在编辑中添加了.json文件)这已经设置好了(我在编辑中添加了.json文件)我应该在fileDirname之后添加一些内容吗,即:
“${fileDirname}/myfolder”
因为据我所知,fileDirname本身已经完成了获取路径的工作。我添加了“cwd”:“${fileDirname}”
但没有任何成功。我真的应该在fileDirname之后添加一些东西吗,即:“${fileDirname}/myfolder”
因为据我所知,fileDirname本身已经完成了获取路径的工作。我添加了“cwd”:“${fileDirname}”
,但没有成功。