【问题标题】:How does using conda to install a package change my python version and remove conda?使用 conda 安装包如何更改我的 python 版本并删除 conda?
【发布时间】:2019-06-29 23:35:03
【问题描述】:

我一直在使用 Anaconda 和 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 版本)

我使用的命令是:

$ conda install -c anaconda tensorflow-gpu

但是,在完成之后(后面会详细介绍这个 cmd 的输出),我不再有 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(!?):

$ 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 文件,然后重新安装。我不想养成这样的习惯。

我的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 版本吗?)

【问题讨论】:

    标签: anaconda conda


    【解决方案1】:

    原因

    更改 Python 版本而不更新 conda 包会破坏 Conda。。 Python 版本更改 (2.7.14 -> 3.6.8) 造成了新的 python 有一个新的 site-packages 不再包含 conda 包的情况,而如果您只在 2.7.x 内更新,这不会是问题。

    Conda 包含一组二进制文件(例如,当您在 shell 中键入 conda 时调用的内容)和一个同名的 Python 包。 Python 包是 Conda 整体运行所必需的,每当您尝试使用 conda 时,它就会被加载。

    问题在于 Anaconda 上的许多包似乎都在触发 Python 版本更改,但随后并未触发 conda 包更新。这听起来像是依赖解析器忽略的东西 - 即,默认行为应该是保护 conda 所在的 base 环境的完整性。

    试图恢复

    一种可能的恢复途径是暂时使用独立版本的conda-exec 来修复您的base 环境。您可以从任何目录执行以下所有操作,因此可以使用临时文件或放置下载的任何位置。如果这可行或需要调整,请在 cmets 中报告!

    1. Download the appropriate standalone Conda 用于您的平台(这里我们将使用linux-64/conda-standalone-4.9.2)。实际的二进制文件将位于 .tar.bz2 文件中的 conda_standalone/conda.exe。不要介意.exe 它是一个二进制文件,应该在shell 调用时运行。我还是要把它重命名为conda-exec

      # download archive
      wget -qO conda-standalone-4.9.2.tar.bz2 https://anaconda.org/conda-forge/conda-standalone/4.9.2/download/linux-64/conda-standalone-4.9.2-ha770c72_2.tar.bz2
      
      # extract only the binary and rename
      tar -xzOf conda-standalone-4.9.2.tar.bz2 standalone_conda/conda.exe > conda-exec
      
      # add executable permissions
      chmod +x conda-exec
      
    2. 暂时将CONDA_ROOT_PREFIX 设置为安装的基础。通常这是anaconda3miniconda3 文件夹;在这种情况下,我们将使用 OP 给出的路径:

       export CONDA_ROOT_PREFIX=/home/me/anaconda2
      
    3. 测试它是否有效:

       ./conda-exec info
      

      要检查的关键是 base environment: 正确识别您的 base 环境所在的位置并将其显示为 (writable)。您还应该在 package cache: base 环境中看到 pkgs 文件夹。

    选项 1:恢复到以前的版本

    1. 确定当前版本之前的修订版本(我们将在此处用&lt;k-1&gt; 表示),并尝试恢复它:

       ./conda-exec list -n base --revisions
       ./conda-exec install -n base --revision <k-1>
      

      如果这可行,您应该完成。启动一个新的 shell 并再次尝试使用conda。否则,另一种选择是……

    选项 2:为当前 Python 安装 conda

    1. (重新)在 base 环境中安装 conda 包:

       ./conda-exec install -n base conda
      

      确保建议的 Conda 版本与当前安装的 Python 版本相对应。如果 --force-reinstall 标志声称要求已得到满足,它可能会很有用。

    尝试一个新的外壳,看看conda 是否正常工作。您无需保留conda-exec

    最后的追索权

    如果一切都失败了,您可能只需要重新安装。其他人报告说安装在其他目录中并且仍然能够使用和访问他们的环境。

    预防措施

    通过更好的实践避免破坏

    首先,只是一个一般性的(有意见的)建议:更多地利用虚拟环境。这并不能直接解决问题,但它会帮助您拥有一个明显不易遇到此类陷阱的工作流程。你一开始就不应该接受如此巨大的变化,而不是base。就个人而言,我很少在基础设施之外的 base 中安装东西(emacs、jupyter 相关的东西、conda 等)。1 软件包去进入特定项目或至少开发类型的环境。

    例如,如果我进行所示的安装,我会为它创建一个新的环境

    conda create -n tf36 anaconda::tensorflow-gpu python=3.6
    

    或您真正希望使用的任何 Python 版本。

    直接解决方案:固定

    Conda 确实支持包固定,这是通过将 Python 2 转换为 3 来确保您永远不会再次破坏 base 安装的更直接的方法。也就是说,在 env 的 conda-meta 文件夹中创建一个文件,pinned 并添加行

    python 2.7.*
    

    请注意,一些用户报告了 3.6 -> 3.7 转换的类似问题,因此我认为在此处包含次要版本是必要的。见the documentation on pinning


    [1] 请注意,我使用的是 Miniconda,而不是 Anaconda 安装程序,因此我从一开始就可以更好地控制 base

    【讨论】:

    • 感谢您在这里的详细解释,如果您能在此处查看这个非常相似但未决的问题,我们将不胜感激:stackoverflow.com/questions/54758234/…
    • 感谢您的解释。我无法正常恢复,实际上不得不从头开始删除并重新安装 Anaconda。只是通过conda update --all 就可以发生这个问题,而没有任何警告,这有点糟糕。
    • 我想确认在 3.6->3.7 转换中面临同样的问题。
    • 那么如何在不破坏 conda 的情况下更新 python 版本呢?我试图简单地从 3.6->3.8 更新 python 并破坏了我的环境。
    • @Mastiff 通常,不建议就地升级 Python 版本(除了补丁)。这也几乎没有意义,因为它几乎触发了每个包都需要一个新的构建。只需创建一个新环境。另外,我强烈建议所有用户不要使用他们的 base 环境进行工作,因此其中的 Python 版本应该无关紧要。
    【解决方案2】:

    我已经通过删除任何 PYTHONHOME sys PATH(s) 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-15
      • 1970-01-01
      • 2018-04-10
      • 2022-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多