How to create conda environment with specific python version?

PythonCondaMiniconda

Python Problem Overview


I have miniconda3 installed and since I would like to have an environment with python version 3.3.0, I create it via

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

/usr/bin/python

and ipython has python version 3.6.8 and path

/home/myname/.local/bin/ipython

I can access the correct python with python3 which is at

/home/myname/miniconda3/envs/mattention/bin/python3

however, ipython3 has python version 3.6.8 again.

conda install python=3.3.0

left the situation unchanged.

A solution would be to open IPython via

python3 -m IPython

however, while this works fine for python here I get the error message

/home/myname/miniconda3/envs/mattention/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, i.e. not by setting an alias in the .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

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 Solutions


Solution 1 - Python

You need to install ipython as well into your given environment

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.

Solution 2 - Python

To create an environment named py33 with python 3.3.0, using the channel conda-forge and a list of packages:

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:

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

Use this command to remove the environment:

conda env remove -n py33

Solution 3 - Python

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. Somehow the init process copies the alias and always reload that, thus overwrites the "correct" version.

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. But the activated evironment python was not pointing to it, as indicated by which python,even if I deleted updated my shell config.

In the end it was solved by 'reverse' conda init (remove the generated conda function in .zshrc), remove alias, and re-init.

I guess other shell is using the same mechanism.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAKGView Question on Stackoverflow
Solution 1 - PythonilmarinenView Answer on Stackoverflow
Solution 2 - PythonbbaassssiieeView Answer on Stackoverflow
Solution 3 - PythonSamKChangView Answer on Stackoverflow