简体   繁体   中英

How to run python from different drive using Visual Studio Code

My OS is Windows 10.

I have test.py file in E drive.

I installed Python 3.7.2 and Visual Studio Code in C drive.

I set system path for Python 3.7.2 so I could run python on CMD on both of the drives.

在此处输入图像描述

I also set python.pythonPath in setting.json file in.vscode under my work folder.

So python.pythonPath is same as the system path.

but I can't run that test.py file on VScode.

It shows python 3.7.2 file's path and says "No such file or directory".

Is there a way to run python file on different drive using VScode?

If you've already set Python as an environment variable, you can simply route to the directory where the file is present (in this case E: drive) and run the command:

python test.py

You can use debugging to run specific files in a reusable way.

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

It will create a launch.json file with contents similar to this. You can add additional
properties. VSCode is good about helping you create and edit the launch.json file.

{
    // 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']
        }
    ]
}

I installed Code Runner Extension and it followed my system path of python 3.7.2 automatically. Now I can simply run my code with this short cut "Crtl+Alt+N" instead of entering "python test.py" every time on the terminal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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