Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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-32\python.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 Files\Python37\python.exe as shown in the pyvenv.cfg:

home = C:\Program Files\Python37

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

like image 897
Chris T Avatar asked Oct 17 '25 13:10

Chris T


1 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 .vscode\settings.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:\some\path\Python 3.7 32-bit\python.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.

like image 169
np8 Avatar answered Oct 20 '25 04:10

np8



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!