-1

Python Terminal

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

1
  • 1
    In order to use your virtual environment you need to run the Activate script. You can then install any libraries, modules etc., that you want to use in that specific environment. When you have finished and want to return to "normal" processing you run the deactivate script. It is only necessary to run in a virtual environment if you want to preserve a certain set of libraries in your project. There is nothing to stop you using all the globally installed libraries in parallel. Commented Aug 7, 2024 at 12:21

2 Answers 2

2

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

Screenshot IDE

Here is where things get interesting:

  • In VS Code, select env2 as the interpreter (bottom right corner)
  • In your terminal, activate 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
Sign up to request clarification or add additional context in comments.

1 Comment

thank you this is a really good example that clarified what I was thinking
0

Go to

File --> Settings --> Project --> Python Interpreter

here from dropdown you can select you local interpreter or you can select existing virtual environment.

If you want to test newer version or older version or some new package without modifying existing setup you can select virtual environment else local python setup.

Same will be asked when you create a new project , you can create a new virtual environment , use local setup or use existing virtual environment

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.