繁体   English   中英

如何使用 Visual Studio Code 从不同的驱动器运行 python

[英]How to run python from different drive using Visual Studio Code

我的操作系统是 Windows 10。

我在 E 盘中有 test.py 文件。

我在 C 驱动器中安装了 Python 3.7.2 和 Visual Studio Code。

我为 Python 3.7.2 设置了系统路径,因此我可以在两个驱动器上的 CMD 上运行 python。

在此处输入图像描述

我还在我的工作文件夹下的.vscode中的setting.json文件中设置了python.pythonPath。

所以 python.pythonPath 与系统路径相同。

但我无法在 VScode 上运行该 test.py 文件。

它显示 python 3.7.2 文件的路径并显示“没有这样的文件或目录”。

有没有办法使用 VScode 在不同的驱动器上运行 python 文件?

如果您已经将 Python 设置为环境变量,您可以简单地路由到文件所在的目录(在本例中为 E:驱动器)并运行命令:

python test.py

您可以使用调试以可重用的方式运行特定文件。

https://code.visualstudio.com/docs/python/debugging

它将创建一个launch.json 文件,其内容与此类似。 您可以添加额外的
特性。 VSCode 擅长帮助您创建和编辑 launch.json 文件。

{
    // 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": "RunPython",
            "type": "python",
            "request": "launch",
            // run a file by specifying a full path
            "program": "E:/FullPathTo/FileA.py",
            // OR run the file currently open in VSCode
            // // "program": "${file}",
            // OR run a file relative to the current folder you have open
            // // "program": "${workspaceFolder}/folder/FileB.py"
            "console": "integratedTerminal",
            "args": ['InputArg1','InputArg2']
        }
    ]
}

我安装了 Code Runner Extension,它自动遵循了我的系统路径 python 3.7.2。 现在我可以简单地使用这个快捷方式“Crtl+Alt+N”运行我的代码,而不是每次在终端上输入“python test.py”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM