conda设置python虚拟环境(不同版本python共存)

Anaconda与conda区别 
  conda可以理解为一个工具,也是一个可执行命令,其核心功能是包管理与环境管理。包管理与pip的使用类似,环境管理则允许用户方便地安装不同版本的python并可以快速切换。 conda的设计理念——conda将几乎所有的工具、第三方包都当做package对待,甚至包括python和conda自身 Anaconda则是一个打包的集合,里面预装好了conda、某个版本的python、众多packages、科学计算工具等等

1 查找虚拟环境

 conda info --envs 或者或 conda info -e

2 选择虚拟环境

source activate py35_pytorch

3 退出虚拟环境

source deactivate py35_pytorch

4 安装librosa

conda install -c conda-forge librosa

1.首先,我们可以检查以查看哪些版本的Python可供我们使用:

conda search "^python$"

您将收到包含您可以定位的不同版本的Python的输出,包括Python3和Python2版本。由于我们在本教程中使用Anaconda与Python 3,您将只能访问Python 3版本的软件包。

让我们使用最新版本的Python 3创建一个环境。我们可以通过将版本3分配给python参数来实现。 我们将调用环境py2-env ,但是您可能希望为您的环境使用更具描述性的名称,特别是如果您使用环境来访问多个版本的Python。

conda create --name py35_pytorch python=3.5

我们将收到输出,包含有关下载内容和要安装的软件包的信息,然后提示您继续执行y或n 。 只要你同意,键入y 。

conda实用程序现在将获取环境的包,并让它知道什么时候完成。

2.windows可以通过键入以下内容激活新环境:
默认路径为C:\Users\asus\Anaconda3\envs\py2-env

 To activate this environment, use:
 > activate py35_pytorch

 To deactivate this environment, use:
 > deactivate py35_pytorch

ubuntu通过以下激活或停用

source activate my_env
source deactivate

3.此外,你可以通过以下命令,将虚拟环境安装到指定路径

conda create --prefix=D:\python36\py36 python=3.6

路径D:\python36是先建好的文件夹,py36是需要安装的虚拟环境名称。请注意,安装完成后,虚拟环境的全称包含整个路径,为D:\python36\py36。激活指定路径下的虚拟环境的命令如下:

activate D:\python36\py36

想要删除指定路径下的虚拟环境,使用如下的命令:

conda remove --prefix=D:\python36\py36 --all

这里写图片描述

4.通过以下命令查看已建好的环境

conda info --envs

5.对虚拟环境中安装额外的包。
    使用命令conda install -n your_env_name [package]即可安装package到your_env_name中,例如numpy :

conda install --name py2-env python=3.5 numpy

6.如果你知道你想创建一个numpy环境,你可以在conda conda create命令:

conda create --name my_env python=3.5 numpy

7.如果您不再处理特​​定项目,并且不再需要相关环境,则可以将其删除。为此,请键入以下内容:

conda remove --name py2-env --all

现在,当您键入conda info --envs命令时,您删除的环境将不再列出。
8.删除环境中的某个包。
   使用命令conda remove --name $your_env_name  $package_name 即可。
9、设置国内镜像
如果需要安装很多packages,你会发现conda下载的速度经常很慢,因为Anaconda.org的服务器在国外。所幸的是,清华TUNA镜像源有Anaconda仓库的镜像,我们将其加入conda的配置即可:
# 添加Anaconda的TUNA镜像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
# TUNA的help中镜像地址加有引号,需要去掉
# 设置搜索时显示通道地址
conda config --set show_channel_urls yes 

$ export ENV_NAME="py35_pytorch"  # or any other name you prefer
$ conda create --name $ENV_NAME --file all_pkg_versions.txt python=3.5 anaconda
$ source activate $ENV_NAME

 

补充:help

~$ conda create --help
usage: conda create [-h] [-y] [--dry-run] [-f] [--file FILE] [--no-deps]
                    [--only-deps] [-m] [-C] [--use-local] [--offline]
                    [--no-pin] [-c CHANNEL] [--override-channels]
                    [-n ENVIRONMENT | -p PATH] [-q] [--copy] [-k]
                    [--update-dependencies] [--no-update-dependencies]
                    [--channel-priority] [--no-channel-priority] [--clobber]
                    [--show-channel-urls] [--no-show-channel-urls]
                    [--download-only] [--json] [--debug] [--verbose]
                    [--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 or update in 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).
  --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, update, remove, or change
                        dependencies. This WILL lead to broken environments
                        and inconsistent behavior. Use at your own risk
  --only-deps           Only install dependencies.
  -m, --mkdir           Create the environment directory if necessary.
  -C, --use-index-cache
                        Use cache of channel index files, even if it has expired.
  --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.
  -p PATH, --prefix PATH
                        Full path to environment prefix.
  -q, --quiet           Do not display progress bar.
  --copy                Install all packages using copies instead of hard- or
                        soft-linking.
  -k, --insecure        Allow conda to perform "insecure" SSL connections and
                        transfers. Equivalent to setting 'ssl_verify' to 'false'.
  --update-dependencies, --update-deps
                        Update dependencies. Overrides the value given by
                        `conda config --show update_deps`.
  --no-update-dependencies, --no-update-deps
                        Don't update dependencies. Overrides the value given
                        by `conda config --show update_deps`.
  --channel-priority, --channel-pri, --chan-pri
                        Channel priority takes precedence over package
                        version. Overrides the value given by `conda config
                        --show channel_priority`.
  --no-channel-priority, --no-channel-pri, --no-chan-pri
                        Package version takes precedence over channel
                        priority. Overrides the value given by `conda config
                        --show channel_priority`.
  --clobber             Allow clobbering of overlapping file paths within
                        packages, and suppress related warnings.
  --show-channel-urls   Show channel urls. Overrides the value given by `conda
                        config --show show_channel_urls`.
  --no-show-channel-urls
                        Don't show channel urls. Overrides the value given by
                        `conda config --show show_channel_urls`.
  --download-only       Solve an environment and ensure package caches are
                        populated, but exit prior to unlinking and linking
                        packages into the prefix.
  --json                Report all output as json. Suitable for using conda
                        programmatically.
  --debug               Show debug output.
  --verbose, -v         Use once for info, twice for debug, three times for trace.
  --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

 

 

转载于:https://blog.csdn.net/co_zy/article/details/77412610

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值