How to permanently unset PYTHONHOME environment variable in Visual Studio 2022?

Question:

I cannot launch Python in Visual Studio because I am unable to unset the PYTHONHOME environment variable, which is permanently set to C:Python311 even after getting rid of it through windows control panel system environment editor, uninstalling Python311, and a full repair and reinstallation of Visual Studio.

Problem

I have tried to unset PYTHONHOME through the usual Windows control panel system environment variable editor (locally and globally), but it does not solve the problem.

If I open a new PowerShell outside of Visual Studio and type echo $ENV:PYTHONHOME, the terminal outputs nothing, which is what I want.

However, when I type echo $ENV:PYTHONHOME in Visual Studio, it outputs C:Python311. Why is it so? I tried to uninstall this Python version, but it doesn’t solve the problem. I’ve also tried a full reinstallation of Visual Studio, but Visual Studio’s powershell seems to load its own environment variables on top of the ones from Windows.

Question

What should I do to permanently get rid of PYTHONHOME in Visual Studio?

Asked By: chckx592

||

Answers:

You may need to edit the .vcxproj file directly. The .vcxproj file is just an XML file so can be edited with a text editor. Open it up and search for "pythonhome".

Make sure to see which other lines in that file are trying to use PYTHONHOME and edit those accordingly.

Answered By: Robert Cheatham

The environment variables have three scopes:

  1. Machine
  2. User
  3. Process

The environment variables follow the hierarchy Machine -> User -> Process, where the children overwrite the values of the environment variables inherited from the parents.

PYTHONHOME was in the "Process" scope and pertained to the Visual Studio environment.

To permanently clear PYTHONHOME from Visual Studio, open a Developer PowerShell in Visual Studio and execute the following command.

[Environment]::SetEnvironmentVariable("PYTHONHOME", "")

This should permanently clear PYTHONHOME from the current process.

Answered By: chckx592