How to run python interactive in current file's directory in Visual Studio Code? Python路径问题
How to run python interactive in current file's directory in Visual Studio Code?
问题
When executing "Run Selection/Line in Python Terminal"
command in VSCode, terminal's current working directory is the workspace root directory. How can we set current directory of terminal to the current file's directory when running the selection/line?
回答1
这个用来解决调试时的路径问题
I used the option Run -> Add Configuration (or Open configuration, if available) This will open your current 'launch.json' file. Now you may add this line to the configuration wanted (in my case was Python):
"cwd": "${fileDirname}"
This line will make VSCode to run your stuff in the same folder as the file is being executed.
You can get more details in this link: https://code.visualstudio.com/docs/editor/variables-reference
Here is my full json file (just for reference):
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}"
}
]
}
回答2
Update following release 2019.10.44104
Following release 2019.10.44104 of the VS Code python extension, you can now set the python.dataScience.notebookFileRoot
to ${fileDirname}
to directly start the python interactive window in the directory of the file you're running.
Note that the root directory will not change if you then run code from another file unless you interrupt/restart the kernel (or close VS Code). On this aspect, see the following comment and the corresponding github issue.
For the Python Interactive Window, the setting you're looking for is python.dataScience.notebookFileRoot
. However, as explained in this answer to a similar question,
Always opening on the file location (without having to set
notebookFileRoot
to an absolute path per folder) is not supported via thenotebookFileRoot
setting. The VSCode variables such as${fileDirname}
are specific to task and debug configuration files (launch.json
andtask.json
).
See also the associated github issue.
As indicated, you can still set this setting to a specific absolute path, which might be enough if you're mainly working on a single project at a time.
Alternatively, you could also add the following code at the top of your script/notebook:
import os
os.chdir('absolute-path-to-workingDir')
Having a structure like this
workingDir
|- foo
|- file.py
and a file.py
:
#%%
import os
print(os.getcwd())
the output in the interactive session is the absolute path of the workingDir
.
下面这个是用来解决,在terminal里面执行Python文件时候的路径问题
File-->Preference 然后搜索@ext:ms-python.python execute
If you’re using the Python extension from Microsoft like me all you have to do is select “Python > Terminal: Execute in File Dir” from the extensions settings and tada issue fixed.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2021-11-19 2021年上海市成人高校招生最低录取控制分数线
2020-11-19 Install and manage packages in Visual Studio using the NuGet Package Manager
2020-11-19 How to check the version of an assembly (dll)?
2020-11-19 RSACryptoServiceProvider.FromXmlString
2014-11-19 炉石相关的开源