How can I allow VSCode Python to execute Python files without specifying absolute paths for python.exe and the file to be run?

3

1

I have a simple python project located in

c:\users\myuser\pythonproject\

I also have the latest version of python installed, and VSCode is running inside of ..\pythonproject\

When using the integrated terminal inside of VSCode to run a python script (right click > 'Run Python File in Terminal'), one would expect the following command:

python myfile.py

instead, VSCode specifies the absolute file path for not only Python.exe, but the file that is being run (despite the fact that it's being run inside the CWD:

C:/Users/myuser/AppData/Local/Programs/Python/Python37-32/
python.exe "c:/Users/myuser/pythonproject/myfile.py"

this seems cumbersome and clutters up my terminal. Is there any way to get VSCode to simplify the above verbose command to use relative commands (eg 'python')?

Thanks for any input. This may seem like a minor nitpick, but using this console all day has made me resent having to scroll through all this unnecessary info (especially since my actual project directory is much more complex than my example, and so longer).

Sajid Ansari

Posted 2019-09-09T18:12:42.800

Reputation: 33

Try to add the folder containing python.exe in path variable. – Biswapriyo – 2019-09-09T20:33:40.857

Answers

0

You need to install python and your project inside folders with shorter path.

As you most likely don't wish to change your folders, you may create NTFS junction points. Junctions are created by the mklink command.

You may this way create directory junctions under C:\ so the folders are aliased to, for example, C:\python and C:\project.

Much info is available on the Web regarding junctions, for example
The Complete Guide to Creating Symbolic Links (aka Symlinks) on Windows.

harrymc

Posted 2019-09-09T18:12:42.800

Reputation: 306 093

Thanks for the info. Is this to say that it isn't possible to actually make VSCode use the relative paths? If that's the case, this seems like the best option – Sajid Ansari – 2019-09-09T20:12:58.760

I doubt that relative paths can be made to work in VSCode, and frankly this isn't worth the headache of analyzing eventual possible problems. The junction solution will work, and you may even give them one-letter names. – harrymc – 2019-09-09T20:20:00.547

This is a good solution, unfortunately I do not have privileges to mklink to the C: directory, but this seems to be the only workaround. – Sajid Ansari – 2019-09-10T18:32:55.797

You mean you don't have permission to create a folder in C: and then create a junction inside it? – harrymc – 2019-09-10T19:08:07.500

I am able to make folders, however when attempting to use mklink I get You do not have sufficient privilege to perform this operation.. I am not an administrator of this system, so I can only assume that is why. – Sajid Ansari – 2019-09-10T19:51:47.707

2

I had the same problem, and I stumbled upon a (partial) solution:

First I selected a Python interpreter in Visual Studio Code by using the Select Python Environment option on the Status Bar. (docs)

This created a .vscode\settings.json file, where I simply changed the python.pythonPath value from an absolute file path to just python.exe:

{
    "python.pythonPath": "python.exe"
}

The full project and Python path is still displayed, but at least the Python path is cleaner:

enter image description here

Alex Vang

Posted 2019-09-09T18:12:42.800

Reputation: 121