【问题标题】:How to auto-activate the environment after the start in version 1.56?1.56版本启动后如何自动激活环境?
【发布时间】:2021-05-14 06:55:17
【问题描述】:

有什么方法可以在 VS Code 1.56 中自动激活环境?

之前,我使用以下工作区设置来自动激活终端中的环境:

"terminal.integrated.shellArgs.linux": [
    "/path/to/activate.sh",
],

它在启动后立即激活环境。

这些设置在最新版本中已被弃用:

https://code.visualstudio.com/updates/v1_56#_terminal

我尝试使用新设置:

"terminal.integrated.profiles.linux": {
    "bash": {
        "path": "bash",
        "args": [
            "/path/to/activate.sh",
        ],
    },
},

不幸的是,它仅适用于新终端,或者当我在启动后打开不同的选项卡并在 VS Code 完全加载后切换到终端时。

有什么方法可以立即激活环境吗?

【问题讨论】:

    标签: python visual-studio-code


    【解决方案1】:

    这在我的settings.json 中对我有用:

        "terminal.integrated.defaultProfile.linux": "zsh",
        "terminal.integrated.profiles.linux": {
            "bash": {
                "path": "bash"
            },
            "zsh": {
                "path": "zsh",
                "args": ["-l"]
            },
            "fish": {
                "path": "fish"
            },
            "tmux": {
                "path": "tmux",
                "icon": "terminal-tmux"
            },
            "pwsh": {
                "path": "pwsh",
                "icon": "terminal-powershell"
            }
        },
    

    确保将“args”键添加到您正在使用的 shell。就我而言,它是 zsh,所以我添加了“-l”参数来加载我的 .zprofile 脚本。

    希望对您有所帮助! :)

    有关终端配置文件 Confs 结帐的更多详细信息:https://code.visualstudio.com/updates/v1_55#_terminal-profiles

    【讨论】:

    • 它在 VS Code 1.56 中对你有用吗?通过工作,我的意思是它是否在启动时被激活。当我打开一个新终端时,该语法对我有用,但如果终端在启动时打开,它将不会激活环境。 Make sure to add the "args" key to the shell you are using. |我使用这个键,如我提供的示例所示。
    • 是的,它在启动时起作用。但是如果你想激活一个venv,你需要添加:"python.terminal.activateEnvInCurrentTerminal": true,,对于python,添加:"python.venvPath": "./<venv_name>"到设置文件。注意还要确保你选择了正确的interperter
    • 我当前的 VS Code 版本是 1.56.2
    • 我尝试添加“python.terminal.activateEnvInCurrentTerminal”,但它没有改变任何东西。我已经指定了“python.venvPath”,尽管有警告“此设置只能应用于本地窗口的用户设置或远程窗口的远程设置。”。是否在用户设置或工作区设置中配置了环境?
    • 我正在使用用户设置,但如果设置,工作区设置应该胜过用户设置。因此,请确保在两种设置上保持一致。我有一个定义的结构,我总是遵循我的项目,这就是我使用用户设置的原因。
    【解决方案2】:

    当您打开项目或打开另一个终端时,要“自动”激活 python venv 并在集成终端上加载 .env 文件,请确保您已设置以下设置:

    用户设置界面

    Python: Env File
    ${workspaceFolder}/.env
    Python>Terminal: Activate Env In Current Terminal
    True
    Python>Terminal: Activate Environment
    True
    
    

    确保 venv 位于项目文件夹的根目录下,例如:

    ./MyProject (workspaceFolder)
    ./MyProject/venv
    ./MyProject/.env
    ./MyProject/djangoproject
    ./MyProject/djangoproject/[djangoapps]
    ./MyProject/djangoproject/manage.py
    ./MyProject/setup.py
    ./MyProject/requirements.txt
    

    如果失败,请尝试将工作区设置设置为:

    "python.pythonPath": "./venv/bin/python",
    "python.terminal.activateEnvInCurrentTerminal": true,
    

    作为参考,这里是我当前的设置:

    用户设置.json:

    {
        "python.sortImports.path": "./venv/bin/python",
        "terminal.integrated.defaultProfile.linux": "zsh",
        "python.terminal.activateEnvInCurrentTerminal": true,
        "terminal.integrated.profiles.linux": {
            "bash": {
                "path": "bash"
            },
            "zsh": {
                "path": "zsh",
                "args": ["-l"]
            },
            "fish": {
                "path": "fish"
            },
            "tmux": {
                "path": "tmux",
                "icon": "terminal-tmux"
            },
            "pwsh": {
                "path": "pwsh",
                "icon": "terminal-powershell"
            }
        },
        "terminal.integrated.profiles.osx": {
            "bash": {
                "path": "bash"
            },
            "zsh": {
                "path": "zsh",
                "args": ["-l"]
            },
            "fish": {
                "path": "fish"
            },
            "tmux": {
                "path": "tmux",
                "icon": "terminal-tmux"
            },
            "pwsh": {
                "path": "pwsh",
                "icon": "terminal-powershell"
            }
        },
    }
    

    工作区设置.json:

    {
        "python.pythonPath": "./venv/bin/python",
        "python.terminal.activateEnvInCurrentTerminal": true,
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      • 2022-07-03
      • 1970-01-01
      相关资源
      最近更新 更多