[Python] How to automate Python venv configuration for VS Code workspace?

Discussão em 'Python' iniciado por Stack, Maio 9, 2025.

  1. Stack

    Stack Membro Participativo

    I have multiple VS Code workspaces for different projects. Each of these workspaces has a dedicated virtual environment.

    I want to write a Python script to create new venvs in bulk, then assign each venv to its workspace/project by modifying the settings.json file.

    The reason for doing this is because I like to refresh the venvs regularly, and have different versions of Python for different projects. I can also automate running pip install -r requirements.txt for each project. Doing this manually is quite an overhead and a script to automate this is easy.

    But, I'm struggling with VS Code. So far this is what I have in the workspace settings.json file:

    {
    "python.defaultInterpreterPath": "/home/user/venv/devvenv/venvname/bin/python3.13",
    "python.terminal.activateEnvironment": true
    }


    When I open VS Code and run an .ipynb notebook, I am asked for an interpreter. It does give me the option of the specified venv.

    When I open a Terminal, no venv is activated.

    When I run a .py file, VS Code uses the OS (Ubuntu) default interpreter.

    How can I automate VS Code settings so that:

    • .ipynb notebooks automatically use the correct venv
    • Terminal automatically activates the correct venv on open
    • .py files are executed with the correct venv

    Continue reading...

Compartilhe esta Página