How to install Python 2 on macOS 12.3+ - python

macOS 12.3 update drops Python 2 and replaces it with version 3:
https://developer.apple.com/documentation/macos-release-notes/macos-12_3-release-notes
Python
Deprecations
Python 2.7 was removed from macOS in this update. Developers should use Python 3 or an alternative language instead. (39795874)
I understand we need to migrate to version 3, but in the meantime we still need version 2. Homebrew does not seem to have it anymore:
brew install python#2.7
Warning: No available formula with the name "python#2.7". Did you mean python#3.7, python#3.9, python#3.8, python#3.10 or python-yq?
brew install python2
Warning: No available formula with the name "python2". Did you mean ipython, bpython, jython or cython?
What gives?

You can get any Python release, including the last Python 2, from the official download site:
https://www.python.org/downloads/release/python-2718/ → macOS 64-bit installer

A bit more relevant information. It was removed due to the fact that macOS remove the support of python2, and there is an open issue to remove all the python2 formulae as well.
Instead of using python official installer, I would suggest using pyenv, which is easier to manage multiple python versions in your local.

Related

Modules installed but not found by Python in Raspberry Pi 3

I have a Raspberry Pi 3 with Raspbian and I upgraded python version from 3.7 to 3.8. If I type python --version in the terminal the correct version appears as the system version. However none of the modules that I have installed AFTER the version change seem to work. Python gives ModuleNotFoundError when trying to import ANY of the modules that I have installed.
I can see the modules with pip freeze but Python seems to not be able to find them.
I followed this instructions to purge 3.7. I reinstalled pip after purging python 3.7 but pip as again installed in /home/pi/.local/lib/python3.7/site-packages/pip. How can I get rid of 3.7 completely?
python3.8 -m pip install SomePackage # specifically Python 3.8 should work.
More documentation here: https://docs.python.org/3/installing/index.html#work-with-multiple-versions-of-python-installed-in-parallel
Just to summarize the comments and suggestions from other answers:
The problem I have was caused by the fact that even I had set Python 3.8 as default and python -v was pointing to Python 3.8 the pip script was installing modules for Python 3.7.
The suggested solution was to use pip3.8 (or whatever version someone might have) to install packages for that equivalent Python version and that works good.
Ideally best option if someone wants to have multiple versions of python is to use pyenv. You can create multiple virtual environments with multiple python versions.
However Do not uninstall the default Python. I have also tried to uninstall the default Python 3.7 to avoid having two versions of python 3 and keeping track of which module is installed where. This was a bad idea. I did not know that many Linux distributions have applications which use the default Python. You might get a black screen and who knows what other problems see this discussion Removed Python 3 on 18.04, how can I fix my system?

How can I know which version of pip package to install?

I'm using Ubuntu 14.04, with Python 3.4.3. I want to install the latest version of ansible-lint that is supported on that Ubuntu version and with this Python version.
One of the dependencies of the ansible-lint package is ansible.
When I simply use pip3 install ansible-lint I install for me a version of ansible that is not supported - since if I run ansible-lint --version I get the following error:
Ansible requires a minimum of Python2 version 2.6 or Python3 version 3.5. Current version: 3.4.3.
So how can I make sure that pip3 install the correct version with the correct versions of the dependencies?
From the documentation:
Python-3.5 was chosen as a minimum because it is the earliest Python-3
version adopted as the default Python by a Long Term Support (LTS)
Linux distribution (in this case, Ubuntu-16.04). Previous LTS Linux
distributions shipped with a Python-2 version which users can rely
upon instead of the Python-3 version
Also specifically:
However, if you happen to be testing Ansible code with Python-3.4 or
earlier, you will find that the byte string formatting here won’t
work. Upgrade to Python-3.5 to test.
In short it seems like no version of ansible is good on python 3.4 - you should upgrade to at least 3.5. If you don't want to change the system-wide version, look into creating virtual environments for this project.

Is it possible to use Cocoa API with Python?

I was wondering if it is possible to use Cocoa (Apple's API) with Python, so being able to run any code like in this link, so NSWorkspace functions and so on, this might be a super stupid question, but I was still wondering if it was possible...
Yes.
There are Python packages, kind of wrappers around Objective-C, install them like:
$ pip install pyobjc-core
$ pip pyobjc-framework-Cocoa
$ pip pyobjc-framework-Quartz
https://pypi.org/project/pyobjc-framework-Cocoa/
MacOS by default comes with python 2.7 and pyObjC 2.5, and has done for years. If you want a newer version of either python, such as python 3, or the newest version of pyObjC, then you have to install it yourself.
The latest version of pyObjC is 5.2.
However, on a default installation of MacOS from at least Snow Leopard onward, the following should work:
python
>>> import Appkit
>>> AppKit.NSWorkspace.sharedWorkspace().runningApplications()
Catalina is stated to be the last version to contain python bundled.

nltk_contrib for python latest version (python version > 3)

I have installed python 3.5 and NLTK. Also, I have installed the nltk_contrib library from https://github.com/nltk/nltk_contrib which is not supporting to the latest version of python. The error is that the library written for older version. So, the library has print without parenthesis which will not support to the latest version of Python.
Is there any latest version nltk_contrib without down grade the python?
Thanks in advance.
nltk contrib has not been ported to python 3 yet.
See this answer for more details:
How to install nltk_contrib in Anaconda
You need to figure out which part of the code you need for python 3 and either port it yourself.
Or set up a virtualenv or and env for anaconda with python 2.7 and use it there.
http://conda.pydata.org/docs/using/envs.html

How to install python 2.7.4 on OSX Mountain Lion

I tried installing python 2.7.4 by overwriting the native Apple's installation of python 2.7 by following this link (wolfpaulus.com), and as a result both easy_install and pip no longer work.
This link (apple.com) suggests the native Apple bundle should never be deleted.
Is it possible to restore python 2.7 supplied by Apple without reinstalling OSX?
Is there a definitive guide to upgrading python to 2.7.4 on OSX Lion? For instance, before breaking my set up I could install bumpy, django into 2.7.4, but matplotlib refused to install to anything but 2.7, either from binaries or source.
Thanks.
It is absolutely not a good idea to replace Apples default python installation. If you have done, then you could try with the following command
" brew install python --framework "
R
You should never remove anything in /System/Library or /usr (other than /usr/local). Files installed in those locations are managed by Apple as part of OS X. At worst you can fatally damage your OS X system; at best you are at risk of all of your changes being wiped out by the next software update. In particular with Python, you’ve just wiped out Apple-supplied extensions to Python that are not available elsewhere. The right way to update a system component like Python is to install the new version at a different location, like the python.org Python installers do in /Library/Frameworks, and then manage access by modifying your shell PATH environment variable. Unfortunately, Apple does not make it easy to install individual components of OS X that have been mistakenly removed. To properly restore all of the system Python that you've deleted, the easiest approach is to reinstall OS X itself.
When you install another version of Python, you should also use it to install additional versions of pip or easy_install. Generally, each Python version should have its own version of each.

Categories

Resources