146

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/myenv/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/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, 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.

AKG
  • 1,799
  • 4
  • 13
  • 23

3 Answers3

178

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.

ilmarinen
  • 4,557
  • 3
  • 16
  • 12
  • 1
    Nice, tried it and it works - now, `ipython` calls the desired version. Now only the part of my question remains, if there is a way to call with `python` instead of `python3` the desired version. – AKG Jun 22 '19 at 08:24
  • It should have worked already, but it's possible that you have something going on in your environment. You can try running the command line "which python" to see how linux is determining the location of python. If you have an alias set up for python, that might also mess things up (that is , you have used the ocmmand line too "alias") – ilmarinen Jun 22 '19 at 08:37
  • This is the command I have run to get the paths which I reported in my question. – AKG Jun 22 '19 at 08:41
  • 1
    Is there a reason to have to do this? It at least _feels_ like conda should be taking care of this – roganjosh Jun 22 '19 at 08:42
  • 1
    conda relies a fair bit on linux to do the lookup for an executable, stepping away from that would break a lot of things. Now, if I would create an environment as I've done in my, reasonably clean, linux machine, I would be able to run an ipython console with python 3.3 simply by running the command "ipython". You seem to look up paths in /home/myname/.local/bin before the environment path. I don't know why this is, but as long as that path has precedence over your activated environment you will pick up the wrong ipython executable. – ilmarinen Jun 22 '19 at 08:54
  • (Continuing comment...) Given the way anaconda is activating an environment (it prepends PATH with the appropriate path), I can only theorise that there is an alias set up. – ilmarinen Jun 22 '19 at 08:54
  • It's interesting that your theory relies on Linux since I'm on Windows and Anaconda addresses most of the Windows-related issues with installing the scientific stack :) – roganjosh Jun 22 '19 at 09:01
  • @roganjosh, on Windows there is afaik only the PATH, so your lookup for an executable can only be done through it (or current working directory), so it's in a way simpler. – ilmarinen Jun 22 '19 at 09:05
  • @roganjosh Exactly, I also had the _feeling_ that having different environments with different default python versions is one of the main ideas of conda environments - this also gave me the courage for asking. – AKG Jun 22 '19 at 09:07
  • This accepted answer is the answer to a different question. How do you create conda environment with a specific python version? Do you need the `ipython` part in general, or is `conda create -n "myenv" python=3.3.0` sufficient? – aviator May 04 '21 at 20:25
  • The answer solved the problem, which is in the interaction between python and ipython (or any python-based executable) you want to use in said environment. If you don't need the other executables you don't need to install it – ilmarinen May 04 '21 at 20:54
21

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 use

conda env create -f environment.yml

for using an environment.yml file instead of requirements.txt:

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

To automate a backup of the current environment run:

conda env export > environment.yml

Use this command to remove the environment:

conda env remove -n py33
bbaassssiiee
  • 6,013
  • 2
  • 42
  • 55
  • 1
    Just to make the example complete, create the environment described above with `conda env create -f environment.yml` – Daniel Flemström Sep 06 '22 at 05:36
  • To make the example even more complete, you can create environment.yml automatically by running `conda env export > environment.yml` – dinarkino Jan 31 '23 at 12:53
  • By the way, `conda install -f -y -q --name py33 -c conda-forge --file requirements.txt` didn't work for me because some packages were "not available from current channels". So, I activated env and installed packages with `pip install -r requirements.txt` – dinarkino Jan 31 '23 at 13:05
1

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.

SamKChang
  • 91
  • 1
  • 8