VS Code 在解释器上找不到虚拟环境,但在集成终端上可以找到

发布于 2025-01-13 03:21:41 字数 383 浏览 1 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事与诗 2025-01-20 03:21:41

我会回答我自己的问题。
原来我的 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/,替换

export PYTHONPATH=/home/username/.local/lib/python3.8
export PATH=/home/username/.local/bin:$PATH

export PYTHONPATH=/my_path/venv/lib/python3.8/
export PATH=/my_path/venv/bin:$PATH 

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 my venv 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 the pip path. In other words, activating venv did not make any difference, since the Python libraries where loaded from the pip path.

So, first I had to ensure that pip had to point to the my venv folder, by modifying the .bashrc file in /home/username/, replacing

export PYTHONPATH=/home/username/.local/lib/python3.8
export PATH=/home/username/.local/bin:$PATH

with

export PYTHONPATH=/my_path/venv/lib/python3.8/
export PATH=/my_path/venv/bin:$PATH 

All I had to do after was re-installing each of the required packages in the newer venv (generating a requirements.txt file from the older pip path helped).

Then I selected the venv path in the VS Code interpreter and everything is working fine now.

Bonjour°[大白 2025-01-20 03:21:41

您实际上不需要 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.

很酷不放纵 2025-01-20 03:21:41

“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文