-1

New to utilizing virtual environments. I understand to concept but not the complete execution within vscode.

I have read the vsCode guide on using Python venvs and the vscode guide on getting started with python

I'm not understanding the link between how the Terminal window displays its in the venv and how vscode shows its using the selected virtual interpreter. I want to make sure I know which one I am working in so that I can effetely manage packages.

These two images demonstrate my point. They both show me that vscode is using the .venv interpreter based on the blue region pointed by the red arrow. But the first image shows the Terminal is in the venv while the second image shows the Terminal is in the venv. This is confusing to me because when I use "pip" to install via the terminal I want to make sure its downloading the package to the right venv and not to global. I would assume if vscode is using the venv interpreter the Terminal should also default to the venv.

Do you have to just run the .\venv\Scripts\Activate.ps1 in the Terminal anytime you want to run "pip" to add a new package in your venv?

vscode_in_the_venv_but_term_is_not vscode_AND_term_in_venv

Tim51
  • 141
  • 3
  • 13
  • 3
    an env must be activated to add packages to it. Note, I **always** use `python -m pip install ` rather than just pip. – JonSG Feb 14 '23 at 19:58
  • 1
    For your terminal, pay attention to what it says in the terminal (the "yellowish/greenish" arrows in your images). VSCode will activate and use your "selected environment" when you launch the program if you use its Run features - but your terminal may not be in that environment, especially when you first open the folder. – topsail Feb 14 '23 at 20:11

1 Answers1

0

When the virtual environment name is displayed in front of your terminal directory, it means that your current terminal is using the activated virtual environment. At this time, directly using pip command will use the pip.exe in the virtual environment, and the installed package will also be installed in the virtual environment.

If the virtual environment is not activated, if your current terminal does not display the name of the virtual environment, directly using the pip command may use the pip.exe in the global environment, that is, the python version configured by the computer environment variable, and the package will also be installed under it.

If you're worried, you can use the full install command. For example, the following command uses the complete python interpreter path to ensure that the sklearn package is installed under the virtual environment .venv under folder py12.

e:\workspace\py12\.venv\Scripts\python.exe -m pip install sklearn

PS: If you choose a virtual environment interpreter, then creat a new terminal will automatically activate the environment. This is controlled by the setting below, which defaults to true.

    "python.terminal.activateEnvironment": true,

enter image description here

JialeDu
  • 6,021
  • 2
  • 5
  • 24