can't automatically activate virtual environment in vscode

280 views Asked by At

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

3

There are 3 answers

4
JialeDu On

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

0
user23403909 On

You can create a custom task that will run source .venv/bin/activate whenever you open a folder.

To do that you can go File -> Preferenses -> User Tasks and there add a new custom task like this:

    {
        "label": "Virtual Environment Activation",
        "type": "shell",
        "command": "source",
        "args": [
            ".venv\\Scripts\\python",
        ],
        "runOptions": {
            "runOn": "folderOpen"
        }
    },

Now, the task will automatically be sent to the shell whenever you open a folder. You can obviously tweak this task according to your own preferences, but this simple one should do the job.

Also, note that your "args": might need to be .venv/bin/activate with single slashes instead of \\. I just don't know how it better works on Mac

0
Mr. Man On

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.