Visual Code how can I activate the virtual environment automatically?

Question:

Maybe a simple question, but I cannot figure it out. I work within Visual Studio Code on a MacBook. Within a directory I create a virtual environment and have activated that with source .venv/bin/activate scripts and everything works fine.
In the terminal I see (.venv) (base) gaston@MacBook-Pro WebScraping %

When I start visual code another time and open this same directory I do not see the (.venv) so I again give manually the command to activate.

My question is there a way to activate the virtual environment automatically when opening the directory?

Asked By: matje59

||

Answers:

VS Code Default Python Interpreter Path

You can do this in a number of ways. If your goal is to have the virtual environment selected immediately at the launch of VS Code, you will have to target that virtual environment as the Python Interpreter in VSC.

https://code.visualstudio.com/docs/python/environments

System Preferences->Settings->Python: Default Interpreter Path

You’ll want to point it to your virtualenv Python installation, likely something like:

envFolder/bin/python3

VS Code Launch Configuration Python Interpreter

You can also set a specific launch configuration so that VSCode will still use your system Python instance by default; however, specific launch configurations will have the virtual environment specified.

https://code.visualstudio.com/docs/python/debugging

Answered By: kylepapili

I did something within settings and now it works.
When I open VSC now it opens the directory and the right environment, just the way I wanted it.

Took me some while to figure what finally did the trick:
I opened settings Command + , typed env in the search box. Scrolled through the results until I saw:

Python > Terminal: Activate Env In Current Terminal

and checked the checkbox. That solved my problem.

Answered By: matje59
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.