简体   繁体   English

Python 尽管在 WSL2 中激活了虚拟环境,但版本没有改变

[英]Python versions are not changing despite activating virtual environment in WSL2

Background:背景:

In WSL2 (ubuntu 20.04) I created a python virtual environment inside a directory.在 WSL2 (ubuntu 20.04) 中,我在目录中创建了一个 python 虚拟环境。 Using the command python3 -m venv venv my system's python version was set to python3.11 (after downloading) via sudo update-alternatives --config python3 and then choosing the version.使用命令python3 -m venv venv我系统的 python 版本通过sudo update-alternatives --config python3设置为 python3.11(下载后),然后选择版本。 I noticed I was having some errors of missing modules when I started WSL2 (happening after a computer restart), I read this was because I was using a different python version than the one ubuntu 20.04 came with so I switched back to 3.8 via the config menu as before.我注意到我在启动 WSL2 时遇到了一些缺少模块的错误(在计算机重新启动后发生),我读到这是因为我使用的 python 版本与 ubuntu 20.04 随附的版本不同,所以我通过配置切换回 3.8菜单和以前一样。 I am also using VS code that's connected to my WSL2.我还在使用连接到我的 WSL2 的 VS 代码。

These are some of the contents of my venv directory: venv/bin/python venv/bin/python3 venv/bin/python3.11 venv/bin/pip venv/bin/pip3这些是我的 venv 目录的一些内容: venv/bin/python venv/bin/python3 venv/bin/python3.11 venv/bin/pip venv/bin/pip3

Question:题:

After activating my virutal env via source venv/bin/activate , when I do python3 --version I still get a version of 3.8.10 despite creating the virtual environment with 3.11.通过source venv/bin/activate我的虚拟环境后,当我执行python3 --version时,尽管使用 3.11 创建了虚拟环境,但我仍然获得 3.8.10 版本。 I was able to get the interpretor set to 3.11 on VS code.I know I was in the virtual environment since my command prompt had (venv) in front.我能够在 VS 代码上将解释器设置为 3.11。我知道我在虚拟环境中,因为我的命令提示符前面有 (venv)。 I went into the python console while in the virtual env and did import sys and sys.path this was my output ['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload'] .我在虚拟环境中进入 python 控制台并import syssys.path这是我的 output ['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload'] Why isn't the python version changing, am I misunderstanding something or did I not do something correctly?为什么 python 版本没有改变,是我误解了什么还是我做错了什么? Seems like pip isn't working either but works when I switch my system python to 3.11 (I tried installing it on 3.8 but it said it was already installed).似乎 pip 也没有工作,但是当我将系统 python 切换到 3.11 时可以工作(我尝试在 3.8 上安装它,但它说它已经安装)。

Solved:解决了:

Answered below, just re-created the virtual env while making sure my system python version was 3.11 (may have been some mixup earlier).在下面回答,只是重新创建了虚拟环境,同时确保我的系统 python 版本是 3.11(之前可能有些混淆)。

By changing the selection in sudo update-alternatives --config python3 you change the selected python version also in the chosen vitrual environment (at least when using venv , it might be different with other tools).通过更改sudo update-alternatives --config python3中的选择,您还可以在所选的虚拟环境中更改所选的 python 版本(至少在使用venv时,它可能与其他工具不同)。

So changing back to whatever python version you want for the environment through sudo update-alternatives --config python3 would solve the issue.因此,通过sudo update-alternatives --config python3改回您想要的环境版本 python 将解决问题。

What errors of missing modules are you getting?您遇到了哪些缺少模块的错误?

I deleted my venv directory and recreated my virtual environment while on python3.11.我在 python3.11 上删除了我的 venv 目录并重新创建了我的虚拟环境。 This has resolved my issue.这已经解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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