本文介绍了如何使我的vscode终端将venv用于python,而不是系统版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 python -m venv venv 设置了venv,vscode将其识别为有效的解释器,并使用代码运行器vscode扩展名按预期运行我的代码.但是,即使在运行 source/Fake/path/to/env/bin/activate 之后,vscode集成的bash终端也仅使用python的系统版本.终端在输入提示上显示(env),但通过 os.path.dirname(sys.executable)检查版本,显示python的版本是我的版本系统路径,而不是虚拟路径中的路径.因此,我正在安装的软件包将安装到系统python而非venv.

I've set up a venv using python -m venv venv, which is recognised by vscode as a valid interpreter and runs my code as expected using the code-runner vscode extension. But the vscode integrated bash terminal only uses the system version of python, even after running source /Fake/path/to/env/bin/activate. The terminal shows (env) on the input prompt but checking the version through os.path.dirname(sys.executable) shows that the version of python is the one in my system path rather than the one in the venv. So packages I'm installing are being installed to system python rather than the venv.

这是vscode中的终端而不是venv本身的问题,因为当我在本机终端(非集成)中执行上述步骤时,我获得了venv python的正确路径.

This is a problem with the terminal in vscode rather than with the venv itself, as when I follow the above steps in a native terminal (non-integrated) I get the correct path to the venv python.

我尝试将vscode中的 python.venvPath 变量设置为 $ {workspaceFolder}/env fake/path/to/env ,但均无效.我认为可能可以在 terminal.integrated.shellArgs.macos 设置下设置bash shell参数,但是还无法解决如何在启动时运行正确的激活脚本的问题.作为参考,我当前的vscode设置如下:

I've tried setting the python.venvPath variable in vscode to either ${workspaceFolder}/env or to fake/path/to/env but neither works. I thought it might be possible to set bash shell arguments under the terminal.integrated.shellArgs.macos setting but haven't been able to work out how to get it to run the correct activate script on startup.For reference, my current vscode settings are below:

"settings": {
    "code-runner.fileDirectoryAsCwd": true;
    "code-runner.executorMap": {
        "python": "env/bin/python3",
    },
    "python.venvPath": "${workspaceFolder}/env",
    "terminal.integrated.cwd": "${workspaceFolder}",
    },
}

基本上,我希望集成终端激活venv activate bash脚本并临时替换该路径,以将venv python用作默认设置.然后,我将能够使用pip3将相关的软件包安装到venv而不是系统python.但是尽管提示在输入之前显示(env)的提示,但实际的行为是在发出命令时它指向错误的python版本. python3 pip3 .

Basically, I'm expecting the integrated terminal to activate the venv activate bash script and temporarily replace the path to pick up my venv python as the default. I would then be able to use pip3 to install relevant packages to the venv rather than to the system python. But despite the prompt displaying (env) before the entry the actual behaviour is that it's pointing to the wrong python version when issuing commands e.g. python3 or pip3.

任何建议都会很棒!

推荐答案

我遇到了同样的问题,尝试了各种方法,但是最终还是有效.
1. python -m venv myvenv
2.打开命令面板,然后选择解释器myenv
3. CTRL + SHIFT +`在myenv
中打开终端然后我就能看到预期的...(myenv)C:\ Git \ pyPilot>

I had the same issue, tried all kinds of things but this ended up working.
1. python -m venv myvenv
2. Open command palette and select interpreter myenv
3. CTRL + SHIFT + ` to open terminal in myenv
and then I was able to see the expected... (myenv) C:\Git\pyPilot>

这篇关于如何使我的vscode终端将venv用于python,而不是系统版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 07:13