zl程序教程

您现在的位置是:首页 >  后端

当前栏目

How to run python interactive in current file's directory in Visual Studio Code? Python路径问题

PythonVisualStudiocode to in &# 路径
2023-09-11 14:14:17 时间

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')

 

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.