Search code examples
pythonvisual-studio-codevirtual-environment

I want to ask a question about Python virtual environment


What happens when you activate a virtual environment named Foldenv in the terminal of python vscode and then use a virtual environment named venv in the palette?

enter image description here

i enter print ("hello world") in venv interpreter but terminal show me (Foldenv) in front of hello world


Solution

  • Python will use the python interpreter in the lower right corner (the one you selected in the Select Interpreter panel) to run the code.

    But it doesn't activate another virtual environment again when there is already a terminal and activate a virtual environment. Unless you close the current terminal and run the code.

    enter image description here

    Another thing to note is that when you use the button to execute code in a terminal, then this terminal will always exist as the terminal for executing code. The following two cases are examples for your understanding.

    The first case

    Select the Foldenv environment interpreter and activate the environment in the terminal. Note that no code is running at this time.

    enter image description here

    Switch the interpreter to .venv in the Select Interpreter panel, Use the play button to execute the code. At this time, a new terminal will be created automatically and the .venv environment will be activated to run the code.

    enter image description here

    The second case

    Select the Foldenv environment interpreter and execute the code directly. A terminal is automatically created and the environment is activated at this point.

    enter image description here

    Switch the interpreter to .venv and run the code again. At this time, the code will be executed directly in the current terminal, but the .venv environment interpreter will be used.

    enter image description here