使用多个 Python 引擎(32Bit/64bit 和 2.7/3.5)

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

Using multiple Python engines (32Bit/64bit and 2.7/3.5)

pythonpython-3.xpython-2.7anacondaconda

提问by rmf

I would like to use Python for scientific applications and after some research decided that I will use Anaconda as it comes bundled with loads of packages and add new modules using conda installthrough the cmd is easy.

我想将 Python 用于科学应用程序,经过一些研究决定我将使用 Anaconda,因为它捆绑了大量软件包,并且conda install通过 cmd添加新模块很容易。

I prefer to use the 64 bit version for better RAM use and efficiency but 32bit version is needed as well because some libraries are 32bit. Similarly, I prefer to use Python 3.5 as that is the future and the way things go. But loads of libraries are still 2.7 which means I need both.

我更喜欢使用 64 位版本以获得更好的 RAM 使用和效率,但也需要 32 位版本,因为有些库是 32 位的。同样,我更喜欢使用 Python 3.5,因为那是未来和事情的发展方向。但是大量的库仍然是 2.7,这意味着我需要两者。

I have to install 4 versions of Anaconda (64bit 2.7, 64bit 3.5, 32bit 2.7, 64bit 3.5). Each version is about 380MB. I am aiming to use Jupyter notebook and Spyder as the IDE. I had to switch between versions when required. I had conflicting libraries, path issues and all sorts of weird problems.

我必须安装 4 个版本的 Anaconda(64 位 2.7、64 位 3.5、32 位 2.7、64 位 3.5)。每个版本大约 380MB。我的目标是使用 Jupyter notebook 和 Spyder 作为 IDE。我不得不在需要时在版本之间切换。我有冲突的库、路径问题和各种奇怪的问题。

So, I am planning to do a clean install from scratch. I would like to know if there is a more sensible way to handle this. I use Windows 7 64 bit for now if that matters.

因此,我计划从头开始进行全新安装。我想知道是否有更明智的方法来处理这个问题。如果重要的话,我现在使用 Windows 7 64 位。

采纳答案by Mike Müller

Make sure to set the right environmental variables (https://github.com/conda/conda/issues/1744)

确保设置正确的环境变量(https://github.com/conda/conda/issues/1744

Create a new environment for 32bit Python 2.7:

为 32 位 Python 2.7 创建一个新环境:

set CONDA_FORCE_32BIT=1
conda create -n py27_32 python=2.7

Activate it:

激活它:

set CONDA_FORCE_32BIT=1
activate py27_32

Deactivate it:

停用它:

deactivate py27_32

Create one for 64 bit Python 3.5:

为 64 位 Python 3.5 创建一个:

set CONDA_FORCE_32BIT=
conda create -n py35_64 python=3.5

Activate it:

激活它:

set CONDA_FORCE_32BIT=
activate py35_64

The best would be to write the activation commands in a batch file so that you have to type only one command and cannot forget to set the right 32/64 bit flag.

最好的办法是在批处理文件中编写激活命令,这样您只需键入一个命令,并且不会忘记设置正确的 32/64 位标志。

UPDATE

更新

You don't need to install a full Anaconda distribution for this. Minicondais enough:

您不需要为此安装完整的 Anaconda 发行版。Miniconda就足够了:

These Miniconda installers contain the conda package manager and Python. Once Miniconda is installed, you can use the conda command to install any other packages and create environments, etc. ...

There are two variants of the installer: Miniconda is Python 2 based and Miniconda3 is Python 3 based. Note that the choice of which Miniconda is installed only affects the root environment. Regardless of which version of Miniconda you install, you can still install both Python 2.x and Python 3.x environments.

这些 Miniconda 安装程序包含 conda 包管理器和 Python。安装 Miniconda 后,您可以使用 conda 命令安装任何其他软件包并创建环境等。...

安装程序有两种变体:Miniconda 基于 Python 2,Miniconda3 基于 Python 3。注意选择安装哪个 Miniconda 只影响 root 环境。无论您安装哪个版本的 Miniconda,您仍然可以同时安装 Python 2.x 和 Python 3.x 环境。

I would recommend you to use Miniconda3 64-bit as your root environment.

我建议您使用 Miniconda3 64 位作为根环境。

You can always install a full Anaconda later with:

您以后可以随时安装完整的 Anaconda:

conda install anaconda

Notethat it might downgrade some of your previously install packages in your active environment.

请注意,它可能会降级您之前在活动环境中安装的一些软件包。

回答by VectorVictor

I just wanted to add to Mike Mullers answer, as I also wanted my IPython to switch between 32 bit and 64 bit.

我只是想补充一下 Mike Mullers 的回答,因为我还希望我的 IPython 在 32 位和 64 位之间切换。

After setting up the 32bit or 64bit environment. Use the following commands

设置好32bit或64bit环境后。使用以下命令

pip install ipykernel

to install ipykernel on this env. Then assign it with:

在这个环境上安装 ipykernel。然后分配给它:

python -m ipykernel install --user --name myenv --display-name "Python (myenv)"

here myenv is the name of your new environment. See this page here for further details on switching kernels - http://ipython.readthedocs.io/en/stable/install/kernel_install.html

这里 myenv 是新环境的名称。有关切换内核的更多详细信息,请参阅此页面 - http://ipython.readthedocs.io/en/stable/install/kernel_install.html

回答by merv

Setting the Subdirectory Constraint

设置子目录约束

Conda has a configuration variable subdirthat can be used to constrain package searching to platforms (e.g., win-32). I think the simplest procedure is to create the empty env, set it's subdir, then proceed with the (constrained) installations. For example,

Conda 有一个配置变量subdir,可用于将包搜索限制为平台(例如,win-32)。我认为最简单的过程是创建空的 env,将其设置为subdir,然后继续进行(受限)安装。例如,

win-32, Python 2.7

win-32,Python 2.7

conda create -n py27_32
conda activate py27_32
conda config --env --set subdir win-32
conda install python=2.7

win-64, Python 3.7

win-64,Python 3.7

conda create -n py37_64
conda activate py37_64
conda config --env --set subdir win-64
conda install python=3.7

Alternatively, if you need to, for example, create an environment from a YAML file, but want a win-32platform, one can use the CONDA_SUBDIRenvironment variable:

或者,例如,如果您需要从 YAML 文件创建环境,但想要一个win-32平台,则可以使用CONDA_SUBDIR环境变量:

set CONDA_SUBDIR=win-32
conda env create -f env.yaml -n my_env_32
set CONDA_SUBDIR=
conda activate my_env_32
conda config --env --set subdir win-32

The nice thing about this procedure is the variable will now always be set whenever activating the env, so future changes to the env will remain within the specified subdirectory.

这个过程的好处是现在每当激活 env 时都会设置变量,因此将来对 env 的更改将保留在指定的子目录中。



Ad HocConstraints

临时约束

It is also possible to specify the platform in the --channel|-cargument:

也可以在--channel|-c参数中指定平台:

conda install -c defaults/win-32 --override-channels python=3.7

Here the --override-channelsis required to ensure that only the provided channel(s) and subdirectory (win-32) is used.

这里--override-channels需要确保仅使用提供的频道和子目录 ( win-32)。

However, setting the subdiron the whole env is likely a more reliable practice.

但是,在整个 env 上设置subdir可能是更可靠的做法。



[Untested!] YAML Constraints

[未经测试!] YAML 约束

Given the previous option, it may also be possible to use subdirspecifications in a YAML environment definition. For example,

鉴于前面的选项,也可以在 YAML 环境定义中使用subdir规范。例如,

name: my_env
channels:
 - defaults/win-32
dependencies:
 - python=3.7

If someone tries this and it works, please comment - or just edit the answer.

如果有人尝试此操作并且它有效,请发表评论 - 或者只是编辑答案。