1

I am trying to create a new conda Python 2.7 environment. For this purpose I am typing this in terminal:

conda create -n my_env_name python==2.7

After activating created environment (conda activate my_env_name) and checking Python version (python --version) I am getting Python 3.10.2

No matter which Python version I am trying to use in the new environment I am always getting Python 3.10.2 answer when checking the Python version.

Any idea what is wrong and how to solve this?

(I am working on iMAC, Chip Apple M1, macOS Monterey 12.1)

(After doing the same on my old machine everything works fine and after checking the Python version in a newly created environment I am getting Python 2.7.18.)

Here are some additional info.

  1. When env is activated commands:
  • which python gives- /opt/local/bin/python
  • type python gives- python is /opt/local/bin/python
  • echo $PATH gives- /opt/local/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/uros/Desktop/iraf-main/unix/hlib/ecl.sh://Users/uros/.iraf/bin:/opt/anaconda3/envs/py27/bin:/opt/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/Apple/usr/bin
  1. When env is not activated commands:
  • which python gives- /opt/local/bin/python
  • type python gives- python is /opt/local/bin/python
  • echo $PATH gives- /opt/local/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/uros/Desktop/iraf-main/unix/hlib/ecl.sh://Users/uros/.iraf/bin:/opt/anaconda3/bin:/opt/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/Apple/usr/bin

After running conda list output is this: enter image description here

user16454053
  • 101
  • 1
  • 9
  • Is this means that I need to install anaconda2/miniconda2? – user16454053 Mar 03 '22 at 15:19
  • @user16454053 can you please check what `which python` and `type python` report when your env is active and when it is not? – FlyingTeller Mar 03 '22 at 15:33
  • @FlyingTeller Doh! sure enough, you're right. `conda create -n p27 python==2.7.18` followed by `conda activate p27` and `python --version` just gave me `2.7.18`. I take it all back! – joanis Mar 03 '22 at 15:35
  • 1
    @user16454053 `conda create -n p27 python==2.7` gave me a `PackagesNotFoundError` saying `python==2.7` could not be found. I had to specify `python==2.7.18` for it to actually work. Did you get any error message when you tried to create your environment? – joanis Mar 03 '22 at 15:38
  • No I am not getting any errors when creating environment. – user16454053 Mar 03 '22 at 15:52
  • What does `which python` and `type python` as well as `echo $PATH` give you when run while the env is active and while it is not? What does `conda list` give you after activating the env? I would sugggest to add that info to the question rather than posting in the comments – FlyingTeller Mar 04 '22 at 12:34
  • I guess you can always install the specific version of python after you have created the environment? –  Mar 07 '22 at 16:45
  • If you have in mind to run IRAF with 32 bit, please keep in mind that Apple M1 machines do not allow executing 32-bit code. You need to use the 654 bit versions. – olebole Mar 11 '22 at 12:06
  • @user16454053 Just came back to look at this question. I believe the PATH you show when the env is activated indicates the problem: `/opt/anaconda3/envs/py27/bin` is in the PATH, but *after* `/opt/local/bin`. That's just weird to me. On my machine, `conda activate py27` puts that bin path (well, its equivalent on my machine) first on my PATH. Try `export PATH=/opt/anaconda3/envs/py27/bin:$PATH` and see if you finally get 2.7.18. That'll confirm my theory of the problem, but I don't know why your PATH gets set in the wrong order, that's the real question now. – joanis Mar 15 '22 at 15:04
  • Does https://stackoverflow.com/a/65183109/3216427 help? I.e., try `source /opt/anaconda3/etc/profile.d/conda.sh; conda activate py27`. Does it make any difference? – joanis Mar 15 '22 at 15:09

2 Answers2

1

I had the same problem with my M1 Mac.

As it turns out when working with python versions below 3.8 according to this post here there is no support for running osx-arm64. They go into the details on how to fix this.

You can also follow this blog post from Danny Cunningham How to Manage Conda Environments on an Apple Silicon M1 Mac Manage both ARM64 and x86 Python environments using conda

in your case it should be something like:

CONDA_SUBDIR=osx-64 conda create -n my_env_27_x86 python=2.7 

I hope this helps,

0

TL;DR With conda3, specify the Python 2.7 version fully, e.g., 2.7.18.

When I tried to reproduce your command in my own conda3 environment, I got this error:

PackagesNotFoundError: The following packages are not available from current channels:

  - python==2.7

However, when I specified the version of Python 2.7 fully, it worked for me:

conda create -n p27 python==2.7.18
conda activate p27
python --version

shows that Python 2.7.18 is the default Python in that environment.

Alternative: use conda2

With Anaconda3/Miniconda3, the default Python will be Python 3.x, whereas with Aanconda2/Miniconda2, the default Python would be Python 2.7.x. On your old machine, you might have had conda2 installed, which would explain why it worked.

Thanks to @FlyingTeller for pointing out conda2 is not necessary, though.

joanis
  • 10,635
  • 14
  • 30
  • 40
  • I just run the same commands as you and does not work for me. Python version is again 3.10.2 – user16454053 Mar 03 '22 at 15:55
  • Does the `conda create` command output any error messages? Are there other instances of Python on your path, as `which python` and examining `$PATH` might reveal? – joanis Mar 03 '22 at 15:57
  • No errors is created. After typing which python I am getting /opt/local/bin/python. – user16454053 Mar 03 '22 at 16:00
  • Does `conda activate` actually add the new env's `bin` folder at the front of your `$PATH`? – joanis Mar 03 '22 at 16:01
  • `/opt/local/bin/python` is your system-wide Python, not the one installed by conda. I think you need to read up on activating conda correctly on your new machine. – joanis Mar 03 '22 at 16:02
  • Hmm interesting point. Thanks I will try to see how to do this (many new stuff for me :) ). – user16454053 Mar 03 '22 at 16:04
  • Any idea on how to activate conda correctly ... I try everything I know and problem is still there. – user16454053 Mar 03 '22 at 16:27
  • You'll have to share more information: how did you install conda? Did you let Conda add some lines to your `.bashrc`? What does your PATH look like? What version of conda are you using? Review https://docs.conda.io/projects/conda/en/latest/user-guide/install/macos.html and make sure you followed all the steps. – joanis Mar 03 '22 at 18:29