简体   繁体   中英

vscode python linux: how to automatically open terminal in right venv?

every time I open a vscode workspace I get a terminal the isn't attached to the right venv, although I did put the right python path in ws.code-workspace.json :

{
    "folders": [
        {
            "path": "."
        }
    ],
    "settings": {
        "python.pythonPath": ".venv/bin/python3"
    }
}

is there a way to automatically get the terminal running in the right venv?

Your steps to detect the venv path are correct, but: The right answer is that currently the terminal isn't capable to auto open with the right venv- only the second opened terminal will detect the venv and open it automatically.

UPDATE : there is a new flag that one can use: "python.terminal.activateEnvironment": true

If you want to use a virtual Python environment within VSCode you need to specify the absolute path to the corresponding interpreter.

Just add the following line to your workspace settings:

"python.pythonPath": "absolute/path/to/venv/bin/python"

Now open a Python file and click on Terminal -> New Terminal and VSCode will automatically activate the virtual env as soon as the terminal launched.

As you see in the screenshot below, when opening a terminal the command

$ source /.../bin/activate

is executed automatically.

在此处输入图像描述

Note : Before opening a new terminal there be a Python file already to make VSCode activate the virtual env specified in workspace-settings.有一个 Python 文件已经于使 VSCode 激活工作空间设置中指定的虚拟环境。 If instead you have a file focused which is not .py (for example a .json file), it will just open a new default terminal.

最新设置是"python.terminal.activateEnvInCurrentTerminal": true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM