pythonenvironment

How do I properly use my python environments?


So I have a kinda clumsy python environment like this: My Python environment in VScode

I feel like it's really messy and I just want one environment that's the default, and that should be whatever latest version of python there is. In the past I would just download the latest from python.org but I kinda want to automate that and just be like a brew update or smth. How can I delete my old python environments, set a new global one thats on python 3.12.2, and how should I install/upgrade py environments moving forward?

I tried looking on Stack Overflow for other answers and there was something called pyenv but that didn't really work out that well so I decided to give up on it.


Solution

  • “how can I delete my Python environments”.

    If you want to remove the interpreter and delete it from your system, I just verified this right now on Mac OSX. If you want to remove a Python Interpreter from the list, choose shift + command + p. Then choose “Python: Select Interpreter”. This will show that it is now the active interpreter appear on the bottom right of the VSCode Window. Now launch the terminal within vscode. Remove the interpret from your system using the shell (e.g. rm [path-to-interpreter]. Close VSCode and then reopen VSCode. And now return back to “Python: Select Interpreter” and you will see the Interpreter no longer appears there. I just verified this right now.

    “how can I properly use my Python environments”

    Aside from adding an Interpreter using shift + command + p -> Python: Select Interpreter: -> +Create Virtual Environment and then specify the path to the bin/python3 of your given virtual environment, I recommend using the .vscode launch.json to get both interpreter AND debugger running. This will both allow you to specify a given interpreter as well as allow you to debug within vscode using that interpreter. For example you have have this configuraiton in your .vscode launch.json:

    {
      "name": “My APP",
      "type": "python",
      "python": "/Users/me/projects/ai/myapp/venv/bin/python3",
      "request": "launch",
      "program": "${workspaceFolder}/ai/myapp/main.py",
      "cwd": "${workspaceFolder}/ai/myapp",
      "console": "integratedTerminal",
      "justMyCode": false,
      "envFile": "${workspaceFolder}/ai/myapp/.env"
    },
    

    Here you are giving a name to your configuration. You are specifying the type. Note “python” is being deprecated and the documentation suggests using debugpy. Importantly, you are specify the “python” property which points to python3 executable in your venv path. “program” allows you to define the actual path to your script. “cwd” as the name suggests changes the current working directory to your directory that holds the pythonn script. “console” I usually use integratedTerminal. As the docs say, "If redirectOutput is set to True, output is also displayed in the debug console.”. “justMyCode” means do you only want to debug your code or also debug the modules you are using in your code, such as modules you installed via pip3. “envFile” is convenient as it allows you to execute the script with specified environment variables.