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?
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?
问题
回答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.
相关文章
- 【华为云技术分享】计算python程序执行时间
- 【Python成长之路】python 基础篇 -- 装饰器【华为云分享】
- 【Python五篇慢慢弹(5)】类的继承案例解析,python相关知识延伸
- Python 字符串_python 字符串截取_python 字符串替换_python 字符串连接
- python_os模块
- Python计时库——Time库的使用详解
- 如何安装Python环境以及为Visual Studio 2012安装Python插件
- [LINK]用Python计算昨天、今天和明天的日期时间
- 华为OD机试 - Excel单元格数值统计(Java & JS & Python)
- Python编程语言学习:基于python各种库(matplotlib、Image)利用多种方法展示图片或进行图片可视化之详细攻略
- Python编程语言学习:python语言中快速查询python自带模块&函数的用法及其属性方法、如何查询某个函数&关键词的用法、输出一个类或者实例化对象的所有属性和方法名之详细攻略
- IDE之VS:利用 Visual Studio中的IDE配置python语言进行编程
- Python:利用python编程将上海十六区,2020年5月份房价实时地图(数据来源房天下)进行柱状图、热图可视化
- Python编程语言学习:python语言中快速查询python自带模块&函数的用法及其属性方法、如何查询某个函数&关键词的用法、输出一个类或者实例化对象的所有属性和方法名之详细攻略
- Python语言编程学习:利用python输出当前python版本、MSC版本型号
- Python语言学习:利用python获取当前/上级/上上级目录路径(获取路径下的最后叶目录的文件名、合并两个不同路径下图片文件名等目录/路径案例、正确加载图片路径)之详细攻略
- Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(Canvas)
- 成功解决read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and wil
- 猿创征文|Python基础——Visual Studio版本——pytest
- 猿创征文|Python基础——Visual Studio版本——第五章 文件I/O
- 已解决2.Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and wi
- 〖Python WEB 自动化测试实战篇③〗- python-selenium环境配置搭建
- Python学习105:协程
- python 协程嵌套
- python之配置文件读写
- python setup.py install 报错:error: [WinError 3] 系统找不到指定的路径。: 'C:Program Files (x86)Microsoft Visual Studio 14.0VCPlatformSDKlib
- python基础===python中文手册
- python基础===利用PyCharm进行Python远程调试(转)
- python基础===Python性能优化的20条建议
- 【Leetcode刷题Python】只出现一次的数字 II
- 【异常】前端ERR! stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.