VS Code 在解释器上找不到虚拟环境,但在集成终端上可以找到
我的 Linux 机器上安装了一个完全可以工作的虚拟环境。 此 venv 可以由 VS 代码中的终端调用 source /mypath/venv/bin/activate
定期使用。
问题是 VS Code 中的 Python 解释器无法访问虚拟环境中的任何包,尽管按照大多数指南中的说明在解释器上设置了路径。
我决定在 .vscode
文件夹内的 settings.json
文件中手动设置路径,如下所示:
{
"python.pythonPath": "/mypath/venv/bin/python3.8"
}
venv 仍然无法通过解释器访问。还有其他建议吗?
I have a fully working virtual environment installed on my Linux machine.
This venv can be regularly used by the terminal in VS code calling source /mypath/venv/bin/activate
.
The problem is that the Python interpreter in VS code cannot access any of the packages in the virtual environment, despite setting up the path on the interpreter as described in most of the guides.
I decided to manually set up the path in the settings.json
file inside the .vscode
folder as follows:
{
"python.pythonPath": "/mypath/venv/bin/python3.8"
}
venv is still not accessible through the interpreter. Any other suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会回答我自己的问题。
原来我的
pip
安装指向的路径(标准/home/username/.local/bin/pip
)与我的venv
不同> 目录(/my_path/venv/bin/pip
)。您可以通过执行命令
which pip
来显示路径。在我的具体案例中,当我第一次设置 Linux 机器时,出现了一些混乱,这意味着 venv 仅安装了少量软件包,而目录包含 Python 库并实际使用是
pip
路径。换句话说,激活 venv 没有任何区别,因为 Python 库是从 pip 路径加载的。因此,首先我必须通过修改
/ 中的
,替换.bashrc
文件来确保pip
必须指向我的venv
文件夹。 home/username/为
All I had to do after is re-installing every required packages in the newer
venv
(生成requirements.txt
来自旧pip
路径的文件帮助)。然后我在 VS Code 解释器中选择了 venv 路径,现在一切正常。
I will answer my own question.
Turned out my
pip
installation was pointing to a path (standard/home/username/.local/bin/pip
) which was different from myvenv
directory (/my_path/venv/bin/pip
).You can display the path by executing the command
which pip
.In my very specific case, there was some mix-up when I first setup my Linux machine, meaning that
venv
only had a small amount of packages installed, while the directory containing the Python libraries and actually being used was thepip
path. In other words, activatingvenv
did not make any difference, since the Python libraries where loaded from thepip
path.So, first I had to ensure that
pip
had to point to the myvenv
folder, by modifying the.bashrc
file in/home/username/
, replacingwith
All I had to do after was re-installing each of the required packages in the newer
venv
(generating arequirements.txt
file from the olderpip
path helped).Then I selected the
venv
path in the VS Code interpreter and everything is working fine now.您实际上不需要 settings.json 文件。
您可以尝试删除 .venv 文件夹并通过
python -m venv .env
创建一个新文件夹似乎 VS Code 已将某些内容从 .venv 更改为 .env。我不知道为什么。
执行 python -m venv .env 后,在 vs code 中打开终端,它将激活您的 .env。
您可以(如果您冻结了 pip 安装)执行
pip install -rrequirements.txt
就可以了。You actually do not need the settings.json file.
You could try to remove the .venv folder and create a new one by
python -m venv .env
It seems that vs code have changed something from .venv to .env. I'm not sure why.
After doing
python -m venv .env
open the terminal in vs code and it will active your .env.You could (if you froze your pip installations) do a
pip install -r requirements.txt
and you are all good to go.“python.envFile”设置的默认值为“${workspaceFolder}/.env”,将其更改为“${workspaceFolder}/.venv”并重新启动 vscode。
The default for the "python.envFile" setting is "${workspaceFolder}/.env" change it to "${workspaceFolder}/.venv" and restart vscode.