问题:如何更改默认的Anaconda python环境

我已经安装了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

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

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)

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.


回答 0

如果您只想更改为其他环境,请使用

source activate environment-name

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


通常,最好只是创建新环境。但是,如果您确实想在默认环境中更改Python版本,则可以执行以下操作:

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

conda update conda

然后跑

conda install python=3.5

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

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

If you just want to change to another environment, use

source activate environment-name

(you can create environment-name with `conda create)


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

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

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.

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


回答 1

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

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

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

示例:
这是我在Mac OSX上的操作方式

  1. 打开终端并输入:

    nano ~/.bash_profile

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

    source activate p3.5

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

  4. 输入以下内容以查看活跃的环境

    conda info -e

结果表明,默认情况下我正在使用我的p3.5环境。

对于Windows:在打开命令提示符时,
使用创建一个命令文件(.cmd),activate environment_name并按照以下说明执行该文件。

  1. 创建一个批处理文件命令,例如“ my_conda.cmd”,将其放入“应用程序数据”文件夹中。
  2. 将其配置为在每次打开时自动启动cmd。此设置位于注册表中:注册表
    项:HKCU \ SOFTWARE \ Microsoft \ Command处理器
    值:自动运行
    类型:REG_EXPAND_SZ
    数据:“%AppData%\ my_conda.cmd”

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

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.

The following assumes environment_name is the name of your environment

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

Example:
Here’s how i did it on 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

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

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

  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”

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


回答 2

在Linux下,有一种更简单的方法可以通过修改~/.bashrc或来设置默认环境。~/.bash_profile 最后,您会发现类似

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

替换为

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

这就是全部。

Under Linux there is an easier way to set the default environment by modifying ~/.bashrc or ~/.bash_profile At the end you’ll find something like

# 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.


回答 3

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

在此处输入图片说明

“ 属性”窗口的“ 目标”表单应该已经用文本填充,例如%windir%\system32\cmd.exe "/K" C:\Users\xxx\AppData\Local\Continuum\Miniconda3\Scripts\activate.bat C:\Users\xxx\AppData\Local\Continuum\Miniconda3\ 该命令的三个部分:1)启动… \ cmd.exe 2)运行… \使用环境3)\ acitvate.bat … \ Miniconda3 \

将第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

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

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

希望这可以帮助

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

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.

enter image description here

The Target form of 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\

Change 3rd part to path of the environment (say myenv) you want as default i.e. fill the Target form something 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

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.

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 in form 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


回答 4

永久更改

conda install python={version}

临时更改

查看您的环境

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

如果未显示您要安装的环境

运行conda create -n py36 python=3.6 anacondapython 3.6更改版本作为您的首选

激活环境(使用Anaconda提示符)

运行activate envnmeenvnme,您可以conda info --envs在运行时通过此命令作为示例conda info --envs显示

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

然后跑 activate py36

检查运行 python --version

在Windows中,优良作法是在激活另一个环境之前先停用它。 https://docs.conda.io/projects/conda/zh/latest/user-guide/tasks/manage-environments.html?highlight=deactivate%20environment

Change permanent

conda install python={version}

Change Temporarily

View your environments

run conda info --envs on your terminal window or an Anconda Prompt

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

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

Activating an environment (use Anaconda prompt)

run activate envnme envnme you can find by this commandconda info --envs as a example when you run conda info --envs it show

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

then run activate py36

to check run 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


回答 5

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

Anaconda使用默认环境命名,base并且您不能使用相同的名称创建新的环境(例如python 3.6)。这是故意的。如果您希望基本的Anaconda为python 3.6,则正确的方法是为python 3.6安装Anaconda。作为软件包管理器,Anaconda的目标是封装不同的环境,因此为什么您必须在其中激活激活资源以及为什么不能随便安静地切换基本软件包,因为这可能会导致生产系统出现许多问题。

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

Anaconda uses a default environment named base and 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 right way 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.


回答 6

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

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

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

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

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

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”:

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.


回答 7

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

conda search python

然后更新到所需的版本:

conda install python=3.*.*

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

其他有用的命令:

conda info
python --version

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:

conda search python

Then update to the version you want:

conda install python=3.*.*

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

Other helpful commands:

conda info
python --version

回答 8

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

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

预习

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

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:

C:\Users\BenBouali\Anaconda3\ WILL CHANGE INTO 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.


回答 9

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

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

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

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

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

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.

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


回答 10

加载你的 “基地”环境-如OP的py34-当你加载你的终端/壳。

如果您使用Bash,请输入以下行:

conda activate py34

在您.bash_profile(或.bashrc)中:

$ echo 'conda activate py34' >> ~/.bash_profile

每次运行新的终端时,py34都会加载conda环境。

Load your “base” environment — as OP’s py34 — when you load your terminal/shell.

If you use Bash, put the line:

conda activate py34

in your .bash_profile (or .bashrc):

$ echo 'conda activate py34' >> ~/.bash_profile

Every time you run a new terminal, conda environment py34 will be loaded.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。