2020年11月24日

linux 服务安装 Anaconda2

点击数:7

Anaconda 介绍

介绍部分来源于简书:https://www.jianshu.com/p/62f155eb6ac5,作者:Raxxie

简介

Anaconda(官方网站)就是可以便捷获取包且对包能够进行管理,同时对环境可以统一管理的发行版本。Anaconda包含了conda、Python在内的超过180个科学包及其依赖项。

特点

Anaconda具有如下特点:

  • 开源
  • 安装过程简单
  • 高性能使用Python和R语言
  • 免费的社区支持

其特点的实现主要基于Anaconda拥有的:

  • conda包
  • 环境管理器
  • 1,000+开源库

如果日常工作或学习并不必要使用1,000多个库,那么可以考虑安装Miniconda(图形界面下载及命令行安装请戳),这里不过多介绍Miniconda的安装及使用。

Anaconda、conda、pip、virtualenv的区别

① Anaconda

Anaconda是一个包含180+的科学包及其依赖项的发行版本。其包含的科学包包括:conda, numpy, scipy, ipython notebook等。

② conda

conda 是包及其依赖项和环境的管理工具。

适用语言:Python, R, Ruby, Lua, Scala, Java, JavaScript, C/C++, FORTRAN。

适用平台:Windows, macOS, Linux

用途:

  • 快速安装、运行和升级包及其依赖项。

  • 在计算机中便捷地创建、保存、加载和切换环境。

    如果你需要的包要求不同版本的Python,你无需切换到不同的环境,因为conda同样是一个环境管理器。仅需要几条命令,你可以创建一个完全独立的环境来运行不同的Python版本,同时继续在你常规的环境中使用你常用的Python版本。——conda官方网站

    • conda 为 Python 项目而创造,但可适用于上述的多种语言。

    • conda 包和环境管理器包含于 Anaconda 的所有版本当中。

③ pip

  • pip 是用于安装和管理软件包的包管理器。
  • pip 编写语言:Python。

  • Python 中默认安装的版本:

  • Python 2.7.9 及后续版本:默认安装,命令为 pip
    Python 3.4 及后续版本:默认安装,命令为 pip3
    pip 名称的由来:pip 采用的是递归缩写进行命名的。其名字被普遍认为来源于2处:

  • “Pip installs Packages”(“pip安装包”)
    “Pip installs Python”(“pip安装Python”)

④ virtualenv

  • virtualenv:用于创建一个独立的 Python 环境的工具。
  • 解决问题:

    • 当一个程序需要使用 Python 2.7 版本,而另一个程序需要使用 Python 3.6 版本,如何同时使用这两个程序?
    • 如果将所有程序都安装在系统下的默认路径,如:/usr/lib/python2.7/site-packages,当不小心升级了本不该升级的程序时,将会对其他的程序造成影响。

    • 如果想要安装程序并在程序运行时对其库或库的版本进行修改,都会导致程序的中断。

    • 在共享主机时,无法在全局site-packages目录中安装包。

  • virtualenv将会为它自己的安装目录创建一个环境,这并不与其他virtualenv环境共享库;同时也可以选择性地不连接已安装的全局库。

⑤ pip 与 conda 比较

  • 依赖项检查
    • pip:
      • 不一定会展示所需其他依赖包。
      • 安装包时或许会直接忽略依赖项而安装,仅在结果中提示错误。
    • conda:
      • 列出所需其他依赖包。
      • 安装包时自动安装其依赖项。
      • 可以便捷地在包的不同版本中自由切换。
  • 环境管理
    • pip:维护多个环境难度较大。
    • conda:比较方便地在不同环境之间进行切换,环境管理较为简单。
  • 对系统自带Python的影响
    • pip:在系统自带Python中包的**更新/回退版本/卸载将影响其他程序。
    • conda:不会影响系统自带Python。
  • 适用语言
    • pip:仅适用于Python。
    • conda:适用于Python, R, Ruby, Lua, Scala, Java, JavaScript, C/C++, FORTRAN。

⑥ conda与pip、virtualenv的关系

  • conda结合了pip和virtualenv的功能。

linux 服务器安装 Anaconda2

Anaconda的官方服务器由于种种原因,在国内访问慢得如同蜗牛爬,是在无法忍受这种速度。国内清华大学的镜像https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/比较靠谱,国内使用口碑很不错。

打开上面的链接,里面就是 anaconda 的归档文件,有各种平台的安装包,也有从 1 到 3 的各个版本。因为工作的需要,原始应用使用的是 anaconda2 的版本,为了不出乱子继续使用该版本。我选择Anaconda2-5.3.1-Linux-x86_64.sh,这个从归档中看是 Anaconda2 的最高版本,应该是相当稳定的版本,毕竟现在都到 3.5 了。

在服务器上执行下面命令下载到当前用户目录:

wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda2-5.3.1-Linux-x86_64.sh

虽然说国内镜像,但下载速度也没有想象的那么快!20分钟下载了不到 20%…┭┮﹏┭┮

经历了一顿午饭的时间,终于下载完了。该安装包是一个可执行脚本,里面包含了众多工具包以及软件包的二进制byte。执行脚本:

sh /Anaconda2-5.3.1-Linux-x86_64.sh

通过上面命令开始安装 anaconda2,安装过程一般情况下只需要回车和输入yes 即可。默认情况 anaconda2 是安装在当前用户目录下的,当然在安装交互阶段提示时可以输入需要安装的目录即可。

下面给大家展示一下 3.5.3 的包中包含的依赖包清单:

PREFIX=/home/junwen.zhou/software/anaconda2
installing: python-2.7.15-h1571d57_0 ...
Python 2.7.15 :: Anaconda, Inc.
installing: blas-1.0-mkl ...
installing: ca-certificates-2018.03.07-0 ...
installing: conda-env-2.6.0-1 ...
installing: intel-openmp-2019.0-118 ...
installing: libgcc-ng-8.2.0-hdf63c60_1 ...
installing: libgfortran-ng-7.3.0-hdf63c60_0 ...
installing: libstdcxx-ng-8.2.0-hdf63c60_1 ...
installing: bzip2-1.0.6-h14c3975_5 ...
installing: expat-2.2.6-he6710b0_0 ...
installing: fribidi-1.0.5-h7b6447c_0 ...
installing: gmp-6.1.2-h6c8ec71_1 ...
installing: graphite2-1.3.12-h23475e2_2 ...
installing: icu-58.2-h9c2bf20_1 ...
installing: jbig-2.1-hdba287a_0 ...
installing: jpeg-9b-h024ee3a_2 ...
installing: libffi-3.2.1-hd88cf55_4 ...
installing: libsodium-1.0.16-h1bed415_0 ...
installing: libtool-2.4.6-h544aabb_3 ...
installing: libuuid-1.0.3-h1bed415_2 ...
installing: libxcb-1.13-h1bed415_1 ...
installing: lzo-2.10-h49e0be7_2 ...
installing: mkl-2019.0-118 ...
installing: ncurses-6.1-hf484d3e_0 ...
installing: openssl-1.0.2p-h14c3975_0 ...
installing: patchelf-0.9-hf484d3e_2 ...
installing: pcre-8.42-h439df22_0 ...
installing: pixman-0.34.0-hceecf20_3 ...
installing: snappy-1.1.7-hbae5bb6_3 ...
installing: xz-5.2.4-h14c3975_4 ...
installing: yaml-0.1.7-had09818_2 ...
installing: zlib-1.2.11-ha838bed_2 ...
installing: blosc-1.14.4-hdbcaa40_0 ...
installing: glib-2.56.2-hd408876_0 ...
installing: hdf5-1.10.2-hba1933b_1 ...
installing: libedit-3.1.20170329-h6b74fdf_2 ...
installing: libpng-1.6.34-hb9fc6fc_0 ...
installing: libssh2-1.8.0-h9cfc8f7_4 ...
installing: libtiff-4.0.9-he85c1e1_2 ...
installing: libxml2-2.9.8-h26e45fe_1 ...
installing: mpfr-4.0.1-hdf1c602_3 ...
installing: pandoc-1.19.2.1-hea2e7c5_1 ...
installing: readline-7.0-h7b6447c_5 ...
installing: tk-8.6.8-hbc83047_0 ...
installing: zeromq-4.2.5-hf484d3e_1 ...
installing: dbus-1.13.2-h714fa37_1 ...
installing: freetype-2.9.1-h8a8886c_1 ...
installing: gstreamer-1.14.0-hb453b48_1 ...
installing: libcurl-7.61.0-h1ad7b7a_0 ...
installing: libxslt-1.1.32-h1312cb7_0 ...
installing: mpc-1.1.0-h10f8cd9_1 ...
installing: sqlite-3.24.0-h84994c4_0 ...
installing: unixodbc-2.3.7-h14c3975_0 ...
installing: curl-7.61.0-h84994c4_0 ...
installing: fontconfig-2.13.0-h9420a91_0 ...
installing: gst-plugins-base-1.14.0-hbbd80ab_1 ...
installing: alabaster-0.7.11-py27_0 ...
installing: appdirs-1.4.3-py27h28b3542_0 ...
installing: asn1crypto-0.24.0-py27_0 ...
installing: atomicwrites-1.2.1-py27_0 ...
installing: attrs-18.2.0-py27h28b3542_0 ...
installing: backports-1.0-py27_1 ...
installing: backports_abc-0.5-py27_0 ...
installing: beautifulsoup4-4.6.3-py27_0 ...
installing: bitarray-0.8.3-py27h14c3975_0 ...
installing: boto-2.49.0-py27_0 ...
installing: cairo-1.14.12-h8948797_3 ...
installing: cdecimal-2.3-py27h14c3975_3 ...
installing: certifi-2018.8.24-py27_1 ...
installing: chardet-3.0.4-py27_1 ...
installing: click-6.7-py27_0 ...
installing: cloudpickle-0.5.5-py27_0 ...
installing: colorama-0.3.9-py27_0 ...
installing: configparser-3.5.0-py27_0 ...
installing: constantly-15.1.0-py27h28b3542_0 ...
installing: contextlib2-0.5.5-py27_0 ...
installing: dask-core-0.19.1-py27_0 ...
installing: decorator-4.3.0-py27_0 ...
installing: defusedxml-0.5.0-py27_1 ...
installing: docutils-0.14-py27_0 ...
installing: enum34-1.1.6-py27_1 ...
installing: et_xmlfile-1.0.1-py27_0 ...
installing: fastcache-1.0.2-py27h14c3975_2 ...
installing: filelock-3.0.8-py27_0 ...
installing: funcsigs-1.0.2-py27_0 ...
installing: functools32-3.2.3.2-py27_1 ...
installing: futures-3.2.0-py27_0 ...
installing: glob2-0.6-py27_0 ...
installing: gmpy2-2.0.8-py27h10f8cd9_2 ...
installing: greenlet-0.4.15-py27h7b6447c_0 ...
installing: grin-1.2.1-py27_4 ...
installing: heapdict-1.0.0-py27_2 ...
installing: idna-2.7-py27_0 ...
installing: imagesize-1.1.0-py27_0 ...
installing: incremental-17.5.0-py27_0 ...
installing: ipaddress-1.0.22-py27_0 ...
installing: ipython_genutils-0.2.0-py27_0 ...
installing: itsdangerous-0.24-py27_1 ...
installing: jdcal-1.4-py27_0 ...
installing: kiwisolver-1.0.1-py27hf484d3e_0 ...
installing: lazy-object-proxy-1.3.1-py27h14c3975_2 ...
installing: linecache2-1.0.0-py27_0 ...
installing: locket-0.2.0-py27_1 ...
installing: lxml-4.2.5-py27hefd8a0e_0 ...
installing: markupsafe-1.0-py27h14c3975_1 ...
installing: mccabe-0.6.1-py27_1 ...
installing: mistune-0.8.3-py27h14c3975_1 ...
installing: mkl-service-1.1.2-py27h90e4bf4_5 ...
installing: mpmath-1.0.0-py27_2 ...
installing: msgpack-python-0.5.6-py27h6bb024c_1 ...
installing: numpy-base-1.15.1-py27h81de0dd_0 ...
installing: olefile-0.46-py27_0 ...
installing: pandocfilters-1.4.2-py27_1 ...
installing: parso-0.3.1-py27_0 ...
installing: path.py-11.1.0-py27_0 ...
installing: pep8-1.7.1-py27_0 ...
installing: pkginfo-1.4.2-py27_1 ...
installing: pluggy-0.7.1-py27h28b3542_0 ...
installing: ply-3.11-py27_0 ...
installing: psutil-5.4.7-py27h14c3975_0 ...
installing: ptyprocess-0.6.0-py27_0 ...
installing: py-1.6.0-py27_0 ...
installing: pyasn1-0.4.4-py27h28b3542_0 ...
installing: pycodestyle-2.4.0-py27_0 ...
installing: pycosat-0.6.3-py27h14c3975_0 ...
installing: pycparser-2.18-py27_1 ...
installing: pycrypto-2.6.1-py27h14c3975_9 ...
installing: pycurl-7.43.0.2-py27hb7f436b_0 ...
installing: pyflakes-2.0.0-py27_0 ...
installing: pyodbc-4.0.24-py27he6710b0_0 ...
installing: pyparsing-2.2.0-py27_1 ...
installing: pysocks-1.6.8-py27_0 ...
installing: pytz-2018.5-py27_0 ...
installing: pyyaml-3.13-py27h14c3975_0 ...
installing: pyzmq-17.1.2-py27h14c3975_0 ...
installing: qt-5.9.6-h8703b6f_2 ...
installing: qtpy-1.5.0-py27_0 ...
installing: rope-0.11.0-py27_0 ...
installing: ruamel_yaml-0.15.46-py27h14c3975_0 ...
installing: scandir-1.9.0-py27h14c3975_0 ...
installing: send2trash-1.5.0-py27_0 ...
installing: simplegeneric-0.8.1-py27_2 ...
installing: sip-4.19.8-py27hf484d3e_0 ...
installing: six-1.11.0-py27_1 ...
installing: snowballstemmer-1.2.1-py27_0 ...
installing: sortedcontainers-2.0.5-py27_0 ...
installing: sphinxcontrib-1.0-py27_1 ...
installing: sqlalchemy-1.2.11-py27h7b6447c_0 ...
installing: subprocess32-3.5.2-py27h14c3975_0 ...
installing: tblib-1.3.2-py27_0 ...
installing: toolz-0.9.0-py27_0 ...
installing: tqdm-4.26.0-py27h28b3542_0 ...
installing: typing-3.6.6-py27_0 ...
installing: unicodecsv-0.14.1-py27_0 ...
installing: wcwidth-0.1.7-py27_0 ...
installing: webencodings-0.5.1-py27_1 ...
installing: werkzeug-0.14.1-py27_0 ...
installing: wrapt-1.10.11-py27h14c3975_2 ...
installing: xlrd-1.1.0-py27_1 ...
installing: xlsxwriter-1.1.0-py27_0 ...
installing: xlwt-1.3.0-py27_0 ...
installing: zope-1.0-py27_1 ...
installing: automat-0.7.0-py27_0 ...
installing: babel-2.6.0-py27_0 ...
installing: backports.shutil_get_terminal_size-1.0.0-py27_2 ...
installing: cffi-1.11.5-py27he75722e_1 ...
installing: cycler-0.10.0-py27_0 ...
installing: cytoolz-0.9.0.1-py27h14c3975_1 ...
installing: entrypoints-0.2.3-py27_2 ...
installing: harfbuzz-1.8.8-hffaf4a1_0 ...
installing: html5lib-1.0.1-py27_0 ...
installing: hyperlink-18.0.0-py27_0 ...
installing: jedi-0.12.1-py27_0 ...
installing: llvmlite-0.24.0-py27hdbcaa40_0 ...
installing: more-itertools-4.3.0-py27_0 ...
installing: multipledispatch-0.6.0-py27_0 ...
installing: networkx-2.1-py27_0 ...
installing: nltk-3.3.0-py27_0 ...
installing: openpyxl-2.5.6-py27_0 ...
installing: packaging-17.1-py27_0 ...
installing: partd-0.3.8-py27_0 ...
installing: pathlib2-2.3.2-py27_0 ...
installing: pexpect-4.6.0-py27_0 ...
installing: pillow-5.2.0-py27heded4f4_0 ...
installing: pyasn1-modules-0.2.2-py27_0 ...
installing: pycairo-1.17.1-py27h2a1e443_0 ...
installing: pyqt-5.9.2-py27h05f1152_2 ...
installing: python-dateutil-2.7.3-py27_0 ...
installing: qtawesome-0.4.4-py27_0 ...
installing: setuptools-40.2.0-py27_0 ...
installing: singledispatch-3.4.0.3-py27_0 ...
installing: sortedcollections-1.0.1-py27_0 ...
installing: sphinxcontrib-websupport-1.1.0-py27_1 ...
installing: ssl_match_hostname-3.5.0.1-py27_2 ...
installing: sympy-1.2-py27_0 ...
installing: traceback2-1.4.0-py27_0 ...
installing: traitlets-4.3.2-py27_0 ...
installing: zict-0.1.3-py27_0 ...
installing: backports.functools_lru_cache-1.5-py27_1 ...
installing: bleach-2.1.4-py27_0 ...
installing: clyent-1.2.2-py27_1 ...
installing: cryptography-2.3.1-py27hc365091_0 ...
installing: cython-0.28.5-py27hf484d3e_0 ...
installing: get_terminal_size-1.0.0-haa9412d_0 ...
installing: gevent-1.3.6-py27h7b6447c_0 ...
installing: isort-4.3.4-py27_0 ...
installing: jinja2-2.10-py27_0 ...
installing: jsonschema-2.6.0-py27_0 ...
installing: jupyter_core-4.4.0-py27_0 ...
installing: navigator-updater-0.2.1-py27_0 ...
installing: nose-1.3.7-py27_2 ...
installing: pango-1.42.4-h049681c_0 ...
installing: pickleshare-0.7.4-py27_0 ...
installing: pygments-2.2.0-py27_0 ...
installing: pytest-3.8.0-py27_0 ...
installing: testpath-0.3.1-py27_0 ...
installing: tornado-5.1-py27h14c3975_0 ...
installing: unittest2-1.1.0-py27_0 ...
installing: wheel-0.31.1-py27_0 ...
installing: zope.interface-4.5.0-py27h14c3975_0 ...
installing: astroid-1.6.5-py27_0 ...
installing: distributed-1.23.1-py27_0 ...
installing: flask-1.0.2-py27_1 ...
installing: jupyter_client-5.2.3-py27_0 ...
installing: nbformat-4.4.0-py27_0 ...
installing: pip-10.0.1-py27_0 ...
installing: prompt_toolkit-1.0.15-py27_0 ...
installing: pyopenssl-18.0.0-py27_0 ...
installing: terminado-0.8.1-py27_1 ...
installing: flask-cors-3.0.6-py27_0 ...
installing: ipython-5.8.0-py27_0 ...
installing: nbconvert-5.4.0-py27_1 ...
installing: pylint-1.9.2-py27_0 ...
installing: service_identity-17.0.0-py27h28b3542_0 ...
installing: urllib3-1.23-py27_0 ...
installing: ipykernel-4.9.0-py27_1 ...
installing: requests-2.19.1-py27_0 ...
installing: twisted-18.7.0-py27h14c3975_1 ...
installing: anaconda-client-1.7.2-py27_0 ...
installing: jupyter_console-5.2.0-py27_1 ...
installing: prometheus_client-0.3.1-py27h28b3542_0 ...
installing: qtconsole-4.4.1-py27_0 ...
installing: sphinx-1.7.9-py27_0 ...
installing: spyder-kernels-0.2.6-py27_0 ...
installing: anaconda-navigator-1.9.2-py27_0 ...
installing: anaconda-project-0.8.2-py27_0 ...
installing: notebook-5.6.0-py27_0 ...
installing: numpydoc-0.8.0-py27_0 ...
installing: jupyterlab_launcher-0.11.2-py27h28b3542_0 ...
installing: spyder-3.3.1-py27_1 ...
installing: widgetsnbextension-3.4.1-py27_0 ...
installing: ipywidgets-7.4.1-py27_0 ...
installing: jupyterlab-0.33.11-py27_0 ...
installing: _ipyw_jlab_nb_ext_conf-0.1.0-py27_0 ...
installing: jupyter-1.0.0-py27_7 ...
installing: astropy-2.0.8-py27h035aef0_0 ...
installing: bokeh-0.13.0-py27_0 ...
installing: bottleneck-1.2.1-py27h035aef0_1 ...
installing: conda-4.5.11-py27_0 ...
installing: conda-build-3.15.1-py27_0 ...
installing: datashape-0.5.4-py27_1 ...
installing: h5py-2.8.0-py27h989c5e5_3 ...
installing: imageio-2.4.1-py27_0 ...
installing: matplotlib-2.2.3-py27hb69df0a_0 ...
installing: mkl_fft-1.0.4-py27h4414c95_1 ...
installing: mkl_random-1.0.1-py27h4414c95_1 ...
installing: numpy-1.15.1-py27h1d66e8a_0 ...
installing: numba-0.39.0-py27h04863e7_0 ...
installing: numexpr-2.6.8-py27hd89afb7_0 ...
installing: pandas-0.23.4-py27h04863e7_0 ...
installing: pywavelets-1.0.0-py27hdd07704_0 ...
installing: scipy-1.1.0-py27hfa4b5c9_1 ...
installing: bkcharts-0.2-py27_0 ...
installing: dask-0.19.1-py27_0 ...
installing: patsy-0.5.0-py27_0 ...
installing: pytables-3.4.4-py27ha205bf6_0 ...
installing: scikit-image-0.14.0-py27hf484d3e_1 ...
installing: scikit-learn-0.19.2-py27h4989274_0 ...
installing: odo-0.5.1-py27_0 ...
installing: statsmodels-0.9.0-py27h035aef0_0 ...
installing: blaze-0.11.3-py27_0 ...
installing: seaborn-0.9.0-py27_0 ...
installing: anaconda-5.3.1-py27_0 ...
installation finished.

上面清单中PREFIX就是我指定的安装目录,不是默认目录。从清单中我们很清晰的看到有那些包,比如算法同学最新的几个工具或者库jupyternumpyjupyterlabpandasipython等。

安装 Anaconda3 的时候是否有提醒安装 VScode,我已不记得了,但今天安装 Anaconda2 的时候询问是否安装 VSCode编辑器,服务器端其实是可以不用安装的,但是我想看看结果,也就安装了,安装过程还算不慢,最终成功安装在此处:/usr/share/code

Anaconda2 安装过程的最后,会询问是否切换环境变量,如果选择了切换,就会在当前用户的.bashrc文件中生成一堆 shell,内容如下 :

# added by Anaconda2 5.3.1 installer
# >>> conda init >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="(CONDA_REPORT_ERRORS=false '/home/junwen.zhou/software/anaconda2/bin/conda' shell.bash hook 2> /dev/null)"
if [? -eq 0 ]; then
    \eval "__conda_setup"
else
    if [ -f "/home/junwen.zhou/software/anaconda2/etc/profile.d/conda.sh" ]; then
        . "/home/junwen.zhou/software/anaconda2/etc/profile.d/conda.sh"
        CONDA_CHANGEPS1=false conda activate base
    else
        \export PATH="/home/junwen.zhou/software/anaconda2/bin:PATH"
    fi
fi
unset __conda_setup
# <<< conda init <<<

从脚本中可以看出来,激活的环境名为 base

有时候会出点问题,如果安装完成后,没有自动切换环境,则需要手动激活切换。具体怎么看是否激活 base 环境呢?

  • 查看 python 的路径:which python,如果 python 的路径不是你安装 anaconda 的路径的前缀${PREFIX}/bin/python,其中 $PREFIX换为你选择安装 anaconda 的路径,说明未切换。
  • 查看 Shell 环境,一般切换后,Console 窗口会出现(base) 用户名@机器名:~$这样的信息。

使用 Anaconda2

激活 anaconda 环境

通过 source 命令让 ~/.bashrc 里面的脚本生效,也就意味着会执行conda activate base此条命令,如果唯有执行,则手动执行:

source ~/.bashrc
conda activate base

激活环境,其实就是让安装在 base 环境名下的资源路径生效,即将各种安装包的路径加入到环境变量 PATH 中去。环境名其实就是环境变量的分组,用过 SwitchHosts 的同学应该知道,我们会把不同开发环境的 hosts 配置在不同的分组里,可以自由切换,虽然不是那么准确的比喻,但也就是这么一回事。conda 所有的虚拟环境都在 anaconda安装目录下的envs目录中。

$ conda info -e

# conda environments:
#
base                     /home/junwen.zhou/software/anaconda2
py3                   *  /home/junwen.zhou/software/anaconda2/envs/py3

退出 anaconda 的环境

有激活,就有失效,既然能灵活切换,必然有对应的命令:

conda deactivate

anaconda 的其他命令

命令 说明
conda clean 移除未使用的包和缓存
conda config xxx 配置 conda,后面参数为配置文件路径,默认当前用户目录下的.condarc
conda create -n abc python=3.6 创建一个新的环境,abc 为环境名,可以自己随意定义,python 版本为 3.6,具体可以使用 conda create -h 查看帮助信息
conda install yaml 安装依赖包,yaml 是 python 的一个依赖模块
conda list 查看安装依赖包列表信息
conda remove yaml 移除安装的模块,相同的命令还有conda uninstall yaml
conda search xyz 搜索 xyz 模块
conda update 升级 conda 依赖包或模块,此外 conda upgrade 也是相同的作用
conda -V 查看 conda 版本信息
conda -h [|help] 帮助信息
conda info -e 查看当前环境

下面这些命令使用的较少,如果有需要,可以通过-h 参数查看手册:

  build
  convert
  develop
  env
  index
  inspect
  metapackage
  render
  server
  skeleton

比如:conda build -h 查看该命令的帮助信息:

 conda-build [-h] [-V] [-n] [--output] [--python PYTHON] [--perl PERL]
                   [--numpy NUMPY] [--R R_BASE] [--lua LUA]
                   [--bootstrap BOOTSTRAP]
                   [--append-file APPEND_SECTIONS_FILE]
                   [--clobber-file CLOBBER_SECTIONS_FILE]
                   [-m VARIANT_CONFIG_FILES] [-e EXCLUSIVE_CONFIG_FILES]
                   [--old-build-string] [--check] [--no-anaconda-upload]
                   [--no-include-recipe] [-s] [-t] [--no-test] [-b] [-p]
                   [--skip-existing] [--keep-old-work] [--dirty] [-q]
                   [--debug] [--token TOKEN] [--user USER] [--label LABELS]
                   [--no-force-upload] [--password PASSWORD] [--sign SIGN]
                   [--sign-with SIGN_WITH] [--identity IDENTITY]
                   [--config-file CONFIG_FILE] [--repository REPOSITORY]
                   [--no-activate] [--no-build-id] [--croot CROOT] [--verify]
                   [--no-verify] [--strict-verify]
                   [--output-folder OUTPUT_FOLDER]
                   [--no-prefix-length-fallback] [--prefix-length-fallback]
                   [--prefix-length _PREFIX_LENGTH] [--no-locking]
                   [--no-remove-work-dir] [--error-overlinking]
                   [--no-error-overlinking] [--long-test-prefix]
                   [--no-long-test-prefix] [--keep-going]
                   [--cache-dir CACHE_DIR] [--no-copy-test-source-files]
                   [--merge-build-host] [--stats-file STATS_FILE]
                   [--extra-deps EXTRA_DEPS [EXTRA_DEPS ...]] [-c CHANNEL]
                   [--override-channels]
                   RECIPE_PATH [RECIPE_PATH ...]

Tool for building conda packages. A conda package is a binary tarball
containing system-level libraries, Python modules, executable programs, or
other components. conda keeps track of dependencies between packages and
platform specifics, making it simple to create working environments from
different sets of packages.

Anaconda2 更换镜像源

conda 安装 python 模块,是从官方镜像仓库中下载依赖包进行安装,和安装 conda 同样的问题,网速让人难受,可以通过更换镜像源提高下载速度。

查看配置

conda config --show

通过上面的命令可以查看到 conda 的默认配置信息,其中最为关注的则是channels镜像源配置信息:

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - defaults
custom_channels:
  pkgs/r: https://repo.anaconda.com
  pkgs/main: https://repo.anaconda.com
  pkgs/pro: https://repo.anaconda.com
  pkgs/free: https://repo.anaconda.com
custom_multichannels:
  local: []
  defaults: ["https://repo.anaconda.com/pkgs/main", "https://repo.anaconda.com/pkgs/free", "https://repo.anaconda.com/pkgs/r", "https://repo.anaconda.com/pkgs/pro"]
default_channels:
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/free
  - https://repo.anaconda.com/pkgs/r
  - https://repo.anaconda.com/pkgs/pro

更新镜像源

输入下面这两行命令即可换源成功。

请注意换源后,一旦更新库更新库会把所有的库在更新一遍。

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes

更新依赖模块

# 更新所有模块
conda update -all
# 更新 conda 自身
conda update conda

# 更新 base 环境的 defaults 通道(镜像源)的 conda 版本
conda update -n base -c defaults conda

pip 和 conda 同时使用

# 查看已经安装的包:
pip list 或者 conda list

# 安装和更新:
pip install requests
pip install requests –upgrade
# 或者
conda install requests
conda update requests

conda create -n abc -c tisinghua python=3.7

发表评论