小能豆

Python environment broken in Visual Studio 2022

py

My Python environment in Visual Studio 2022 is completely broken due to a past misconfiguration, and I try to find ways to debug it.

When I launch Python through PowerShell from desktop, I have no problem.

However, when I launch Python from Visual Studio 2022, it automatically sets the environment variable PYTHONHOME to “C:\Python311,” which was an old folder that I deleted and don’t use anymore. Due to this, my Python environment is broken across all projects.

I have tried to do the following without success:

  1. Delete PYTHONHOME in both “User” and “System” global system environments.
  2. Delete everything related to C:\Python311 environment in regedit.
  3. Do [System.Environment]::SetEnvironmentVariable("PYTHONHOME", "", [System.EnvironmentVariableTarget]::Machine) in the PowerShell launched from Visual Studio.
  4. Delete any ps1 scripts that could set PYTHONHOME.
  5. Find anything related to PYTHONHOME in C:\Users\username\AppData\Local\Microsoft\VisualStudio
  6. Reinstall Visual Studio with default configuration.
  7. Reset all user config files with devenv.exe /ResetSettings (see here)
  8. Look into the logs (see here).
  9. Be assisted by chatGPT.

My understanding is that Visual Studio uses a misconfigured file defined somewhere in my system when launching Python, which automatically sets PYTHONHOME=”C:\Python311”. I would like to know where this script file is so that I can reconfigure PYTHONHOME to null. It is not project-specific but global.

If you need more information, please let me know; I will update my question accordingly.


阅读 77

收藏
2023-12-17

共1个答案

小能豆

In Visual Studio, the Python environment can be configured through various mechanisms, and it might be challenging to locate the exact cause of the misconfiguration. Here are some additional steps you can try:

  1. Check Visual Studio Python Environments:
  2. Open Visual Studio.
  3. Go to “View” > “Other Windows” > “Python Environments.”
  4. Check if the desired Python version is listed and set as the default.
  5. If there’s an outdated or incorrect Python environment, you can remove it.
  6. Visual Studio Settings:
  7. Check if there are any user-specific settings in Visual Studio that might be affecting the Python environment.
  8. Open Visual Studio settings (Tools > Options), navigate to “Python” settings, and review the configurations.
  9. Python Integration Scripts:
  10. Visual Studio uses scripts during startup to set up the Python environment.
  11. Check if there are any custom scripts in Visual Studio’s startup that might be setting PYTHONHOME.
  12. Look for Python-related initialization scripts in Visual Studio installation directories.
  13. Project Configurations:
  14. Check if there are any project-specific configurations that might be overriding the global settings.
  15. Open the project file or solution file in a text editor and look for any Python-related configurations.
  16. Visual Studio Extension:
  17. If you have any Python-related extensions installed in Visual Studio, check their configurations.
  18. Some extensions might override or set specific Python environments.
  19. User-Specific vs System-Wide Settings:
  20. Confirm that you are modifying user-specific settings if the misconfiguration is user-specific. Check both user and system environment variables.
  21. Restart Visual Studio:
  22. After making changes, restart Visual Studio to ensure that the new configurations take effect.
  23. Visual Studio Developer Command Prompt:
  24. Launch the “Visual Studio Developer Command Prompt” and check if there are any scripts or configurations setting PYTHONHOME.
  25. Check for Startup Scripts:
  26. Look for any startup scripts in Visual Studio’s startup directory that might be affecting the Python environment.
  27. Check for VSCode Settings:

  28. If you are using Visual Studio Code alongside Visual Studio, check its settings for Python-related configurations.

Remember to save any changes and restart Visual Studio after making modifications. If the issue persists, you might consider contacting Microsoft Support for assistance, as they can provide more specific guidance based on your system and Visual Studio configuration.

2023-12-17