Python工具篇(一):安裝包以及虛擬環境管理的利器Conda

conda官網文檔參考鏈接:https://conda.io/projects/conda/en/latest/index.html

本文把文檔中一些我覺得比較有用的知識搬運一下,以便之後查閱

Concepts一節都是一些Conda中用到的常見概念,Getting started with conda一節中是一些比較實用的操作,想看conda命令直接跳到該節即可

目錄

Installation

The fastest way to obtain conda is to install Miniconda, a mini version of Anaconda that includes only conda and its dependencies. If you prefer to have conda plus over 7,500 open-source packages, install Anaconda.

  • To see which Python installation is currently set as the default:
    On Windows, open an Anaconda Prompt and run—where python.
    On macOS and Linux, open the terminal and run—which python.

Concepts

Conda is an open-source package management system and environment management system. Conda quickly installs, runs, and updates packages and their dependencies. Conda easily creates, saves, loads, and switches between environments on your local computer. With just a few commands, you can set up a totally separate environment to run that different version of Python, while continuing to run your usual version of Python in your normal environment.

Conda commands

The conda command is the primary interface for managing installations of various packages.

Tip: You can abbreviate many frequently used command options that are preceded by 2 dashes (–) to just 1 dash and the first letter of the option. So --name and -n are the same, and --envs and -e are the same.

Command reference (https://conda.io/projects/conda/en/latest/commands.html)

Conda packages

A conda package is a compressed tarball file (.tar.bz2) or .conda file that contains:

  • system-level libraries.
  • Python or other modules.
  • executable programs and other components.
  • metadata under the info/ directory.
  • a collection of files that are installed directly into an install prefix.

The .conda file format was introduced in conda 4.7 as a more compact, and thus faster, alternative to a tarball.

Metapackages

When a conda package is used for metadata alone and does not contain any files, it is referred to as a metapackage. The metapackage may contain dependencies to several core, low-level libraries and can contain links to software files that are automatically downloaded when executed. Metapackages are used to capture metadata and make complicated package specifications simpler.

Anaconda metapackage

The Anaconda metapackage collects together all the packages in the Anaconda installer. The command conda create -n envname anaconda creates an environment that exactly matches what would be created from the Anaconda installer.

Conda package specification

在我的電腦裏,所有包都存在D:\Software\Anaconda3\pkgs目錄下

Package structure

.
├── bin
│   └── pyflakes
├── info
│   ├── LICENSE.txt
│   ├── files
│   ├── index.json
│   ├── paths.json
│   └── recipe
└── lib
    └── python3.5
  • bin contains relevant binaries for the package.
  • lib contains the relevant library files (eg. the .py files).
  • info contains package metadata.

Package metadata

The info/ directory contains all metadata about a package.

  • info/files
    Lists all files that are part of the package itself, 1 per line. All of these files need to get linked into the environment. Any files in the package that are not listed in this file are not linked when the package is installed.
Lib/site-packages/torchvision-0.6.0-py3.7.egg-info/PKG-INFO
Lib/site-packages/torchvision-0.6.0-py3.7.egg-info/SOURCES.txt
Lib/site-packages/torchvision-0.6.0-py3.7.egg-info/dependency_links.txt
Lib/site-packages/torchvision-0.6.0-py3.7.egg-info/not-zip-safe
Lib/site-packages/torchvision-0.6.0-py3.7.egg-info/requires.txt
Lib/site-packages/torchvision-0.6.0-py3.7.egg-info/top_level.txt
...
  • info/index.json
    metadata about the package including platform, version, dependencies, name and build info
{
  "arch": "x86_64",
  "build": "py37hfa4b5c9_1",
  "build_number": 1,
  "depends": [
    "depend > 1.1.1"
  ],
  "license": "BSD 3-Clause",
  "name": "fun-packge",
  "platform": "linux",
  "subdir": "linux-64",
  "timestamp": 1535416612069,
  "version": "0.0.0"
}
  • info/paths.json
    a list of files in the package, along with their associated SHA-256, size in bytes, and the type of path (eg. hardlink vs. softlink)
    由此可見,包中的模塊實際都存放在D:\Software\Anaconda3\Lib\site-packages目錄下
{
  "paths": [
    {
      "_path": "lib/python3.7/site-packages/fun-packge/__init__.py",
      "path_type": "hardlink",
      "sha256": "76f3b6e34feeb651aff33ca59e0279c4eadce5a50c6ad93b961c846f7ba717e9",
      "size_in_bytes": 2067
    },
    {
      "_path": "lib/python3.7/site-packages/fun-packge/__config__.py",
      "path_type": "hardlink",
      "sha256": "348e3602616c1fe4c84502b1d8cf97c740d886002c78edab176759610d287f06",
      "size_in_bytes": 87519
    },
    ...
}

Conda channels

conda repository(https://repo.anaconda.com/pkgs/)

A cloud-based repository that contains 7,500+ open-source certified packages that are easily installed locally with the conda install command.

What is a “conda channel”?

Conda channels are the locations where packages are stored. They serve as the base for hosting and managing packages. Conda packages are downloaded from remote channels, which are URLs to directories containing conda packages. The conda command searches a default set of channels, and packages are automatically downloaded and updated from https://repo.anaconda.com/pkgs/. You can modify what remote channels are automatically searched.

Conda environments

Introduction

A conda environment is a directory that contains a specific collection of conda packages that you have installed. For example, you may have one environment with NumPy 1.7 and its dependencies, and another environment with NumPy 1.6 for legacy testing. If you change one environment, your other environments are not affected. You can easily activate or deactivate environments, which is how you switch between them. You can also share your environment with someone by giving them a copy of your environment.yaml file.

Conda directory structure

ROOT_DIR: The directory that Anaconda or Miniconda was installed into.
/pkgs: Also referred to as PKGS_DIR. This directory contains decompressed packages, ready to be linked in conda environments.
/envs: The system location for additional conda environments to be created.

The following subdirectories comprise the default Anaconda environment:
/bin /include /lib /share
Other conda environments usually contain the same subdirectories as the default environment.

Conda performance

Conda’s performance can be affected by a variety of things. Unlike many package managers, Anaconda’s repositories generally don’t filter or remove old packages from the index. This allows old environments to be easily recreated. However, it does mean that the index metadata is always growing, and thus conda becomes slower as the number of packages increases.

Conda Provides commonly used data science libraries and tools, such as R, NumPy, SciPy, and TensorFlow. These are built using optimized, hardware-specific libraries (such as Intel’s MKL or NVIDIA’s CUDA) which speed up performance without code changes.

Improving conda performance

  • Specifying very broad package specs?
    Be more specific. Letting conda filter more candidates makes it faster. For example, instead of numpy, we recommend numpy=1.15 or, even better, numpy=1.15.4.
  • Feeling frustrated with “verifying transaction” and also feeling lucky?
    Run conda config --set safety_checks disabled.
  • Getting strange mixtures of defaults and conda-forge?
    Run conda config --set channel_priority strict.
    Setting strict channel priority makes it so that if a package exists on a channel, conda will ignore all packages with the same name on lower priority channels.This makes things go faster by eliminating possible mixed solutions.
  • Observing that an Anaconda or Miniconda installation is getting slower over time?
    Create a fresh environment. As environments grow, they become harder and harder to solve. Working with small, dedicated environments can be much faster.
  • Remove unused cached files including unused packages
conda clean --all

Getting started with conda

Viewing command-line help

conda --help

OR

conda -h

EXAMPLE: To see help for the create command, in your terminal window or an Anaconda Prompt, run:

conda create -h

Managing conda

SEE ALSO: Getting started with Anaconda Navigator, a graphical user interface that lets you use conda in a web-like interface without having to enter manual commands.

Verify that conda is installed and running on your system by typing:

conda --version

OR

conda info

Update conda to the current version. Type the following:

使用這個命令的時候千萬要注意不能斷網!!!不然conda可能就要出錯了~網絡不好者慎用。本人親身經歷,update到一半突然斷網,之後conda update這個命令就瘋狂報錯

conda update conda

We recommend that you always keep conda updated to the latest version.

Managing environments

When you begin using conda, you already have a default environment named base. You don’t want to put programs into your base environment, though. Create separate environments to keep your programs isolated from each other.

  1. Create a new environment and install a package in it.
  • Creating an environment with commands
# name the environment myenv and install the package scipy
# This environment uses the same version of Python that you are currently using because you did not specify a version.
conda create --name myenv scipy=0.15.0
# create an environment with a specific version of Python
conda create -n myenv python=3.6 scipy=0.15.0 astroid babel
  • Create an environment from an environment.yml file
# Create the environment from the environment.yml file
conda env create -f environment.yml

The first line of the yml file sets the new environment’s name.

  1. To use, or “activate” the new environment, type the following:
conda activate myenv 

Activation entails two primary functions: adding entries to PATH for the environment and running any activation scripts that the environment may contain. These activation scripts are how packages can set arbitrary environment variables that may be necessary for their operation.

Now that you are in your myenv environment, any conda commands you type will go to that environment until you deactivate it.

  1. To see a list of all your environments, type:
conda info --envs
conda env list
  1. Change your current environment back to the default (base):
conda activate

The conda activate command prepends the path of your current environment to the PATH environment variable so that you do not need to type it each time. deactivate removes it. Even when an environment is deactivated, you can still execute programs in that environment by specifying their paths directly, as in ~/anaconda/envs/envname/bin/program_name. When an environment is activated, you can execute the program in that environment with just program_name.

Viewing a list of the packages in an environment

  • If the environment is not activated, in your terminal window or an Anaconda Prompt, run:
conda list -n myenv
  • If the environment is activated, in your terminal window or an Anaconda Prompt, run:
conda list
  • To see if a specific package is installed in an environment, in your terminal window or an Anaconda Prompt, run:
conda list -n myenv scipy

Specifying a location for an environment

# create a new environment in a subdirectory of the current working directory called envs
conda create --prefix ./envs jupyterlab=0.35 matplotlib=3.1 numpy=1.16
# activate an environment created with a prefix
conda activate ./envs

There are a few things to be aware of when placing conda environments outside of the default envs folder.

  • Conda can no longer find your environment with the --name flag. You’ll generally need to pass the --prefix flag along with the environment’s full path to find the environment.
  • Specifying an install path when creating your conda environments makes it so that your command prompt is now prefixed with the active environment’s absolute path rather than the environment’s name.

After activating an environment using its prefix, your prompt will look similar to the following:

(/absolute/path/to/envs) $

This can result in long prefixes:

(/Users/USER_NAME/research/data-science/PROJECT_NAME/envs) $

To remove this long prefix in your shell prompt, modify the env_prompt setting in your .condarc file:

$ conda config --set env_prompt '({name})'

Updating an environment

update the contents of your environment.yml file accordingly and then run the following command:

# The --prune option causes conda to remove any dependencies that are no longer required from the environment.
$ conda env update --prefix ./env --file environment.yml  --prune

Sharing an environment

Cloning an environment

conda create --name myclone --clone myenv

Building identical conda environments

Export an environment with exact package versions for one OS

  1. Run conda list --explicit to produce a spec list such as:
# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: osx-64
@EXPLICIT
https://repo.anaconda.com/pkgs/free/osx-64/mkl-11.3.3-0.tar.bz2
https://repo.anaconda.com/pkgs/free/osx-64/numpy-1.11.1-py35_0.tar.bz2
https://repo.anaconda.com/pkgs/free/osx-64/openssl-1.0.2h-1.tar.bz2
https://repo.anaconda.com/pkgs/free/osx-64/pip-8.1.2-py35_0.tar.bz2
https://repo.anaconda.com/pkgs/free/osx-64/python-3.5.2-0.tar.bz2
https://repo.anaconda.com/pkgs/free/osx-64/readline-6.2-2.tar.bz2
https://repo.anaconda.com/pkgs/free/osx-64/setuptools-25.1.6-py35_0.tar.bz2
https://repo.anaconda.com/pkgs/free/osx-64/sqlite-3.13.0-0.tar.bz2
https://repo.anaconda.com/pkgs/free/osx-64/tk-8.5.18-0.tar.bz2
https://repo.anaconda.com/pkgs/free/osx-64/wheel-0.29.0-py35_0.tar.bz2
https://repo.anaconda.com/pkgs/free/osx-64/xz-5.2.2-0.tar.bz2
https://repo.anaconda.com/pkgs/free/osx-64/zlib-1.2.8-3.tar.bz2

An explicit spec file is not usually cross platform, and therefore has a comment at the top such as # platform: osx-64 showing the platform where it was created.

  1. To create this spec list as a file in the current working directory, run:
conda list --explicit > spec-file.txt
  1. use the spec file to create an identical environment (an environment based on
    exact package versions) on the same machine or another machine:
conda create --name myenv --file spec-file.txt
  1. use the spec file to install its listed packages into an existing environment:
conda install --name myenv --file spec-file.txt

Conda does not check architecture or dependencies when installing from a spec file. To ensure that the packages work correctly, make sure that the file was created from a working environment, and use it on the same architecture, operating system, and platform, such as linux-64 or osx-64.

Exporting the environment.yml file

Export an environment to a YAML file that can be read on Windows, macOS, and Linux

# This file handles both the environment's pip packages and conda packages.
conda env export --name ENVNAME > environment.yml

Exporting an environment file across platforms

conda env export --from-history

This will only include packages that you’ve explicitly asked for, as opposed to including every package in your environment.

For example, if you create an environment and install Python and a package:

conda install python=3.7 codecov

This will download and install numerous additional packages to solve for dependencies. This will introduce packages that may not be compatible across platforms.

If you use conda env export, it will export all of those packages. However, if you use conda env export --from-history, it will only export those you specifically chose:

(env-name) ➜  ~ conda env export --from-history
name: env-name
channels:
  - conda-forge
  - defaults
dependencies:
  - python=3.7
  - codecov
prefix: /Users/username/anaconda3/envs/env-name

Creating an environment file manually

name: stats2
channels:
  - javascript
dependencies:
  - python=3.6   # or 2.7
  - bokeh=0.9.2
  - numpy=1.9.*
  - nodejs=0.10.*
  - flask
  - pip:
    - Flask-Testing

Note the use of the wildcard * when defining the patch version number. Defining the version number by fixing the major and minor version numbers while allowing the patch version number to vary allows us to use our environment file to update our environment to get any bug fixes whilst still maintaining consistency of software environment.

Creating an environment from YAML file

conda env create --file envname.yml

Create an environment from the file named environment.yml in the current directory

conda env create

Restoring an environment

Conda keeps a history of all the changes made to your environment, so you can easily “roll back” to a previous version. To list the history of each change to the current environment: conda list --revisions

To restore environment to a previous revision: conda install --revision=REVNUM or conda install --rev REVNUM.

Example: If you want to restore your environment to revision 8, run conda install --rev 8.

Removing an environment

conda remove --name myenv --all

You may instead use conda env remove --name myenv.

Managing Python

When you create a new environment, conda installs the same Python version you used when you downloaded and installed Anaconda(For the installers “Anaconda3” or “Miniconda3,” the default is 3.7).

Viewing a list of available Python versions

conda search python

Installing a different version of Python

# anaconda is the metapackage that includes all of the Python packages comprising the Anaconda distribution
conda create -n py36 python=3.6 anaconda

Updating or upgrading Python

If you are in an environment with Python version 3.4.2, the following command updates Python to the latest version in the 3.4 branch:

conda update python

The following command upgrades Python to another branch—3.6—by installing that version of Python:

conda install python=3.6

Managing virtual packages

“Virtual” packages are injected into the conda solver to allow real packages to depend on features present on the system that cannot be managed directly by conda, like system driver versions or CPU features. Virtual packages are not real packages and not displayed by conda list.

The currently supported list of virtual packages includes:

  • __cuda: Maximum version of CUDA supported by the display driver.
  • __osx: OSX version if applicable.
  • __glibc: Version of glibc supported by the OS.

Listing detected virtual packages

conda info

If a package is detected, you will see it listed in the virtual packages section, as shown in this example:

 active environment : base
   active env location : /Users/demo/dev/conda/devenv
           shell level : 1
      user config file : /Users/demo/.condarc
populated config files : /Users/demo/.condarc
         conda version : 4.6.3.post8+8f640d35a
   conda-build version : 3.17.8
        python version : 3.7.2.final.0
      virtual packages : __cuda=10.0
      base environment : /Users/demo/dev/conda/devenv (writable)
          channel URLs : https://repo.anaconda.com/pkgs/main/osx-64
                         https://repo.anaconda.com/pkgs/main/noarch
                         https://repo.anaconda.com/pkgs/free/osx-64
                         https://repo.anaconda.com/pkgs/free/noarch
                         https://repo.anaconda.com/pkgs/r/osx-64
                         https://repo.anaconda.com/pkgs/r/noarch
         package cache : /Users/demo/dev/conda/devenv/pkgs
                         /Users/demo/.conda/pkgs
      envs directories : /Users/demo/dev/conda/devenv/envs
                         /Users/demo/.conda/envs
              platform : osx-64
            user-agent : conda/4.6.3.post8+8f640d35a requests/2.21.0 CPython/3.7.2 Darwin/17.7.0 OSX/10.13.6
               UID:GID : 502:20
            netrc file : None
          offline mode : False

Managing channels

Adding a channel

The following command adds the channel “new_channel” to the top of the channel list, making it the highest priority:

conda config --add channels new_channel

Conda has an equivalent command:

conda config --prepend channels new_channel

Conda also has a command that adds the new channel to the bottom of the channel list, making it the lowest priority:

conda config --append channels new_channel

國內的源(使用清華大學開源軟件鏡像站):

https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro/
https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/

還有一些用得上的深度學習框架(百度飛漿和pytorch)

https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/Paddle/
https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

Specifying channels when installing packages

We use conda-forge as an example channel. Conda-forge is a community channel made up of thousands of contributors. Conda-forge itself is analogous to PyPI but with a unified, automated build infrastructure and more peer review of recipes.

conda install scipy --channel conda-forge

specify multiple channels by passing the argument multiple times(Priority decreases from left to right - the first argument is higher priority than the second):

conda install scipy --channel conda-forge --channel bioconda

only search the specified channel(s), rather than any channels configured in .condarc. This also ignores conda’s default channels.

conda search scipy --channel file:/<path to>/local-channel --override-channels

Managing packages

Searching for packages

  • Check to see if a package you have not installed named “beautifulsoup4” is available from the Anaconda repository (must be connected to the Internet):
conda search beautifulsoup4
  • To see if a specific package, such as iminuit, exists in a specific channel, such as http://conda.anaconda.org/mutirri, and is available for installation:
conda search --override-channels --channel http://conda.anaconda.org/mutirri iminuit

Package match specifications

conda install python=3.4. Internally, python=3.4 is translated to python 3.4*.

Package dependencies are specified using a match specification. A match specification is a space-separated string of 1, 2, or 3 parts:

  • The first part is always the exact name of the package.
  • The second part refers to the version and may contain special characters:
special characters example
| means OR 1.0 | 1.2 matches version 1.0 or 1.2
* matches 0 or more characters in the version string 1.4* matches 1.4 and 1.4.1b2…
<, >, <=, >=, == and != <=1.0 matches 0.9, 0.9.1, and 1.0
, means AND >=2,< 3 matches all packages in the 2 series

, has higher precedence than |, so >=1,<2|>3 means greater than or equal to 1 AND less than 2 or greater than 3

Remember that the version specification cannot contain spaces, as spaces are used to delimit the package, version, and build string in the whole match specification. python >= 2.7 is an invalid match specification.

When using the command line, put double quotes around any package version specification that contains the space character or any of the following characters: <, >, *, or |.

conda install numpy=1.11 # matches 1.11, 1.11.0, 1.11.1, 1.11.2, 1.11.18, and so on.
conda install numpy==1.11 # matches 1.11, 1.11.0, 1.11.0.0, and so on.
conda install "numpy>1.11"
conda install "numpy=1.11.1|1.11.3"
conda install "numpy>=1.8,<2"
  • The third part is always the exact build string. When there are 3 parts, the second part must be the exact version.

Installing packages

  • To install a specific package such as SciPy into an existing environment “myenv”:
conda install --name myenv scipy
  • If you do not specify the environment name, the package installs into the current environment (During the install process, files are extracted into the specified environment, defaulting to the current environment if none is specified.):
conda install scipy
  • To install a specific version of a package such as SciPy:
conda install scipy=0.15.0
  • To install multiple packages at once, such as SciPy and cURL:
conda install scipy curl
  • Check to see if the newly installed program is in this environment:
conda list

Installing conda packages offline

Installing packages directly from the file does not resolve dependencies.

conda install /path-to-package/package-filename.tar.bz2/ 

Installing packages from Anaconda.org

Packages that are not available using conda install can be obtained from Anaconda.org, a package management service for both public and private package repositories.

To install a package from Anaconda.org:

  1. In a browser, go to http://anaconda.org.
  2. To find the package named bottleneck, type bottleneck in the top-left box named Search Packages.
  3. Find the package that you want and click it to go to the detail page.
    The detail page displays the name of the channel. In this example it is the “pandas” channel.
  4. Now that you know the channel name, use the conda install command to install the package. In your terminal window or an Anaconda Prompt, run:
conda install -c pandas bottleneck

Installing non-conda packages

If a package is not available from conda or Anaconda.org, you may be able to find and install the package via conda-forge or with another package manager like pip.

Pip packages do not have all the features of conda packages and we recommend first trying to install any package with conda. If the package is unavailable through conda, try finding and installing it with conda-forge.

If you still cannot install the package, you can try installing it with pip. The differences between pip and conda packages cause certain unavoidable limits in compatibility but conda works hard to be as compatible with pip as possible.

Viewing a list of installed packages

To list all of the packages in the active environment:

conda list

To list all of the packages in a deactivated environment:

conda list -n myenv

Updating packages

  • To update a specific package:
# update to the latest compatible version
conda update biopython
  • To update Python:
conda update python
  • To update conda itself:
conda update conda

Conda updates to the highest version in its series, so Python 2.7 updates to the highest available in the 2.x series and 3.6 updates to the highest available in the 3.x series.

Example:
If Python 2.7.0 is currently installed, and the latest version of Python 2 is 2.7.5, then conda update python installs Python 2.7.5. It does not install Python 3.5.2.
If Python 3.4.0 is currently installed, and the latest version of Python is 3.5.2, then conda install python=3 installs Python 3.5.2.

conda update always installs the highest version with the same major version number, whereas conda install always installs the highest version.

  • To update the Anaconda metapackage:
conda update conda
conda update anaconda

Update all packages to the latest version of Anaconda. Will install stable and compatible versions, not necessarily the very latest.

Adding default packages to new environments automatically

conda config --add create_default_packages PACKAGENAME1 PACKAGENAME2

You can also edit the .condarc file with a list of packages to create by default.

You can override this option at the command prompt with the --no-default-packages flag.

Removing packages

  • To remove a package such as SciPy in an environment such as myenv:
conda remove -n myenv scipy
  • To remove a package such as SciPy in the current environment:
conda remove scipy
  • To remove multiple packages at once, such as SciPy and cURL:
conda remove scipy curl

Using the .condarc conda configuration file

The conda configuration file .condarc is an optional runtime configuration file that allows advanced users to configure various aspects of conda, such as which channels it searches for packages, proxy settings, and environment directories. For all of the conda configuration options, see the configuration page.

Creating and editing

The .condarc file is not included by default, but it is automatically created in your home directory the first time you run the conda config command.

The .condarc configuration file follows simple YAML syntax.

conda config --add channels conda-forge

Alternatively, you can open a text editor. Name the new file .condarc and save it to your user home directory or root directory. To edit the .condarc file, open it from your home or root directory and make edits in the same way you would with any other text file. If the .condarc file is in the root environment, it will override any in the home directory.

You can find information about your .condarc file by typing conda info in your terminal or Anaconda Prompt. This will give you information about your .condarc file, including where it is located.

You can also download a sample .condarc file to edit in your editor and save to your user home directory or root directory.

To set configuration options, edit the .condarc file directly or use the conda config --set command.

conda config --set auto_update_conda False

For a complete list of conda config commands, use the conda config --help command or see the command reference.You can also see the conda channel configuration for more information.

Searching for .condarc

Conda looks in the following locations for a .condarc file:

if on_win:
 SEARCH_PATH = (
     'C:/ProgramData/conda/.condarc',
     'C:/ProgramData/conda/condarc',
     'C:/ProgramData/conda/condarc.d',
 )
 else:
 SEARCH_PATH = (
     '/etc/conda/.condarc',
     '/etc/conda/condarc',
     '/etc/conda/condarc.d/',
     '/var/lib/conda/.condarc',
     '/var/lib/conda/condarc',
     '/var/lib/conda/condarc.d/',
  )

 SEARCH_PATH += (
     '$CONDA_ROOT/.condarc',
     '$CONDA_ROOT/condarc',
     '$CONDA_ROOT/condarc.d/',
     '~/.conda/.condarc',
     '~/.conda/condarc',
     '~/.conda/condarc.d/',
     '~/.condarc',
     '$CONDA_PREFIX/.condarc',
     '$CONDA_PREFIX/condarc',
     '$CONDA_PREFIX/condarc.d/',
     '$CONDARC',
 )

CONDA_ROOT is the path for your base conda install. CONDA_PREFIX is the path to the current active environment.

General configuration

Listing conda configuration

conda config --show

Channel locations (channels)

channels:
  - <anaconda_dot_org_username> # Non-URL channels are interpreted as Anaconda.org user names
  - http://some.custom/channel
  - file:///some/local/directory
  - defaults # Use defaults to automatically include all default channels.

To select channels for a single environment, put a .condarc file in the root directory of that environment (or use the --env option when using conda config).

Default channels (default_channels)

Normally the defaults channel points to several channels at the repo.anaconda.com repository, but if default_channels is defined, it sets the new list of default channels.

default_channels:
  - http://some.custom/channel
  - file:///some/local/directory

Set a channel alias (channel_alias)

Whenever you use the -c or --channel flag to give conda a channel name that is not a URL, conda prepends the channel_alias to the name that it was given. The default channel_alias is https://conda.anaconda.org.

If channel_alias is set to https://my.anaconda.repo:8080/conda/, then a user who runs the command conda install -c conda-forge some-package will install the package some-package from https://my.anaconda.repo:8080/conda/conda-forge.

make conda install the newest version of a package in any listed channel(channel_priority)

By default, conda prefers packages from a higher priority channel over any version from a lower priority channel. Therefore, you can now safely put channels at the bottom of your channel list to provide additional packages that are not in the default channels and still be confident that these channels will not override the core package set.

To make conda install the newest version of a package in any listed channel:

  • Add channel_priority: false to your .condarc file.
    OR
  • Run the equivalent command:
conda config --set channel_priority false

Always add packages by default (create_default_packages)

When creating new environments, add the specified packages by default. You can override this option at the command prompt with the --no-default-packages flag. The default is to not include any packages.

conda create --no-default-packages -n myenv python
create_default_packages:
  - pip
  - ipython
  - scipy=0.15.0

Disable updating of dependencies (update_dependencies)

By default, conda install updates the given package to the latest version and installs any dependencies necessary for that package. However, if dependencies that satisfy the package’s requirements are already installed, conda will not update those packages to the latest version.

In this case, if you would prefer that conda update all dependencies to the latest version that is compatible with the environment, set update_dependencies to True.

The default is False.

update_dependencies: True

Specify environment directories (envs_dirs)

Specify directories in which environments are located. If this key is set, the root prefix envs_dir is not used unless explicitly included. This key also determines where the package caches are located.

For each envs here, envs/pkgs is used as the pkgs cache, except for the standard envs directory in the root directory, for which the normal root_dir/pkgs is used.

envs_dirs:
  - ~/my-envs
  - /opt/anaconda/envs

Specify package directories (pkgs_dirs)

Specify directories in which packages are located. If this key is set, the root prefix pkgs_dirs is not used unless explicitly included.

pkgs_dirs:
  - /opt/anaconda/pkgs

Activate your base environment when it first starts up(auto_activate_base)

This setting controls whether or not conda activates your base environment when it first starts up. You’ll have the conda command available either way, but without activating the environment, none of the other programs in the environment will be available until the environment is activated with conda activate base.

Obtaining information from the .condarc file

It may be necessary to add the “force” option -f to the following commands.

To get all keys and their values:

conda config --get

To get the value of a specific key, such as channels:

conda config --get channels

To add a new value, such as http://conda.anaconda.org/mutirri, to a specific key, such as channels:

conda config --add channels http://conda.anaconda.org/mutirri

To remove an existing value, such as http://conda.anaconda.org/mutirri from a specific key, such as channels:

conda config --remove channels http://conda.anaconda.org/mutirri

To remove a key, such as channels, and all of its values:

conda config --remove-key channels

Configuring number of threads

You can use your .condarc file or environment variables to add configuration to control the number of threads. You may want to do this to tweak conda to better utilize your system. If you have a very fast SSD, you might increase the number of threads to shorten the time it takes for conda to create environments and install/remove packages.

repodata_threads

  • Default number of threads: None
  • Threads used when downloading, parsing, and creating repodata structures from repodata.json files. Multiple downloads from different channels may occur simultaneously. This speeds up the time it takes to start solving.

verify_threads

  • Default number of threads: 1
  • Threads used when verifying the integrity of packages and files to be installed in your environment. Defaults to 1, as using multiple threads here can run into problems with slower hard drives.

execute_threads

  • Default number of threads: 1
  • Threads used to unlink, remove, link, or copy files into your environment. Defaults to 1, as using multiple threads here can run into problems with slower hard drives.

default_threads

  • Default number of threads: None
  • When set, this value is used for all of the above thread settings. With its default setting (None), it does not affect the other settings.

At your terminal:

conda config --set repodata_threads 2

In .condarc:

verify_threads: 4

Enabling Bash tab completion

conda install argcomplete

Add the following code to your bash profile:

eval "$(register-python-argcomplete conda)"

Improving interoperability with pip

Issues may arise when using pip and conda together.

The conda 4.6.0 release added improved support for interoperability between conda and pip. This feature is still experimental and is therefore off by default.

With this interoperability, conda can use pip-installed packages to satisfy dependencies, cleanly remove pip-installed software, and replace them with conda packages when appropriate.

conda config --set pip_interop_enabled True # This may slow down conda

Using conda with Travis CI

Troubleshooting

The system cannot find the path specified on Windows

  • Cause
    PATH does not contain entries for all of the necessary conda directories. PATH may have too many entries from 3rd party software adding itself to PATH at install time, despite the user not needing to run the software via PATH lookup.
  • Solution
    Strip PATH to have fewer entries and activate your environment.

If there’s some software that needs to be found on PATH (you run it via the CLI), we recommend that you create your own batch files to set PATH dynamically within a console session, rather than permanently modifying PATH in the system settings.

For example, a new conda prompt batch file that first strips PATH, then calls the correct activation(activation prepends the conda environment PATH entries, they have priority) procedure could look like:

set
PATH=”%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;<3rd-party-entries>”
call “<miniconda/anaconda root>\Scripts\activate”

如果想在windows命令行裏使用的話,直接運行下面指令即可:

D:\Software\Anaconda3\Scripts\activate

當然也可以手動在環境變量的PATH裏添加以下路徑:

在這裏插入圖片描述
不過最簡單的還是直接用Anaconda Prompt,它會自動添加PATH路徑同時運行激活腳本

Activating your environment for Window command-line

Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load. To activate this environment
please see https://conda.io/activation.

If you receive this warning, you need to activate your environment. To do so on Windows, run: c:\Anaconda3\Scripts\activate base

Windows is extremely sensitive to proper activation. This is because the Windows library loader does not support the concept of libraries and executables that know where to search for their dependencies (RPATH). Instead, Windows relies on a dynamic-link library search order.

If environments are not active, libraries won’t be found and there will be lots of errors.

Conda itself includes some special workarounds to add its necessary PATH entries. This makes it so that it can be called without activation or with any child environment active. In general, calling any executable in an environment without first activating that environment will likely not work.

cheat sheet

conda

conda info # Verify that conda is installed

packages

conda search beautifulsoup4
conda update conda # Update conda to the current version

conda update python
conda install python=3.6

conda update conda # update the Anaconda metapackage
conda update anaconda

conda install scipy --channel conda-forge --channel bioconda
conda install --name myenv scipy
conda install scipy=0.15.0
conda install scipy curl
conda install -c pandas bottleneck # Installing packages from Anaconda.org
conda remove -n myenv scipy
conda remove scipy curl
conda list -n myenv / conda list # Viewing a list of the packages in an environment
conda list -n myenv scipy # To see if a specific package is installed in an environment
conda config --add create_default_packages PACKAGENAME1 PACKAGENAME2

environments

conda create -n py36 python=3.6 anaconda
conda create -n myenv python=3.6 scipy=0.15.0 astroid babel
conda env create -f environment.yml
conda env update --file environment.yml  --prune # Updating an environment
conda env create # Create an environment from the file named environment.yml in the current directory
conda activate myenv
conda activate # Change your current environment back to the default (base)
conda info –envs / conda env list #  see a list of all your environments
conda create --name myclone --clone myenv # Cloning an environment
conda env export --name ENVNAME --from-history > environment.yml
conda create --name myclone --clone myenv
conda list --revisions
conda install --revision=REVNUM / conda install --rev REVNUM
conda remove --name myenv –all / conda env remove --name myenv

channels

conda config --add channels new_channel / conda config --prepend channels new_channel # adds the channel “new_channel” to the top of the channel list, making it the highest priority
conda config --append channels new_channel # adds the new channel to the bottom of the channel list, making it the lowest priority
conda config --remove channels http://conda.anaconda.org/mutirri
conda config --remove-key channels # To remove a key, such as channels, and all of its values
conda config --get channels # To get the value of a specific key, such as channels

國內的源(使用清華大學開源軟件鏡像站):

https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro/
https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/

還有一些用得上的深度學習框架(百度飛漿和pytorch)

https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/Paddle/
https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

configuration

conda config –show
conda config –get # To get all keys and their values
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章