jim70
jim70

Reputation: 615

VS Code integrated terminal not starting with virtual environment activated while the right interpreter is being selected

Platform and software versions:

Mac OS Mojave, VS Code 1.38.0, Python extension installed. Created virtual environment in project directory using command python3 -m env. Modified setting in Python extension, "python.venvPath": "bin", to handle the bin directory where the python for the virtual environment is stored.

Situation:

When I launch VS Code using code ., and then open a python file in the folder, the interpreter selected is ./bin/python, however the integrated terminal is not set to the right python executable. If I launch a new terminal it sources the virtual environment (which may be due to the Python extension setting "python.terminal.activateEnvironment": true)

Question:

Is there a way to have the integrated terminal also have the virtual environment sourced?

Or is there a better way to have VS Code activate virtual environment created by python3 -m env .?

Thank you.


Edit:

Just reread the VS Code documentation here - https://code.visualstudio.com/docs/python/environments and this time noticed this below. Wondering if there is a way to kill the existing terminal and then launch one upon VS Code launch...

However, launching VS Code from a shell in which a certain Python environment is activated does not automatically activate that environment in the default integrated terminal. Use the Terminal: Create New Integrated Terminal command after VS Code is running.

Upvotes: 0

Views: 2282

Answers (2)

codekoriko
codekoriko

Reputation: 870

At the time of this writting vscode now has

"python.terminal.activateEnvInCurrentTerminal": true,

so I my global settings.json, F1 > preference: Open Settings (JSON)

"python.venvPath": "D:/miniconda3/envs",
"python.terminal.activateEnvInCurrentTerminal": true,

and in my workspace's settings.json, F1 > preference: Open Workspace Settings (JSON)

"python.defaultInterpreterPath": "D:/miniconda3/envs/my-workspace-venv/python.exe"

And it works for me.

Upvotes: 2

Brett Cannon
Brett Cannon

Reputation: 16000

Two things. One, "python.venvPath" is meant to point at a directory that contains other virtual environments, not the bin/ directory that has a Python interpreter from a virtual environment. (I also don't know what python3 -m env is supposed to do; did you mean python3 -m venv?)

Two, there isn't a way to make VS Code automatically launch and complete the loading of the Python extension before VS Code creates a terminal if you have the terminal frame open at start-up.

Upvotes: 2

Related Questions