Home > Back-end >  Visual Code how can I activate the virtual environment automatically?
Visual Code how can I activate the virtual environment automatically?

Time:07-24

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?

CodePudding user response:

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

CodePudding user response:

Thanks, 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.

  • Related