如何更改默认的 Anaconda python 环境

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28436769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:17:19  来源:igfitidea点击:

How to change default Anaconda python environment

pythonanacondaconda

提问by user2734434

I've installed Anaconda and created two extra environments: py3k (which holds Python 3.3) and py34 (which holds Python 3.4). Besides those, I have a default environment named 'root' which the Anaconda installer created by default and which holds Python 2.7. This last one is the default, whenever I launch 'ipython' from the terminal it gives me version 2.7. In order to work with Python 3.4, I need to issue the commands (in the shell)

我已经安装了 Anaconda 并创建了两个额外的环境:py3k(包含 Python 3.3)和 py34(包含 Python 3.4)。除此之外,我有一个名为“root”的默认环境,Anaconda 安装程序默认创建该环境并保存 Python 2.7。最后一个是默认设置,每当我从终端启动“ipython”时,它都会给我 2.7 版。为了使用 Python 3.4,我需要发出命令(在 shell 中)

source activate py34
ipython

which change the default environment to Python 3.4. This works fine, but it's annoying since most of the time I work on Python 3.4, instead of Python 2.7 (which I hold for teaching purposes, it's a rather long story). Anyway, I'll like to know how to change the default environment to Python 3.4, bearing in mind that I don't want to reinstall everything from scratch.

将默认环境更改为 Python 3.4。这很好用,但很烦人,因为我大部分时间都在使用 Python 3.4 而不是 Python 2.7(我认为它是出于教学目的,这是一个相当长的故事)。无论如何,我想知道如何将默认环境更改为 Python 3.4,请记住,我不想从头开始重新安装所有内容。

采纳答案by asmeurer

If you just want to change to another environment, use

如果你只是想改变到另一个环境,使用

source activate environment-name

(you can create environment-namewith `conda create)

(您可以environment-name使用`conda create 创建)



Typically it's best to just create new environments. However, if you really want to change the Python version in the defaultenvironment, you can do so as follows:

通常最好只创建新环境。但是,如果您真的想更改默认环境中的 Python 版本,您可以这样做:

First, make sure you have the latest version of conda by running

首先,通过运行确保您拥有最新版本的 conda

conda update conda

Then run

然后运行

conda install python=3.5

This will attempt to update all your packages in your root environment to Python 3 versions. If it is not possible (e.g., because some package is not built for Python 3.5), it will give you an error message indicating which package(s) caused the issue.

这将尝试将根环境中的所有包更新到 Python 3 版本。如果不可能(例如,因为某些包不是为 Python 3.5 构建的),它会给您一条错误消息,指出是哪个包导致了问题。

If you installed packages with pip, you'll have to reinstall them.

如果您使用 pip 安装了软件包,则必须重新安装它们。

回答by Jev

Under Linux there is an easier way to set the default environment by modifying ~/.bashrcor ~/.bash_profileAt the end you'll find something like

在 Linux 下,有一种更简单的方法可以通过修改~/.bashrc~/.bash_profile在最后你会找到类似的东西来设置默认环境

# added by Anaconda 2.1.0 installer
export PATH="~/anaconda/bin:$PATH"

Replace it with

将其替换为

# set python3 as default
export PATH="~/anaconda/envs/python3/bin:$PATH"

and thats all there is to it.

这就是全部。

回答by FistOfFury

Overview
Some people have multiple Anaconda environments with different versions of python for compatibility reasons. In this case, you should have a script that sets your default environment. With this method, you can preserve the versions of python you use in your environments.

概述
出于兼容性原因,有些人有多个 Anaconda 环境和不同版本的 python。在这种情况下,您应该有一个设置默认环境的脚本。使用此方法,您可以保留在您的环境中使用的 Python 版本。

The following assumes environment_nameis the name of your environment

以下假设environment_name是您的环境名称

Mac / Linux:
Edit your bash profile so that the last line is source activate environment_name. In Mac OSX this is ~/.bash_profile, in other environments this may be ~/.bashrc

Mac / Linux:
编辑您的 bash 配置文件,使最后一行是source activate environment_name. 在 Mac OSX 中,这是 ~/.bash_profile,在其他环境中,这可能是 ~/.bashrc

Example:
Here's how i did it on Mac OSX

示例:
这是我在 Mac OSX 上的做法

  1. Open Terminal and type:

    nano ~/.bash_profile

  2. Go to end of file and type the following, where "p3.5" is my environment:

    source activate p3.5

  3. Exit File. Start a new terminal window.

  4. Type the following to see what environment is active

    conda info -e

  1. 打开终端并输入:

    nano ~/.bash_profile

  2. 转到文件末尾并键入以下内容,其中“p3.5”是我的环境:

    source activate p3.5

  3. 退出文件。启动一个新的终端窗口。

  4. 键入以下内容以查看处于活动状态的环境

    conda info -e

The result shows that I'm using my p3.5 environment by default.

结果显示我默认使用的是我的 p3.5 环境。

For Windows:
Create a command file (.cmd) with activate environment_nameand follow these instructions to have it execute whenever you open a command prompt

对于 Windows:
创建一个命令文件 (.cmd)activate environment_name并按照这些说明在打开命令提示符时执行它

  1. Create a batch file command, e.g. "my_conda.cmd", put it in the Application Data folder.
  2. Configure it to be started automatically whenever you open cmd. This setting is in Registry:
    key: HKCU\SOFTWARE\Microsoft\Command Processor
    value: AutoRun
    type: REG_EXPAND_SZ
    data: "%AppData%\my_conda.cmd"
  1. 创建一个批处理文件命令,例如“my_conda.cmd”,将其放在 Application Data 文件夹中。
  2. 将其配置为每次打开时自动启动cmd。此设置在注册表中:
    键:HKCU\SOFTWARE\Microsoft\Command Processor
    值:AutoRun
    类型:REG_EXPAND_SZ
    数据:“%AppData%\my_conda.cmd”

from this answer: https://superuser.com/a/302553/143794

从这个答案:https: //superuser.com/a/302553/143794

回答by dominik andreas

I wasn't satisfied with any of the answers presented here, since activating an environment takes a few seconds on my platform (for whatever reason)

我对这里提供的任何答案都不满意,因为在我的平台上激活环境需要几秒钟(无论出于何种原因)

I modified my path variable so that the environment I want as default has priority over the actual default.

我修改了我的路径变量,以便我想要的默认环境优先于实际默认值。

In my case I used the following commands to accomplish that for the environment "py35":

就我而言,我使用以下命令为环境“py35”完成此操作:

setx PATH "%userprofile%\Anaconda3\envs\py35\;%PATH%"
setx PATH "%userprofile%\Anaconda3\envs\py35\Scripts;%PATH%"

to find out where your environment is stored, activate it and enter where python. I'm not sure yet if this approach has any downsides. Since it also changes the default path of the conda executable. If that should be the case, please comment.

要找出您的环境的存储位置,请激活它并输入where python. 我还不确定这种方法是否有任何缺点。因为它还更改了 conda 可执行文件的默认路径。如果是这种情况,请发表评论。

回答by Kushdesh

For windows Anaconda comes with Anaconda Prompt which is a shortcut to cmd and can be used run conda commands without adding anaconda in PATH variable. Find the location of it, copy and rename the copy (say myenv_prompt). Right click myenv_prompt and select properties in the context menu.

对于 Windows,Anaconda 带有 Anaconda Prompt,它是 cmd 的快捷方式,可用于运行 conda 命令,而无需在 PATH 变量中添加 anaconda。找到它的位置,复制并重命名副本(比如 myenv_prompt)。右键单击 myenv_prompt 并在上下文菜单中选择属性。

enter image description here

在此处输入图片说明

The Target formof Properties window should already be filled with text, something like %windir%\system32\cmd.exe "/K" C:\Users\xxx\AppData\Local\Continuum\Miniconda3\Scripts\activate.bat C:\Users\xxx\AppData\Local\Continuum\Miniconda3\There are three parts of this command 1)start ...\cmd.exe 2)run ...\acitvate.bat with environment 3)...\Miniconda3\

属性窗口的目标窗体应该已经填充了文本,类似于%windir%\system32\cmd.exe "/K" C:\Users\xxx\AppData\Local\Continuum\Miniconda3\Scripts\activate.bat C:\Users\xxx\AppData\Local\Continuum\Miniconda3\此命令的三个部分 1)start ...\cmd.exe 2)run ...\acitvate.bat with environment 3)...\ Miniconda3\

Change 3rd part to path of the environment (say myenv) you want as default i.e. fill the Target formsomething like %windir%\system32\cmd.exe "/K" C:\Users\xxx\AppData\Local\Continuum\Miniconda3\Scripts\activate.bat C:\Users\xxx\AppData\Local\Continuum\Miniconda3\envs\myenv

将第 3 部分更改为您想要的环境路径(例如 myenv)作为默认值,即填写目标表单,例如%windir%\system32\cmd.exe "/K" C:\Users\xxx\AppData\Local\Continuum\Miniconda3\Scripts\activate.bat C:\Users\xxx\AppData\Local\Continuum\Miniconda3\envs\myenv

Now myenv_prompt will act as shortcut to start cmd with myenv as the default environment for python. This shortcut you can keep in start menu or pinned in taskbar.

现在 myenv_prompt 将作为启动 cmd 的快捷方式,将 myenv 作为 python 的默认环境。您可以将此快捷方式保留在开始菜单中或固定在任务栏中。

One advantage of this method is that you can create a few shortcuts each having different environment as default environment. Also you can set the default folder by filling Start inform of the Properties window

这种方法的一个优点是您可以创建几个快捷方式,每个快捷方式都具有不同的环境作为默认环境。您也可以通过“属性”窗口中填写“开始”来设置默认文件夹

Hope this helps

希望这可以帮助

PS:It is not required to find Anaconda Prompt and can be done by changing target of any shortcut. But you will require to know path of cmd.exe and activate.bat

PS:不需要找到Anaconda Prompt,可以通过更改任何快捷方式的目标来完成。但是你需要知道 cmd.exe 和 activate.bat 的路径

回答by Dulanga Heshan

Change permanent

更改永久

conda install python={version}

Change Temporarily

暂时改变

View your environments

查看您的环境

run conda info --envson your terminal window or an Anconda Prompt

运行conda info --envs终端窗口或Anconda上提示

If It doesn't show environment that you want to install

如果它没有显示您要安装的环境

run conda create -n py36 python=3.6 anacondafor python 3.6 change version as your prefer

conda create -n py36 python=3.6 anaconda根据您的喜好运行python 3.6 更改版本

Activating an environment (use Anaconda prompt)

激活环境(使用 Anaconda prompt)

run activate envnmeenvnme you can find by this commandconda info --envsas a example when you run conda info --envsit show

运行activate envnmeenvnme 你可以通过这个命令找到conda info --envs作为例子,当你运行conda info --envs它时显示

base * C:\Users\DulangaHeshan\Anaconda3 py36 C:\Users\DulangaHeshan\Anaconda3\envs\py36

base * C:\Users\DulangaHeshan\Anaconda3 py36 C:\Users\DulangaHeshan\Anaconda3\envs\py36

then run activate py36

然后运行 activate py36

to check run python --version

检查运行 python --version

In Windows, it is good practice to deactivate one environment before activating another. https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html?highlight=deactivate%20environment

在 Windows 中,最好先停用一个环境,然后再激活另一个环境。 https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html?highlight=deactivate%20environment

回答by cgnorthcutt

The correct answer (as of Dec 2018) is... you can't. Upgrading conda install python=3.6may work, but it might not if you have packages that are necessary, but cannot be uninstalled.

正确答案(截至 2018 年 12 月)是......你不能。升级conda install python=3.6可能有效,但如果您有必需但无法卸载的软件包,则可能无效。

Anaconda uses a default environment named baseand you cannot create a new (e.g. python 3.6) environment with the same name. This is intentional. If you want your base Anaconda to be python 3.6, the rightway to do this is to install Anaconda for python 3.6. As a package manager, the goal of Anaconda is to make different environments encapsulated, hence why you must source activate into them and why you can't just quietly switch the base package at will as this could lead to many issues on production systems.

Anaconda 使用名为的默认环境base,您不能创建具有相同名称的新(例如 python 3.6)环境。这是故意的。如果你希望你的基础 Anaconda 是 python 3.6,正确的方法是为 python 3.6 安装 Anaconda。作为包管理器,Anaconda 的目标是将不同的环境封装起来,因此为什么必须将其 source activate 以及为什么不能随意切换基础包,因为这可能会导致生产系统上的许多问题。

回答by Kream

Create a shortcut of anaconda prompt onto desktop or taskbar, and then in the properties of that shortcut make sure u modify the last path in "Target:" to the path of ur environment:

在桌面或任务栏上创建anaconda prompt的快捷方式,然后在该快捷方式的属性中确保将“目标:”中的最后一个路径修改为您环境的路径:

C:\Users\BenBouali\Anaconda3\ WILL CHANGE INTO C:\Users\BenBouali\Anaconda3\envs\tensorflow-gpu

C:\Users\BenBouali\Anaconda3\ 将变为 C:\Users\BenBouali\Anaconda3\envs\tensorflow-gpu

preview

预览

and this way u can use that shortcut to open a certain environment when clicking it, you can add it to ur path too and now you'll be able to run it from windows run box by just typing in the name of the shortcut.

这样您就可以在单击时使用该快捷方式打开某个环境,您也可以将其添加到您的路径中,现在您只需输入快捷方式的名称即可从 Windows 运行框中运行它。

回答by Brad123

I got this when installing a library using anaconda. My version went from Python 3.* to 2.7 and a lot of my stuff stopped working. The best solution I found was to first see the most recent version available:

我在使用 anaconda 安装库时得到了这个。我的版本从 Python 3.* 升级到 2.7,我的很多东西都停止工作了。我发现的最佳解决方案是首先查看可用的最新版本:

conda search python

Then update to the version you want:

然后更新到你想要的版本:

conda install python=3.*.*

Source: http://chris35wills.github.io/conda_python_version/

来源:http: //chris35wills.github.io/conda_python_version/

Other helpful commands:

其他有用的命令:

conda info
python --version

回答by Fijoy Vadakkumpadan

On Windows, create a batch file with the following line in it:

在 Windows 上,创建一个包含以下行的批处理文件:

start cmd /k "C:\Anaconda3\Scripts\activate.bat C:\Anaconda3 & activate env"

The first path contained in quotes is the path to the activate.bat file in the Anaconda installation. The path on your system might be different. The name following the activate command of course should be your desired environment name.

引号中包含的第一个路径是 Anaconda 安装中 activate.bat 文件的路径。您系统上的路径可能不同。activate 命令后面的名称当然应该是您想要的环境名称。

Then run the batch file when you need to open an Anaconda prompt.

然后在需要打开 Anaconda 提示时运行批处理文件。