首页 > 编程语言 >How to run python interactive in current file's directory in Visual Studio Code?

How to run python interactive in current file's directory in Visual Studio Code?

时间:2022-11-19 09:45:25浏览次数:68  
标签:Code run python current file directory

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 the notebookFileRoot setting. The VSCode variables such as ${fileDirname} are specific to task and debug configuration files (launch.json and task.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')

 

标签:Code,run,python,current,file,directory
From: https://www.cnblogs.com/chucklu/p/16905468.html

相关文章

  • vscode中文环境配置
    1.背景2.配置2.1.安装中文包如果没有按照中文插件需要先按照中文插件  如果你是首次安装,安装完成后会引导你重启,就可以了2.2.设置成中文环境打开VSCode软件,按......
  • UnicodeDecodeError:'gbk' codec can't decode byte 0x80 in position 0 illegal multi
    UnicodeDecodeError:'gbk'codeccan'tdecodebyte0x80inposition0illegalmultibytesequence 回答1ifyouwillopenfilewithutf-8,thenyouneedwrite:o......
  • 关于Docker安装后运行提示Cannot connect to the Docker daemon at unix:///var/run/d
    问题:执行如下命令即可:systemctldaemon-reloadsystemctlrestartdocker.service ......
  • [oeasy]python0017_解码_decode_字节序列_bytes_字符串_str
    ​ 解码decode回忆上次内容code就是码最早也指电报码后来有各种编码、密码、砝码、条码都指的是把各种事物编个号encode就是编码编码就是给事物编个号......
  • python感知机
    感知机是一种二类分类的线性分类器,属于判别模型(另一种是生成模型)。简单地说,就是通过输入特征,利用超平面,将目标分为两类。感知机是神经网络和支持向量机的基础。现实过程如......
  • PythonAnywhere 部署Flas项目
    一、注册账号官网:https://www.pythonanywhere.com/ 二、将GitHub上的项目发送至PythonAnywhere三、配置环境及运行gitclonehttps://github.com/chao-yua......
  • Codeforces Round #834 (Div. 3)
    ABC略。D.MakeItRound问题可以看成凑出尽可能多的\(10\)作为因子。注意到\(10\)的因子只有\(1,2,5,10\)。首先,\(n\)自己已经凑出来的\(10\)没必要拆开,并......
  • 代码随想录day3---LeetCode203移除链表元素&707设计链表&206反转链表
    LeetCode203移除链表元素给你一个链表的头节点head和一个整数val,请你删除链表中所有满足Node.val==val的节点,并返回新的头节点。示例1:输入:head=[1,2,6,......
  • HOG算法的理解与python实现
    HOG称为方向梯度直方图(HistogramofOrientedGradient),主要是为了对图像进行特征提取。所以在传统目标检测算法中经常与SVM结合用于行人识别任务(当前都是基于深度学习来做......
  • python学习笔记(二)
    一、数据类型python里面直接auto了,跟c有很大不同,基本上由编译器自动检测赋值内容,但也可以手动确定。 只不过有挺多其他的函数很方便var1=100var2=200var3=300......