如何使用conda安装软件包来更改我的python版本并删除conda? [英] How does using conda to install a package change my python version and remove conda?
问题描述
我一直在将Anaconda与python 2.7一起使用
I had been using Anaconda with python 2.7
$ python
Python 2.7.14 |Anaconda custom (64-bit)| (default, Dec 7 2017, 17:05:42)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
当我决定安装tensorflow时(由于某种原因,我使用了非gpu版本)
When I decided to install tensorflow (since for some reason I had the non-gpu version)
我使用的命令是:
$ conda install -c anaconda tensorflow-gpu
但是,完成后(此cmd的输出详细信息如下),我不再需要conda了:
However, after it was done (detail on output of this cmd to follow), I no longer had conda:
$ conda install -c conda-forge keras
Traceback (most recent call last):
File "/home/me/anaconda2/bin/conda", line 12, in <module>
from conda.cli import main
ModuleNotFoundError: No module named 'conda'
(注意:我也不再拥有Keras),现在正在运行Python 3.7(!?):
(Note: I also no longer had Keras) and was now running Python 3.7(!?):
$ python
Python 3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
发生了什么事?如何阻止它再次发生?这之前发生过一次,我最终删除了所有anaconda文件,然后重新安装.我不想养成习惯.
What happened? How do I stop it from happening again? This happened once before and I ended up deleting all my anaconda files, then reinstalling. I don't want to make that a habit.
我的conda install
的输出是:
$ conda install -c anaconda tensorflow-gpu
Collecting package metadata: done
Solving environment: done
## Package Plan ##
environment location: /home/me/anaconda2
added / updated specs:
- tensorflow-gpu
The following packages will be downloaded:
package | build
---------------------------|-----------------
_tflow_190_select-0.0.1 | gpu 2 KB anaconda
absl-py-0.7.0 | py36_0 156 KB anaconda
astor-0.7.1 | py36_0 43 KB anaconda
c-ares-1.15.0 | h7b6447c_1 98 KB anaconda
ca-certificates-2018.12.5 | 0 123 KB anaconda
certifi-2018.11.29 | py36_0 146 KB anaconda
cudatoolkit-9.0 | h13b8566_0 340.4 MB anaconda
cudnn-7.1.2 | cuda9.0_0 367.8 MB anaconda
cupti-9.0.176 | 0 1.6 MB anaconda
curl-7.63.0 | hbc83047_1000 145 KB anaconda
gast-0.2.2 | py36_0 138 KB anaconda
git-2.11.1 | 0 9.5 MB anaconda
grpcio-1.16.1 | py36hf8bcb03_1 1.1 MB anaconda
krb5-1.16.1 | h173b8e3_7 1.4 MB anaconda
libcurl-7.63.0 | h20c2e04_1000 550 KB anaconda
libedit-3.1.20181209 | hc058e9b_0 188 KB anaconda
libssh2-1.8.0 | h1ba5d50_4 233 KB anaconda
markdown-3.0.1 | py36_0 107 KB anaconda
mkl_fft-1.0.10 | py36ha843d7b_0 170 KB anaconda
mkl_random-1.0.2 | py36hd81dba3_0 407 KB anaconda
ncurses-6.1 | he6710b0_1 958 KB anaconda
numpy-1.15.4 | py36h7e9f1db_0 47 KB anaconda
numpy-base-1.15.4 | py36hde5b4d6_0 4.3 MB anaconda
openssl-1.1.1 | h7b6447c_0 5.0 MB anaconda
pip-18.1 | py36_0 1.8 MB anaconda
protobuf-3.5.2 | py36hf484d3e_1 610 KB anaconda
python-3.6.8 | h0371630_0 34.4 MB anaconda
qt-4.8.7 | 2 34.1 MB anaconda
setuptools-40.6.3 | py36_0 625 KB anaconda
six-1.12.0 | py36_0 22 KB anaconda
sqlite-3.26.0 | h7b6447c_0 1.9 MB anaconda
tensorboard-1.9.0 | py36hf484d3e_0 3.3 MB anaconda
tensorflow-1.9.0 |gpu_py36h02c5d5e_1 3 KB anaconda
tensorflow-base-1.9.0 |gpu_py36h6ecc378_0 170.8 MB anaconda
tensorflow-gpu-1.9.0 | hf154084_0 2 KB anaconda
termcolor-1.1.0 | py36_1 7 KB anaconda
tk-8.6.8 | hbc83047_0 3.1 MB anaconda
werkzeug-0.14.1 | py36_0 423 KB anaconda
wheel-0.32.3 | py36_0 35 KB anaconda
------------------------------------------------------------
Total: 985.7 MB
The following NEW packages will be INSTALLED:
_tflow_190_select anaconda/linux-64::_tflow_190_select-0.0.1-gpu
c-ares anaconda/linux-64::c-ares-1.15.0-h7b6447c_1
cudatoolkit anaconda/linux-64::cudatoolkit-9.0-h13b8566_0
cudnn anaconda/linux-64::cudnn-7.1.2-cuda9.0_0
cupti anaconda/linux-64::cupti-9.0.176-0
krb5 anaconda/linux-64::krb5-1.16.1-h173b8e3_7
pip anaconda/linux-64::pip-18.1-py36_0
tensorflow-gpu anaconda/linux-64::tensorflow-gpu-1.9.0-hf154084_0
The following packages will be UPDATED:
absl-py conda-forge/noarch::absl-py-0.1.10-py~ --> anaconda/linux-64::absl-py-0.7.0-py36_0
ca-certificates conda-forge::ca-certificates-2018.11.~ --> anaconda::ca-certificates-2018.12.5-0
curl pkgs/main::curl-7.60.0-h84994c4_0 --> anaconda::curl-7.63.0-hbc83047_1000
gast 0.2.0-py27_0 --> 0.2.2-py36_0
grpcio pkgs/main::grpcio-1.12.1-py27hdbcaa40~ --> anaconda::grpcio-1.16.1-py36hf8bcb03_1
libcurl pkgs/main::libcurl-7.60.0-h1ad7b7a_0 --> anaconda::libcurl-7.63.0-h20c2e04_1000
libedit pkgs/main::libedit-3.1-heed3624_0 --> anaconda::libedit-3.1.20181209-hc058e9b_0
markdown conda-forge/noarch::markdown-2.6.11-p~ --> anaconda/linux-64::markdown-3.0.1-py36_0
mkl_fft pkgs/main::mkl_fft-1.0.6-py27hd81dba3~ --> anaconda::mkl_fft-1.0.10-py36ha843d7b_0
ncurses pkgs/main::ncurses-6.0-h9df7e31_2 --> anaconda::ncurses-6.1-he6710b0_1
openssl conda-forge::openssl-1.0.2p-h14c3975_~ --> anaconda::openssl-1.1.1-h7b6447c_0
protobuf conda-forge::protobuf-3.5.2-py27hd28b~ --> anaconda::protobuf-3.5.2-py36hf484d3e_1
python pkgs/main::python-2.7.14-h1571d57_29 --> anaconda::python-3.6.8-h0371630_0
setuptools pkgs/main::setuptools-38.4.0-py27_0 --> anaconda::setuptools-40.6.3-py36_0
six pkgs/main::six-1.11.0-py27h5f960f1_1 --> anaconda::six-1.12.0-py36_0
sqlite pkgs/main::sqlite-3.23.1-he433501_0 --> anaconda::sqlite-3.26.0-h7b6447c_0
tensorflow conda-forge::tensorflow-1.3.0-py27_0 --> anaconda::tensorflow-1.9.0-gpu_py36h02c5d5e_1
tk pkgs/main::tk-8.6.7-hc745277_3 --> anaconda::tk-8.6.8-hbc83047_0
wheel pkgs/main::wheel-0.30.0-py27h2bc6bb2_1 --> anaconda::wheel-0.32.3-py36_0
The following packages will be SUPERSEDED by a higher-priority channel:
certifi conda-forge::certifi-2018.11.29-py27_~ --> anaconda::certifi-2018.11.29-py36_0
git pkgs/main::git-2.17.0-pl526hb75a9fb_0 --> anaconda::git-2.11.1-0
libssh2 pkgs/main::libssh2-1.8.0-h9cfc8f7_4 --> anaconda::libssh2-1.8.0-h1ba5d50_4
mkl_random pkgs/main::mkl_random-1.0.2-py27hd81d~ --> anaconda::mkl_random-1.0.2-py36hd81dba3_0
numpy pkgs/main::numpy-1.15.4-py27h7e9f1db_0 --> anaconda::numpy-1.15.4-py36h7e9f1db_0
numpy-base pkgs/main::numpy-base-1.15.4-py27hde5~ --> anaconda::numpy-base-1.15.4-py36hde5b4d6_0
qt pkgs/main::qt-5.9.4-h4e5bff0_0 --> anaconda::qt-4.8.7-2
tensorflow-base pkgs/main::tensorflow-base-1.9.0-eige~ --> anaconda::tensorflow-base-1.9.0-gpu_py36h6ecc378_0
werkzeug pkgs/main::werkzeug-0.14.1-py27_0 --> anaconda::werkzeug-0.14.1-py36_0
The following packages will be DOWNGRADED:
astor 0.7.1-py27_0 --> 0.7.1-py36_0
tensorboard 1.10.0-py27hf484d3e_0 --> 1.9.0-py36hf484d3e_0
termcolor 1.1.0-py27_1 --> 1.1.0-py36_1
Proceed ([y]/n)? y
Downloading and Extracting Packages
tensorflow-gpu-1.9.0 | 2 KB | ########################################################################################################################################## | 100%
absl-py-0.7.0 | 156 KB | ########################################################################################################################################## | 100%
six-1.12.0 | 22 KB | ########################################################################################################################################## | 100%
git-2.11.1 | 9.5 MB | ########################################################################################################################################## | 100%
_tflow_190_select-0. | 2 KB | ########################################################################################################################################## | 100%
setuptools-40.6.3 | 625 KB | ########################################################################################################################################## | 100%
c-ares-1.15.0 | 98 KB | ########################################################################################################################################## | 100%
cupti-9.0.176 | 1.6 MB | ########################################################################################################################################## | 100%
libssh2-1.8.0 | 233 KB | ########################################################################################################################################## | 100%
gast-0.2.2 | 138 KB | ########################################################################################################################################## | 100%
ncurses-6.1 | 958 KB | ########################################################################################################################################## | 100%
protobuf-3.5.2 | 610 KB | ########################################################################################################################################## | 100%
tensorflow-base-1.9. | 170.8 MB | ########################################################################################################################################## | 100%
ca-certificates-2018 | 123 KB | ########################################################################################################################################## | 100%
python-3.6.8 | 34.4 MB | ########################################################################################################################################## | 100%
cudatoolkit-9.0 | 340.4 MB | ########################################################################################################################################## | 100%
qt-4.8.7 | 34.1 MB | ########################################################################################################################################## | 100%
sqlite-3.26.0 | 1.9 MB | ########################################################################################################################################## | 100%
astor-0.7.1 | 43 KB | ########################################################################################################################################## | 100%
tensorboard-1.9.0 | 3.3 MB | ########################################################################################################################################## | 100%
mkl_fft-1.0.10 | 170 KB | ########################################################################################################################################## | 100%
mkl_random-1.0.2 | 407 KB | ########################################################################################################################################## | 100%
certifi-2018.11.29 | 146 KB | ########################################################################################################################################## | 100%
wheel-0.32.3 | 35 KB | ########################################################################################################################################## | 100%
numpy-base-1.15.4 | 4.3 MB | ########################################################################################################################################## | 100%
numpy-1.15.4 | 47 KB | ########################################################################################################################################## | 100%
curl-7.63.0 | 145 KB | ########################################################################################################################################## | 100%
openssl-1.1.1 | 5.0 MB | ########################################################################################################################################## | 100%
tk-8.6.8 | 3.1 MB | ########################################################################################################################################## | 100%
libedit-3.1.20181209 | 188 KB | ########################################################################################################################################## | 100%
markdown-3.0.1 | 107 KB | ########################################################################################################################################## | 100%
werkzeug-0.14.1 | 423 KB | ########################################################################################################################################## | 100%
krb5-1.16.1 | 1.4 MB | ########################################################################################################################################## | 100%
termcolor-1.1.0 | 7 KB | ########################################################################################################################################## | 100%
pip-18.1 | 1.8 MB | ########################################################################################################################################## | 100%
libcurl-7.63.0 | 550 KB | ########################################################################################################################################## | 100%
tensorflow-1.9.0 | 3 KB | ########################################################################################################################################## | 100%
grpcio-1.16.1 | 1.1 MB | ########################################################################################################################################## | 100%
cudnn-7.1.2 | 367.8 MB | ########################################################################################################################################## | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
(好的-我现在看到了对python 3.7的更改,但这仍然是必须要小心的讨厌的事情.是否有某种方法可以迫使它不理会我的Python版本?)
(OK - I see the change to Python 3.7 now, but that's still a nasty thing to have to be careful about. Is there some way to force it to leave my Python version alone?)
推荐答案
原因
在不更新conda
软件包的情况下更改Python版本会使Conda中断.. Python版本更改(2.7.14-> 3.6.8)导致了新的python
具有新的site-packages
且不再包含conda
软件包的情况,而如果仅在2.7.x内进行更新,则会出现这种情况不会有问题.
Cause
Changing Python versions without updating the conda
package breaks Conda. . The Python version change (2.7.14 -> 3.6.8) created a situation where the new python
has a new site-packages
which no longer contains a conda
package, whereas if you only update within 2.7.x, this wouldn't be an issue.
Conda包含一组二进制文件(例如,在shell中键入conda
时要调用的二进制文件)和同名的Python包.为了使Conda整体正常运行,必须使用Python软件包,并且在您尝试使用conda
时都会加载该Python软件包.
Conda includes both a set of binaries (e.g., what you're invoking when you type conda
in a shell) and a Python package by the same name. The Python package is necessary for Conda as a whole to function and it get's loaded whenever you try to use conda
.
有问题的是,Anaconda上的许多软件包似乎正在触发Python版本更改,但随后没有触发conda
软件包更新.这听起来像是依赖解析器所忽略的东西-即默认行为应该是保护conda
所在的 base 环境的完整性.
It is problematic that many packages on Anaconda seem to be triggering Python version changes, but not subsequently triggering a conda
package update. This sounds like something the dependency resolver is overlooking - i.e., default behavior should be to protect integrity of base environment where conda
lives.
一种可能的恢复途径是临时使用独立版本的conda-exec
修复您的 base 环境.您可以从任何目录执行以下所有操作,因此可以使用临时文件,也可以在放置下载文件的任何地方进行.如果可行或需要调整,请在评论中报告!
One possible route to recovery is to temporarily use a standalone build of the conda-exec
to repair your base env. You can do all the following from any directory, so maybe use a temp or wherever you put downloads. Please report in the comments if this works or needs adjusting!
-
为您的平台下载合适的Conda (此处我们将使用
conda-latest-linux-64.exe
).不要介意.exe
,它是一个二进制文件,应在shell上运行时运行.无论如何,我都会将其重命名为conda-exec
:
Download the appropriate Conda for your platform (here we'll use
conda-latest-linux-64.exe
). Don't mind the.exe
it's a binary and should run when called at the shell. I'm going to rename it toconda-exec
anyway:
wget -O conda-exec https://repo.anaconda.com/pkgs/misc/conda-execs/conda-latest-linux-64.exe
chmod +x conda-exec
暂时将CONDA_ROOT_PREFIX
设置为安装基础.通常,这是anaconda3
或miniconda3
文件夹.在这种情况下,我们将使用OP给定的路径:
Temporarily set CONDA_ROOT_PREFIX
to the base of your install. Typically this is the anaconda3
or miniconda3
folder; in this case, we'll use the path given by OP:
export CONDA_ROOT_PREFIX=/home/me/anaconda2
测试它是否有效:
Test that it works:
./conda-exec info
要检查的关键是base environment:
可以正确识别您的 base env所在的位置,并将其显示为(writable)
.您还应该在package cache:
的 base 环境中看到pkgs
文件夹.
The key thing to check for is that base environment:
correctly identifies to where your base env is and shows it as (writable)
. You should also see the pkgs
folder in your base env in the package cache:
.
选项1:恢复为以前的版本
-
在当前版本之前确定版本(我们在此处用
<k-1>
表示),并尝试将其恢复:
Identify the revision immediately before the current one (we'll denote it by
<k-1>
here), and attempt to restore it:
./conda-exec list -n base --revisions
./conda-exec install -n base --revision <k-1>
如果这可行,则应该完成.启动一个新的外壳,然后再次尝试使用conda
.否则,另一个选择是...
If this works, you should be done. Start a new shell and try using conda
again. Otherwise, the other option is...
选项2:为当前的Python安装conda
-
(重新)在 base env中安装
conda
软件包:
(Re-)Install the
conda
package in the base env:
./conda-exec install -n base conda
确保建议的Conda构建与当前安装的Python版本相对应.如果--force-reinstall
标志声称已满足要求,则可能会有用.
Make sure that the build of Conda that is suggested corresponds to the version of Python currently installed. The --force-reinstall
flag might be useful if it claims the requirement is already satisfed.
尝试新的外壳,然后查看conda
是否正常工作.您无需保留conda-exec
.
Try a new shell and see if conda
is working. You don't need to keep the conda-exec
around.
如果其他所有方法均失败,则可能只需要重新安装即可.其他人报告说已安装在其他目录中,并且仍然可以使用和访问其env.
If all else fails you may just have to reinstall. Others have reported installing in other directories and being able to still use and access their envs.
首先,只是一般性的建议(建议):充分利用虚拟环境.这并不能直接解决问题,但是可以帮助您拥有一个工作流,该工作流不那么容易遇到此类陷阱.首先,您不应该接受如此巨大的改变,而不是基础.就个人而言,我很少在基础架构之外的 base 中安装东西(emacs,jupyter相关的东西,conda等). 1 进入特定于项目或至少具有开发类型的环境.
First, just a general (opinionated) recommendation: leverage virtual envs more. This isn't directly solving the problem, but it will help you have a workflow that is significantly less prone to encountering such pitfalls. You shouldn't have accepted such a huge change in the first place, not to base. Personally, I rarely install things in base outside of infrastructure (emacs, jupyter-related things, conda, etc.).1 Software packages go into project-specific or at least development-type envs.
例如,如果我正在显示安装,我会为此创建一个新的环境
For example, were I doing the install shown, I would have made a new env for it
conda create -n tf36 anaconda::tensorflow-gpu python=3.6
或您实际希望使用的任何Python版本.
or whatever Python version you actually wish to work in.
Conda确实支持程序包固定,这是更直接的方法,通过将Python 2过渡到3,可以确保您永远不会破坏 base 的安装.即,在env的conda-meta
文件夹中创建一个文件pinned
并添加行
Conda does support package pinning, and this is the more direct way to ensure you never ruin your base install again by transitioning Python 2 to 3. Namely, in the env's conda-meta
folder create a file, pinned
and add the line
python 2.7.*
请注意,一些用户报告了3.6-> 3.7过渡的类似问题,因此我认为此处必须包含次要版本.请参见有关固定的文档.
Note that some users have reported similar issues for 3.6 -> 3.7 transitions, so I believe including the minor version here is necessary. See the documentation on pinning.
[1]请注意,我使用的是Miniconda,而不是Anaconda安装程序,因此我从一开始就对 base 有更多的控制权.
[1] Note that I use Miniconda, not the Anaconda installer, so I have more control over base from the start.
这篇关于如何使用conda安装软件包来更改我的python版本并删除conda?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!