So a question I can't find a clear answer to is if my python interpreter that I have selected (where it shows 3.12.5('.venv':venv) is set that way because I selected to create a virtual environment.
Do I still need to activate the virtual environment every time I use it or since it is already what appears to be preselected, would I not need to?
From what I can tell it still downloads to the global environment but I'm also relatively new to coding in general and setting up my IDE's so I was hoping to get a better understanding of this in particular. I recently realized I was installing all my packages globally and would like to avoid it in the future
Let's walk through an example to illustrate this.
In a new project directory, we create two virtual environments:
py -m venv env1
py -m venv env2
We install pandas in env1
and numpy in env2
:
env1/scripts/activate
pip install pandas
env2/scripts/activate
pip install numpy
We also create a python script where we import pandas
Here is where things get interesting:
env2
as the interpreter (bottom right corner)env1
What you select in the bottom right corner, is the interpreter used by VS Code. As such, it for example tells you it is not able to import pandas, as shown by the yellow lines from the linter.
If you now hit run in the top right corner, you will get the following error, because it will use the interpreter selected in VS Code (env2
):
Traceback (most recent call last):
File "c:\Code\test1.py", line 1, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
However, if we run the script in the terminal (py test1.py
) it will work just fine as it is using env1
:
Hello env1