简体   繁体   English

如何创建具有特定 python 版本的 conda 环境?

[英]How to create conda environment with specific python version?

I have miniconda3 installed and since I would like to have an environment with python version 3.3.0, I create it via我安装了miniconda3 ,因为我想拥有一个 python 版本 3.3.0 的环境,所以我通过

conda create -n "myenv" python=3.3.0

However when I activate the environment via但是,当我通过激活环境时

conda activate myenv

python has version 2.7.15 and path python 有版本 2.7.15 和路径

/usr/bin/python

and ipython has python version 3.6.8 and path并且 ipython 具有 python 版本 3.6.8 和路径

/home/myname/.local/bin/ipython

I can access the correct python with python3 which is at我可以使用位于的python3访问正确的 python

/home/myname/miniconda3/envs/myenv/bin/python3

however, ipython3 has python version 3.6.8 again.但是, ipython3再次具有 python 版本 3.6.8。

conda install python=3.3.0

left the situation unchanged.保持现状不变。

A solution would be to open IPython via一个解决方案是通过打开 IPython

python3 -m IPython

however, while this works fine for python here I get the error message然而,虽然这适用于python在这里我收到错误消息

/home/myname/miniconda3/envs/myenv/bin/python3: No module named IPython

Is it possible to access with the commands python and ipython both python version 3.3.0 in that specific environment, ie not by setting an alias in the .bashrc ?是否可以在该特定环境中使用命令pythonipython访问 python 版本 3.3.0,即不是通过在.bashrc中设置别名来访问?

EDIT:编辑:

Turns out that this problem does not occur if you select version 3.3 instead of 3.3.0 together with @ilmarinen's answer事实证明,如果您使用 select 版本 3.3 而不是 3.3.0 以及@ilmarinen 的答案,则不会发生此问题

conda create -n "myenv" python=3.3 ipython

everything works fine and python as well as ipython result to version python 3.3.5 .一切正常, python以及ipython结果到版本 python 3.3.5

You need to install ipython as well into your given environment您还需要将 ipython 安装到给定的环境中

conda create -n "myenv" python=3.3.0 ipython

The conda environments are prepended to your PATH variable, so when you are trying to run the executable "ipython", Linux will not find "ipython" in your activated environment (since it doesn't exist there), but it will continue searching for it, and eventually find it wherever you have it installed. conda 环境会添加到您的 PATH 变量中,因此当您尝试运行可执行文件“ipython”时,Linux 不会在您激活的环境中找到“ipython”(因为它不存在),但它会继续搜索它,并最终在安装它的任何地方找到它。

To create an environment named py33 with python 3.3.0, using the channel conda-forge and a list of packages:要使用 python 3.3.0 创建一个名为py33的环境,使用通道 conda-forge 和软件包列表:

conda create -y --name py33 python==3.3.0
conda install -f -y -q --name py33 -c conda-forge --file requirements.txt
conda activate py33
...
conda deactivate

Alternatively you can create an environment.yml file instead of requirements.txt:或者,您可以创建一个environment.yml文件而不是 requirements.txt:

name: py33
channels:
  - conda-forge
dependencies:
  - python=3.3.0
  - ipython

Use this command to remove the environment:使用此命令删除环境:

conda env remove -n py33

I had similar issue.我有类似的问题。 And I could't find many useful discussions.而且我找不到很多有用的讨论。

The problem for me was I have alias pointing python to miniconda python hardcoded in my shell config file when I execute conda init zsh .对我来说,问题是当我执行conda init zsh时,我在我的 shell 配置文件中硬编码了将 python 指向 miniconda python 的别名。 Somehow the init process copies the alias and always reload that, thus overwrites the "correct" version. init 进程以某种方式复制别名并始终重新加载它,从而覆盖“正确”版本。

After conda create -n py27 python=2.7 (my system default is 3.6), the version was correctly installed at miniconda3/envs/py27/bin/python . conda create -n py27 python=2.7 (我的系统默认是3.6)后,版本正确安装在miniconda3/envs/py27/bin/python But the activated evironment python was not pointing to it, as indicated by which python ,even if I deleted updated my shell config.但是激活的环境 python 并没有指向它,正如which python所指示的那样,即使我删除了更新的 shell 配置。

In the end it was solved by 'reverse' conda init (remove the generated conda function in .zshrc ), remove alias, and re-init.最后它通过'reverse' conda init 解决(删除 .zshrc 中生成的.zshrc函数),删除别名并重新初始化。

I guess other shell is using the same mechanism.我猜其他shell正在使用相同的机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM