196

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.

3 Answers 3

253

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.

11
  • 2
    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
    Commented Jun 22, 2019 at 8: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
    Commented Jun 22, 2019 at 8:37
  • This is the command I have run to get the paths which I reported in my question.
    – AKG
    Commented Jun 22, 2019 at 8:41
  • 2
    Is there a reason to have to do this? It at least feels like conda should be taking care of this
    – roganjosh
    Commented Jun 22, 2019 at 8:42
  • 2
    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
    Commented Jun 22, 2019 at 8:54
38

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
3
  • 2
    Just to make the example complete, create the environment described above with conda env create -f environment.yml Commented Sep 6, 2022 at 5:36
  • 2
    To make the example even more complete, you can create environment.yml automatically by running conda env export > environment.yml
    – dinarkino
    Commented Jan 31, 2023 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
    Commented Jan 31, 2023 at 13:05
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.