Python 如何同时使用 Anaconda 版本(2.7 和 3.5)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41862682/
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-20 01:46:26  来源:igfitidea点击:

How can I use both Anaconda versions (2.7 & 3.5)?

pythonpython-2.7python-3.xanaconda

提问by Lorah Attkins

I was using the Anaconda 3.5distroin a Windows 10 machine. Due to dependencies in libraries that I want to work with, I had to have the 2.7version installed as well.

我在 Windows 10 机器上使用 Anaconda3.5发行版。由于我想要使用的库中的依赖性,我还必须2.7安装该版本。

The good news is that the libraries I needed can now work with the 2.7version smoothly and Visual Studio 2015 automagically detected my new Python environment.

好消息是,我需要的库现在可以2.7顺利使用该版本,并且 Visual Studio 2015 会自动检测到我的新 Python 环境。

The problem comes when using the command line. Upon issuing the command

使用命令行时出现问题。发出命令后

conda info --envs

I get

我得到

root                  *  C:\Users\norah\Anaconda2

i.e. a single environment (to my understanding and search so far, according to thisI should see two envs listed). This means I can't use condato acquire new packages for my Python3.5installation, at least not at the command line as I used to since condaonly refers to Python2.7. The same goes for the GUI version, Anaconda navigator (I'm not very fond of the GUI version but I tried it out).

即单个环境(据我了解和搜索到目前为止,根据我应该看到列出了两个环境)。这意味着我不能用来conda为我的Python3.5安装获取新包,至少不能像以前那样在命令行中使用,因为conda只引用Python2.7. GUI 版本 Anaconda navigator 也是如此(我不是很喜欢 GUI 版本,但我试过了)。

There's also no way of launching python3 from the command line since

也没有办法从命令行启动 python3,因为

$python

always fires up python2.7 and issuing $python3or $python3.5in the command line doesn't seem to work (nor would adding the path of python3 to the system since the actual executable has the same name as python2 i.e. python.exe)

总是启动 python2.7 并且在命令行中发出$python3$python3.5似乎不起作用(也不会将 python3 的路径添加到系统中,因为实际的可执行文件与 python2 ie 同名python.exe

Is my system taken over by Python2.7? Is anyone here using them both smoothly and if so could you please elaborate on how to do that? Was it a "no no" move to install both versions of Anaconda?

我的系统被 Python2.7 接管了吗?有没有人在这里顺利使用它们,如果可以,请详细说明如何做到这一点?安装两个版本的 Anaconda 是“不,不”之举吗?

回答by Mike Müller

You can make Python 3.5 environment with your Anaconda 2.7:

您可以使用 Anaconda 2.7 创建 Python 3.5 环境:

conda create -n py35 python=3.5

Now, activate it:

现在,激活它:

activate py35

Finally you can install the desired packages:

最后,您可以安装所需的软件包:

conda install numpy

or, the whole anaconda:

或者,整个蟒蛇:

conda install anaconda

The advantage of this approach is that you can also create Python 3.4 or 3.6 environments. Furthermore, you can create environments with different combinations and versions of libraries.

这种方法的优点是您还可以创建 Python 3.4 或 3.6 环境。此外,您可以创建具有不同库组合和版本的环境。

Actually, it makes sense to create a new environment for each larger project.

实际上,为每个较大的项目创建一个新环境是有意义的。

回答by u55

I also despise the virtual environment switch that Anaconda tries to force on us. I prefer to have both executables always instantly available from the command line. I'm pretty sure I had this working on a Windows machine once:

我也鄙视 Anaconda 试图强加给我们的虚拟环境切换。我更喜欢让两个可执行文件始终从命令行立即可用。我很确定我曾经在 Windows 机器上使用过这个:

  1. Install Anaconda2 and Anaconda3 to the C:\ drive as "C:\Anaconda2\" and "C:\Anaconda3\" respectively.
  2. Edit your "Path" environment variable (Control Panel -> System and Security -> System -> Advanced system settings -> Environment Variables) and make sure that "C:\Anaconda2;C:\Anaconda2\Scripts;C:\Anaconda2\Library\bin" is in front of "C:\Anaconda3;C:\Anaconda3\Scripts;C:\Anaconda3\Library\bin".
  3. Copy and rename the file "C:\Anaconda3\python.exe" to "C:\Anaconda3\python3.exe".
  4. Copy and rename the file "C:\Anaconda3\Scripts\conda.exe" to "C:\Anaconda3\Scripts\conda3.exe"
  5. Copy and rename any other scripts you might use in "C:\Anaconda3\Scripts\", such as "pip.exe" to "pip3.exe", etc.
  1. 将 Anaconda2 和 Anaconda3 分别作为“C:\Anaconda2\”和“C:\Anaconda3\”安装到 C:\ 驱动器。
  2. 编辑“路径”环境变量(控制面板 -> 系统和安全 -> 系统 -> 高级系统设置 -> 环境变量)并确保“C:\Anaconda2;C:\Anaconda2\Scripts;C:\Anaconda2\ Library\bin”在“C:\Anaconda3;C:\Anaconda3\Scripts;C:\Anaconda3\Library\bin”的前面。
  3. 将文件“C:\Anaconda3\python.exe”复制并重命名为“C:\Anaconda3\python3.exe”。
  4. 将文件“C:\Anaconda3\Scripts\conda.exe”复制并重命名为“C:\Anaconda3\Scripts\conda3.exe”
  5. 复制并重命名您可能在“C:\Anaconda3\Scripts\”中使用的任何其他脚本,例如“pip.exe”到“pip3.exe”等。

Now, when you type "python" or "conda" at the command line you will get the python2 version, and when you type "python3" or "conda3", etc. at the command line you will get the python3 version.

现在,当您在命令行中键入“python”或“conda”时,您将获得python2 版本,而当您在命令行中键入“python3”或“conda3”等时,您将获得python3 版本。

回答by prashanth manohar

The best way to use both Python 2.7x and Python 3.5x together is Jupyter Notebook.

同时使用 Python 2.7x 和 Python 3.5x 的最佳方法是 Jupyter Notebook。

http://jupyter.org/

http://jupyter.org/

You will be able to work on your browser with IPython Notebook style interface which is great for working with scripting languages.

您将能够使用 IPython Notebook 风格的界面在浏览器上工作,这非常适合使用脚本语言。

I found some of these videos on YouTube very informative:

我在 YouTube 上发现其中一些视频非常有用:

1) https://www.youtube.com/watch?v=HW29067qVWk

1) https://www.youtube.com/watch?v=HW29067qVWk

2) https://www.youtube.com/watch?v=e9cSF3eVQv0

2) https://www.youtube.com/watch?v=e9cSF3eVQv0

Besides Python 2.7 and 3.5, there are a bunch of other languages that you will be able run with your Jupyter Notebook. The various Kernels are available in this link below:

除了 Python 2.7 和 3.5,还有许多其他语言可以在 Jupyter Notebook 上运行。以下链接中提供了各种内核:

https://github.com/jupyter/jupyter/wiki/Jupyter-kernels

https://github.com/jupyter/jupyter/wiki/Jupyter-kernels

After installation, as you start your Jupyter Notebook, your browser will open up a new page showing your Home Directory. Among the 4 tabs (Files|Running|Clusters|Conda):

安装后,当您启动 Jupyter Notebook 时,您的浏览器将打开一个新页面,显示您的主目录。在 4 个选项卡(文件|运行|集群|Conda)中:

1) The Files tab shows all the files in your Home Directory.

1) 文件选项卡显示主目录中的所有文件。

2) Running tab shows all the Notebooks that are running.

2) 运行选项卡显示所有正在运行的笔记本。

3) Clusters tab is provided by IPython parallel.

3) Clusters 选项卡由 IPython parallel 提供。

4) Conda tab is where you need to add the Python version 3.5 (and other languages if needed) to your Jupyter Notebook (If Python 2.7 is default).

4) Conda 选项卡是您需要将 Python 3.5 版(以及其他语言,如果需要)添加到您的 Jupyter Notebook(如果 Python 2.7 是默认值)的位置。

If you are interested to try C++ with your Jupyter Notebook, there are a couple of Kernels available.

如果你有兴趣在你的 Jupyter Notebook 上尝试 C++,有几个内核可用。

1) JupyRoot - https://github.com/root-mirror/root/tree/master/bindings/pyroot/JupyROOT

1) JupyRoot - https://github.com/root-mirror/root/tree/master/bindings/pyroot/JupyROOT

2) Cling - https://github.com/root-mirror/cling

2)坚持 - https://github.com/root-mirror/cling