1

I am trying to create a virtual environment on my Mac using terminal commands for Python 2.7 but it is not working.

I issue the following commands;

mkdir Environments
cd Environments
python -m virtualenv python27 
source python27/bin/activate
python --version

Instead of reporting the version of Python as 2.7 it still says I have version 3.9.7. Terminal displays the following but the version option doesn't report Python 2.7

(python27) (base) StephenLearmonth@Stephens-MBP Environments % python --version
Python 3.9.7 

I have macOS Monterey 12.3.1 and when I type zsh --version I get;

zsh 5.8 (x86_64-apple-darwin21.0)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Stephen501
  • 153
  • 8
  • `python` is probably resolving to your Python 3.9 interpreter, so then virtualenv is making the environment with that interpreter. If you read the docs for virualenv, it goes through the command line options for explicitly providing the interpreter with the `-p` option. – juanpa.arrivillaga May 03 '22 at 17:16
  • Provided that 1) you use python3 by default and 2) python 2.7 is effectively available somewhere on your machine, the correct syntax would be `python -m virtualenv --python /path/to/python27 python27` – Zeitounator May 03 '22 at 17:17

1 Answers1

0

Python2 is end-of-life and was removed from MacOS starting at version 12.3

If you have a strong need for Python2, then you'll need to install it separately (or use Docker), as virtualenv cannot create Python2 environments without having the Python2 interpreter installed first.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Can't I download Python 2.7 to my macOS and create the virtual environment that way? – Stephen501 May 03 '22 at 17:17
  • Sure. If you can find them. - https://www.python.org/downloads/ – OneCricketeer May 03 '22 at 17:17
  • 1
    You can still [download a 2.7 interpreter](https://www.python.org/downloads/release/python-2718/) and create a virtual environment with it using `virtualenv` – juanpa.arrivillaga May 03 '22 at 17:19
  • @juanpa.arrivillaga I tried that but it doesn't work on my macOS. It reports that it cannot install the software because the software does not exist. – Stephen501 May 03 '22 at 17:21
  • 1
    @Stephen501 Try using [pyenv](https://github.com/pyenv/pyenv) I was able to install `2.7.18` a few weeks ago with it on my Mac. But the real question you should be asking yourself - why do you **need** Python 2.7? What scripts/modules are you using that cannot be upgraded? Or haven't been upgraded in the past decade? – OneCricketeer May 03 '22 at 17:22
  • I need Python 2.7 on my system so that I use CoreMLTools. I am following Angela Yu's course on iOS development – Stephen501 May 03 '22 at 17:42
  • @Stephen501 I suggest you find a new course. Version 4.1 deprecated Python2 support - https://coremltools.readme.io/docs/installation – OneCricketeer May 03 '22 at 18:28
  • 1
    @OneCricketeer Thanks. I've since found out that there are other later versions of Coremltools that support later versions of Python. – Stephen501 May 04 '22 at 10:28