Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't automatically activate virtual environment in vscode

I'm trying to automatically activate the virtual environment .venv in the terminal whenever I open the VS Code workspace. I have Python installed via pyenv.

I tried adding the following in both the user settings JSON file and the workspace settings JSON

"python.terminal.activateEnvInCurrentTerminal": true,
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",

but that did not work, I still need to run source .venv/bin/activate in the terminal whenever I launch the workspace to activate the virtual environment

I use pip -V in the terminal to verify which Python interpreter is being used like follows: enter image description here

I want pip -V to point to my virtual environment every time I open up VS Code without the need to run source .venv/bin/activate

like image 769
ZZZ Avatar asked Nov 01 '25 19:11

ZZZ


2 Answers

Attempt this sanity check first by closing VS Code, opening a terminal, and executing the following (I'm assuming you use poetry to create the venv environment, if not, use your method):

mkdir test
cd test
poetry new .
poetry env use "$(which python3)"
poetry install
touch ./test/main.py
code .

This should open VS Code into the newly created project. In the Explorer, expand the "test" folder and open main.py

Assuming you have Microsoft's Python Extension installed, you should receive a message like this, assuming you haven't previously clicked "Don't show again":

enter image description here

You should also see some details about the .venv environment in the status bar at the bottom: enter image description here

This is BEFORE you open a terminal.

If you did not at least see the status information, verify that you have Microsoft's Python Extension installed and that it hasn't somehow become disabled (I know, I know, but I have to say this). Maybe even uninstall and re-install just for completeness sake.

Otherwise:

CMD + Shift + P to open the Command Palette, type in "Preferences: Open Default Settings (JSON)", and hit Enter Ctrl + f to open the little find dialog box, type in venv,and hit Enter

Verify your settings mirror these:

    // Folders in your home directory to look into for virtual environments (supports pyenv, direnv and virtualenvwrapper by default).
    "python.venvFolders": [],

    // Path to folder with a list of Virtual Environments (e.g. ~/.pyenv, ~/Envs, ~/.virtualenvs).
    "python.venvPath": "",

If everything looks okay, there's one last place to look and that's within the extensions' own settings:

Go to Extensions, select "Python", click the little gear icon, and choose "Extension Settings".

Look for "Python -> Terminal: Activate Environment" and make sure that is enabled. Also look for "Python: Venv Path" and make sure that is blank.

Hopefully you're reached this point and fixed the misconfiguration if there was one, or maybe this all worked out of the gate and the problem only seems to be with that one project. If so:

Close VS Code, open a terminal, and cd to ~/Library/Application Support/Code/User/workspaceStorage

Each of the folders here hold settings for each of the workspaces you've opened. It's where VS Code stores most of the state information about the code, outside of the .vscode/settings.json file. If you do a ls -latr, it should sort them by date with the newest at the bottom -- this will help you with the following:

Use cat to print out the workspace.json file in some of the newer folders to find which is for your project -- You'll know when you've found the right folder when you find the file with a path to your troubled workspace.

Rename whatever folder that is to something else -- like "backup". The next time you open VS Code and open that workspace, VS Code should see it for the first time again and configure all of the default settings for it. See if the venv detection works now.

like image 192
Mr. Man Avatar answered Nov 04 '25 08:11

Mr. Man


This setting works if you have created virtual environments for each workspace. But for the python executable in the virtual environment folder, the path should be "${workspaceFolder}\\.venv\\Scripts\\python". there is no bin folder in the virtual environment folder.

Also, in the previous update, a new terminal automatically activates the environment, but the environment name is not displayed in front of the terminal directory.

enter image description here

like image 43
JialeDu Avatar answered Nov 04 '25 07:11

JialeDu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!