conda create 怎么创建纯净的 Python3.6 环境?

刚接触 conda,安装了 Anaconda3,想用 conda create 命令创建一个只含标准模块和几个必需第三方模块(如 pip、wheel)的 Python3.6 环境。
使用命令:

conda create -n test python=3.6

然而在运行后确认安装模块阶段显示创建的环境中会有很多我觉得完全不需要的模块
如下:

Fetching package metadata .............
Solving package specifications: .

Package plan for installation in environment D:\Anaconda3\envs\test:

The following NEW packages will be INSTALLED:

    asn1crypto:      0.22.0-py36h8e79faa_1      defaults
    ca-certificates: 2017.08.26-h94faf87_0      defaults
    cachecontrol:    0.12.3-py36hfe50d7b_0      defaults
    certifi:         2017.7.27.1-py36h043bc9e_0 defaults
    cffi:            1.10.0-py36hae3d1b5_1      defaults
    chardet:         3.0.4-py36h420ce6e_1       defaults
    colorama:        0.3.9-py36h029ae33_0       defaults
    cryptography:    2.0.3-py36h123decb_1       defaults
    distlib:         0.2.5-py36h51371be_0       defaults
    html5lib:        0.999999999-py36ha09b1f3_0 defaults
    idna:            2.6-py36h148d497_1         defaults
    lockfile:        0.12.2-py36h0468280_0      defaults
    msgpack-python:  0.4.8-py36h58b1e9d_0       defaults
    openssl:         1.0.2l-vc14hcac20b0_2      defaults [vc14]
    packaging:       16.8-py36ha0986f6_1        defaults
    pip:             9.0.1-py36hadba87b_3       defaults
    progress:        1.3-py36hbeca8d3_0         defaults
    pycparser:       2.18-py36hd053e01_1        defaults
    pyopenssl:       17.2.0-py36h15ca2fc_0      defaults
    pyparsing:       2.2.0-py36h785a196_1       defaults
    pysocks:         1.6.7-py36h698d350_1       defaults
    python:          3.6.2-h09676a0_15          defaults
    requests:        2.18.4-py36h4371aae_1      defaults
    setuptools:      36.5.0-py36h65f9e6e_0      defaults
    six:             1.10.0-py36h2c0fdd8_1      defaults
    urllib3:         1.22-py36h276f60a_0        defaults
    vc:              14-h2379b0c_1              defaults
    vs2015_runtime:  14.0.25123-hd4c4e62_1      defaults
    webencodings:    0.5.1-py36h67c50ae_1       defaults
    wheel:           0.29.0-py36h6ce6cde_1      defaults
    win_inet_pton:   1.0.1-py36he67d7fd_1       defaults
    wincertstore:    0.2-py36h7fe50ca_0         defaults

Proceed ([y]/n)?

确认将安装的模块中有很多 web 开发的模块,我完全用不到,怎么取消安装这些多余的模块?
我发现只有在创建 Python2.7、3.5、3.6 时会有多余的模块,而在创建其他 Python 版本(如3.4、2.6)环境时则不会出现这种情况:

C:\Users\Drake> conda create -n test python=3.4

Fetching package metadata .............
Solving package specifications: .

Package plan for installation in environment D:\Anaconda3\envs\test:

The following NEW packages will be INSTALLED:

    pip:            9.0.1-py34_1    defaults
    python:         3.4.5-0         defaults
    setuptools:     27.2.0-py34_1   defaults
    vs2010_runtime: 10.00.40219.1-2 defaults
    wheel:          0.29.0-py34_0   defaults
unintuition
Proceed ([y]/n)?

所以怎样才能用 conda 创建干净的 Python3.6 环境?

Update:
在 hezhiming 的提示下,经过阅读 conda 官方文档中的 管理环境包安装参数 后,我找到了解决方法。

conda create --name myenv --file environment.txt

使用 --file FILE 参数指定安装包,本来想省事用 .yml 文件,但是发现依然会有多余模块安装,所以只用 .txt 文件,文件格式:

@EXPLICIT
https://repo.continuum.io/pkgs/free/osx-64/python-3.5.2-0.tar.bz2
https://repo.continuum.io/pkgs/free/osx-64/pip-8.1.2-py35_0.tar.bz2

上面的链接都是Anaconda镜像列表上的源码包,使用该文件创建环境表示只下载安装文件中的包,不会有任何其他动作。
至此,安装干净的Python环境就完成了。
不过,在创建环境后,在新环境内如果使用 conda install 任何模块时,conda 会自动检测你没有安装哪些 conda 默认安装的环境包并自动加入安装。
所以在创建后的新环境中如果想一直完全由自己控制模块的安装,最好使用 pip,而且Anaconda镜像列表中的包比较少,也不如 pip 能装得多。

总结:安装 Anaconda 后如果想创建干净的 Python 环境,就使用 conda 导入文件安装环境,创建后使用 pip 安装其他需求的模块。
感谢所有回答的人。

阅读 33.4k
3 个回答

我理解:

  1. 要么手动指定需要安装哪些包
  2. 要么手动指定需要排除哪些包

查阅命令行参数


usage: conda-script.py create [-h] [-y] [--dry-run] [-f] [--force-pscheck]
                              [--file FILE] [--no-deps] [-m]
                              [--use-index-cache] [--use-local] [--offline]
                              [--no-pin] [-c CHANNEL] [--override-channels]
                              [-n ENVIRONMENT | -p PATH] [-q] [--copy]
                              [--alt-hint] [--update-dependencies]
                              [--no-update-dependencies] [--show-channel-urls]
                              [--no-show-channel-urls] [--json] [--clone ENV]
                              [--no-default-packages]
                              [package_spec [package_spec ...]]

Create a new conda environment from a list of specified packages. To use the created environment, use 'source activate envname' look in that directory first.  This command requires either the -n NAME or -p PREFIX option.

Options:

positional arguments:
  package_spec          Packages to install into the conda environment.

optional arguments:
  -h, --help            Show this help message and exit.
  -y, --yes             Do not ask for confirmation.
  --dry-run             Only display what would have been done.
  -f, --force           Force install (even when package already installed),
                        implies --no-deps.
  --force-pscheck       No-op. Included for backwards compatibility
                        (deprecated).
  --file FILE           Read package versions from the given file. Repeated
                        file specifications can be passed (e.g. --file=file1
                        --file=file2).
  --no-deps             Do not install dependencies.
  -m, --mkdir           Create the environment directory if necessary.
  --use-index-cache     Use cache of channel index files.
  --use-local           Use locally built packages.
  --offline             Offline mode, don't connect to the Internet.
  --no-pin              Ignore pinned file.
  -c CHANNEL, --channel CHANNEL
                        Additional channel to search for packages. These are
                        URLs searched in the order they are given (including
                        file:// for local directories). Then, the defaults or
                        channels from .condarc are searched (unless
                        --override-channels is given). You can use 'defaults'
                        to get the default packages for conda, and 'system' to
                        get the system packages, which also takes .condarc
                        into account. You can also use any name and the
                        .condarc channel_alias value will be prepended. The
                        default channel_alias is http://conda.anaconda.org/.
  --override-channels   Do not search default or .condarc channels. Requires
                        --channel.
  -n ENVIRONMENT, --name ENVIRONMENT
                        Name of environment (in C:\anaconda3\envs).
  -p PATH, --prefix PATH
                        Full path to environment prefix (default:
                        C:\anaconda3).
  -q, --quiet           Do not display progress bar.
  --copy                Install all packages using copies instead of hard- or
                        soft-linking.
  --alt-hint            Use an alternate algorithm to generate an
                        unsatisfiability hint.
  --update-dependencies, --update-deps
                        Update dependencies (default: True).
  --no-update-dependencies, --no-update-deps
                        Don't update dependencies (default: False).
  --show-channel-urls   Show channel urls (default: True).
  --no-show-channel-urls
                        Don't show channel urls.
  --json                Report all output as json. Suitable for using conda
                        programmatically.
  --clone ENV           Path to (or name of) existing local environment.
  --no-default-packages
                        Ignore create_default_packages in the .condarc file.

Examples:

    conda create -n myenv sqlite

构造命令: conda create --no-default-packages --name "test_conda" --prefix "$full_python_path" "pkg1" "pkg2" "pkg3"

启示:

  1. --no-default-packages Ignore create_default_packages in the .condarc file.: 也许可以对这个文件做点手脚(.condarc)

python2.7(最后一个2字头的版本) 3.5(3.5以前称为v2) 3.6(v3)分别是几个大的milestone。自然这些版本会有一些比较大的变化,比如asyncio,print这些熟知的,当然可能也会有一些不是所有人都清楚的,或者说这些内置库本身的升级就是版本升级的重要部分,所以绑定一些特殊的版本自然是有益处的。你可以通过虚拟环境解决你的需求

你可以直接试试 virtualenv,我个人是不用anaconda的

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏