英文:
Does installing packages with a Python version, then downgrading it cause compatibility problems (e.g. module not found)?
问题
在创建一个使用 python=3.11 的 conda 环境后,从一个 requirements.txt 文件安装了多个包,除了一个包之外,所有库都成功安装了。在尝试不同的方式安装最后一个库后,我发现这可能是一个 python 版本的问题,所以我执行了 'conda install python=3.10',然后重试。最后一个包成功安装了,但在尝试运行 python 程序时,出现了一个错误,指出找不到另一个库。这是因为包是使用比运行程序所用的版本更新的版本安装的吗?
尝试检查是否是 VS Code 中的 python 解释器问题。这不是问题。
英文:
TLDR: Does using a python version other than that which was used to initially install the packages in a conda env lead to compatibility problems?
In after creating a conda environment with python=3.11, and installing several packages from a requirements.txt file, all libraries except one were successfully installed. After trying different ways to install the last library, I figured that it might be a python version issue and so I executed 'conda install python=3.10' and retried. The last package was successfully installed but, after trying to run the python program I get an error saying that another library cannot be found. Is that due to the fact that packages were installed using a later version than the ones used to run the program?
Tried to check if it is a python interpreter issue in VS Code. It was not.
答案1
得分: 1
以下是您要翻译的内容:
Conda and Pip Installation Behavior
创建Conda环境时,使用python=3.11
会安装Python 3.11和Pip。在环境内,它会创建一个lib/python3.11/site-packages
目录,这是随后安装Python模块(Python包提供的可导入代码块)的位置。
如果requirements.txt
文件是一个Pip要求文件,那么安装将隐式运行python -m pip install -r requirements.txt
,所有包都将安装到lib/python3.11/site-packages
中。
此外,您使用Conda安装的任何Python包也将类似地安装在那里,但Conda会额外进行跟踪,以跟踪您定义为环境的内容(python=3.11
以及所有Conda包)。它不会跟踪Pip安装的包。
仅Conda正常运作
如果您只安装了Conda包,然后将Conda降级到python=3.10
,它还会正确卸载所有lib/python3.11/site-packages
,然后重新安装匹配的lib/python3.10/site-packages
版本。
Conda忽略Pip安装的包
然而,对于Pip安装的包,情况就不同了。Conda不会调整它们,因为它们不包括在Conda的记录中。因此,您可能仍然在lib/python3.11/site-packages
中留下一堆包,但它们不能被您安装的python=3.10
使用。
因此,您必须重新安装任何Pip安装的包。
更好的策略:使用YAML环境定义
更好的策略是使用YAML文件来管理一切。类似于:
my_py310.yaml
name: my_py310
channels:
- conda-forge
dependencies:
- python=3.10
- pip
## Pip安装的包
- -r requirements.txt
然后使用
conda env create -n my_py310 -f my_py310.yaml
这样,如果您需要降级,您可以编辑或创建此文件的新版本,然后使用一个命令从中创建一个新环境。
建议任何混合使用Conda和Pip包的环境使用此方法根据文档。
英文:
The comments highlight important aspects, but don't explain the mechanics. So, let's walk through it.
Conda and Pip Installation Behavior
When you create a Conda environment with python=3.11
it will install Python 3.11 and Pip. Within the environment, it creates a lib/python3.11/site-packages
directory, which is where subsequent Python modules (the importable code chucks that Python packages deliver) will be installed.
If the requirements.txt
file was a Pip requirements file, then the installation with implicitly run python -m pip install -r requirements.txt
and all packages will be installed to the lib/python3.11/site-packages
.
Also, any Python packages you install with Conda will similarly get installed there, but Conda does the extra bookkeeping of tracking what you have defined as your environment (python=3.11
, plus all Conda packages). It does not track the Pip-installed packages.
Conda-only just works
If you only had Conda-installed packages and then have Conda downgrade to python=3.10
, it will also correctly uninstall all the lib/python3.11/site-packages
and reinstall with matching lib/python3.10/site-packages
versions.
Conda ignores Pip-installed packages
However, it's a different story for Pip-installed packages. Conda will not adjust these because they aren't included in Conda's bookkeeping. Hence, you may still have a bunch of packages residually left in the lib/python3.11/site-packages
but they are no longer usable by the python=3.10
that you have installed.
So, you must reinstall any Pip-installed packages.
Better Strategy: YAML Environment Definition
A better strategy is to use a YAML file to manage everything. Something like:
my_py310.yaml
name: my_py310
channels:
- conda-forge
dependencies:
- python=3.10
- pip
## Pip-installed packages
- -r requirements.txt
then use
conda env create -n my_py310 -f my_py310.yaml
This way, if you had to downgrade, you can edit or create a new version of this file and then create a new environment from it with one command.
Any environment that mixes Conda and Pip packages is recommended to use this approach as per the documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论