How to specify the python version for VSCode virtual environment, when I have multiple python versions intalled?

Question:

I have both 64-bit and 32-bit Python installed. I was trying to create a virtual environment using the 32-bit Python.exe file in VSCode. I selected that version in the Python: Select Interpreter for my workspace (i.e. the C:Program Files (x86)Python37-32python.exe).

I then changed the launch.json file in the workspace to include the "python" interpreter:

{
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "python": "c:/Program Files (x86)/Python37-32/python.exe"
        }
    ]
}

However, when I create the virtual environment:

py -3 -m venv 32_bit_env

The python interpreter it uses is the 64-bit version from C:Program FilesPython37python.exe as shown in the pyvenv.cfg:

home = C:Program FilesPython37

Is there another location to change the directory for the python.exe file in the workspace? Thank you!

Asked By: Chris T

||

Answers:

There are two concepts you are mixing: The Python Interpreter used by VS Code and the py launcher. In addition to that, there is python command. I will try to explain how they are configured and how they affect and what.

1. python command

1.1. Used version specified by

PATH environment variable only. The first folder in the Process PATH environment variable with python.exe is used. Process PATH is formed by combining User and System PATH variables when starting the process. Subprocesses, such as the integrated terminal of VS Code inherit the PATH of the parent process during initialization.

  • If virtual environment is active, the used version is the one pointed by the home key of the pyvenv.cfg. Activating virtual environment modifies the PATH of the shell.

1.2. Used in

When you run python command in a terminal.

2. py command

The py command is the Python Launcher for Windows, added in Python 3.3.

2.2 Used version Specified by

First, these are checked in this order

  • An active virtual environment
  • A shebang line in the script (if present)
  • With -2 or -3 flag a matching PY_PYTHON2 or PY_PYTHON3 Enviroment variable
  • A PY_PYTHON Enviroment variable
  • From [defaults] in py.ini in your %LOCALAPPDATA%py.ini
  • From [defaults] in py.ini beside py.exe (use where py to locate)

It will always use the latest installed version, if not specified otherwise.

3. VS Code python.pythonPath

3.1. Used version specified by

In the .vscodesettings.json, the python.pythonPath key.

3.2. Used in

  • When you use Python: Run Python File in Terminal in VS Code
  • When you use Run: Run Without Debugging for .py file in VS Code
  • When you use Run: Start Debugging for .py file in VS Code
  • When you launch a new integrated terminal in VS Code, if the python.exe is inside a virtual environment, it will be activated.
  • Language services, like auto-complete, linting, syntax-checking, formatting. Only those that are not ran using the integrated terminal.

Specifically, the python.pythonPath setting does not affect to what happens, if you run py or python in the integrated terminal. It can only affect it if you have specified a virtual environment for python.pythonPath, and launched new integrated terminal, and the virtual environment is activated. That is, there is no extra "magic" added VS Code or the VS Code integrated terminal.

The integrated terminal is (by default) just a regular Powershell. It has no clue about your python.pythonPath settings in your VS Code.

How to force using the 32-bit Python 3.7?

You can create a virtual environent with python 3.7-32bit using

py -3.7-32 -m venv 32_bit_env

Or, the full filepath to the 32-bit v.3.7. python.exe, assuming Powershell (hence &):

& "C:somepathPython 3.7 32-bitpython.exe" -m venv 32_bit_env

You can check the full filepath with py -0p, if you need to. Then, you can then make this virtual environment to be automatically activated in new VS Code integrated terminals, by editing the settings.json:

{
    "python.pythonPath": "C:/tmp/someproj/my_venv/Scripts/python.exe"
}

Note that every in the entry has to be replaced with either \ or /. The python.pythonPath has to be absolute.

Answered By: np8

I downloaded the latest python 3.11.2 when I tried to establish the workspace for it in VS Code the top version of python it would accept was 3.10, so It looks like I will be uninstalling it again. and will have to reinstall vsn 3.10 (tick path and all the other options) then change the system environment variables accessed from the search box, as other sources had advised. I am ignoring all this stuff about Pandas and numpty etc. that came when I had to install annaconda . I hope Pythons own pip will give me all the packages I need, rather than using the packages from this huge bloated annaconda monster with its Pandas and numpty ,etc. I am just a begginer on these new languages

Answered By: Trevor's Carnival