You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to upgrade embedded Python 2.7 to 3?

I need to upgrade the 'navite' Python 2.7 that comes with the OS to Python 3. Not to install another version of Python but to upgrade the existing. Is it possible?


When I try to upgrade it I get smth like "This can't be done" without explanations. Later I read that some critical components of OS are secured even for root.

$ brew install python3

and some other ways do not help. Now I have python3 installed, but somewhere in /Library so when I

$ python --version

I get

Python 2.7.10

and

$ python3

starts Python 3.5.1


So, how to upgrade the 'native' python that comes with system?

MacBook Air (13-inch Mid 2013), OS X El Capitan (10.11)

Posted on Mar 12, 2016 4:57 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 12, 2016 7:51 AM

Update your PATH to reference where your upgraded python is installed, with that directory somewhere in the PATH list ahead of the system version. This will cause your Python to be launched in preference to the system version, without altering the system version of Python.

Similarly, you'll probably want to export PYTHONPATH, or whatever your version of Python is using for that.

There should be directions for PATH and PYTHONPATH associated with this homebrew recipe, though I've not looked for that.

Complete customization and upgrading the pre-installed version is an approach much better suited to Linux or BSD. Those platforms are designed to be mix-and-match, and target end-users that want and need that flexibility. On OS X, upgrading the installed components tends to end badly, given dependencies and Apple upgrades. With El Capitan 10.11 and later, SIP deliberately prevents these sorta of changes, as replacing system components is something malware also does.

5 replies
Question marked as Top-ranking reply

Mar 12, 2016 7:51 AM in response to acme.ag

Update your PATH to reference where your upgraded python is installed, with that directory somewhere in the PATH list ahead of the system version. This will cause your Python to be launched in preference to the system version, without altering the system version of Python.

Similarly, you'll probably want to export PYTHONPATH, or whatever your version of Python is using for that.

There should be directions for PATH and PYTHONPATH associated with this homebrew recipe, though I've not looked for that.

Complete customization and upgrading the pre-installed version is an approach much better suited to Linux or BSD. Those platforms are designed to be mix-and-match, and target end-users that want and need that flexibility. On OS X, upgrading the installed components tends to end badly, given dependencies and Apple upgrades. With El Capitan 10.11 and later, SIP deliberately prevents these sorta of changes, as replacing system components is something malware also does.

Mar 12, 2016 7:51 AM in response to acme.ag

Never tamper with the OS X installation of Python, as Apple Applications, System Frameworks, and third-party applications contain Python scripts that are dependent upon the expected OS X installation version of Python. That advice extends to adding third-party Python modules into OS X System locations.


Python 2 and Python 3 development streams are mutually exclusive, so the only upgrade to 2.7 would be from Apple, or the installation of 3.5.1 via a package manager which you have done.


Mr Hoffman's advice about PATH precedence will determine whether you run the OS X Python 2.7.10, the Brew 3.5.1 version, and their respective IDLE IDE. By default, the PYTHONPATHenvironment variable is present, but empty. This default tells the OS X Python to look in its standard location. If you want to access the Python v3.5.1 library modules, then you need to know where Homebrew stashed them, and update PYTHONPATH with that library path. When you want to use the OS X Python, you set PYTHONPATH back to blank. I use a Python version manager to do this for me.


With practice, you can write adaptable Python code that runs on both Python 2.7 and Python 3.5 versions without errors.

Mar 12, 2016 9:20 AM in response to acme.ag

I suggest that you use homebrew to deinstall Python 3.5.1 (brew uninstall python3). Now, brew install pyenv. This gives you a Python version management tool that allows you to install Python 3.5.1 into $HOME/.pyenv. Read the docs, or search on the web for Pyenv to gain additional familiarity.


for Homebrew maintenance, regularly perform brew update, followed by brew upgrade. Order is important.

Mar 12, 2016 9:31 AM in response to acme.ag

Here are the homebrew instructions for Python. Start there, if you've not already read through that. If those aren't sufficient, I'd suggest submitting some comments to the Homebrew folks — your feedback here can help the Homebrew Python folks provide better and more targeted documentation.


Your path may already have the location of python in your path; I'd expect to find that in /usr/local/bin, with Homebrew. Here's what the local version of $PATH looks like, though this system has X11 and maybe some other bits yours doesn't and won't.

$ printf "$PATH\n"

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/opt/local/bin:/opt/lo cal/sbin

$


In the above, you'll see /usr/bin/local in the first :-separated position in the PATH. If you have that anywhere ahead of /usr/bin, you will not need to modify your path. If you don't have that in your PATH, you'll need to add it.


PATH normally gets configured in .bash_profile or .bashrc, or such; whatever you're using on your system. If you don't have one, probably .bash_profile. See this page for details on configuring PATH and related information on recent OS X...


If you're not sure where a command you're using is located in the file system, here's how to see where a particular command is located:


$ whereis python

/usr/bin/python

$


If you don't have a python entry locally in /usr/local/bin and do have a python3 entry from Homebrew, then you're probably going to end up adding a link for python into your path (and that's rather less recommended) or (easier, recommended) add the following alias into your .bash_profile — or just enter python3, of course — and this will cause the python command to be the same as the python3 command:

alias python='python3'


There's also the Apple Shell Scripting Primer, which might help learn more about the terms and topics...

How to upgrade embedded Python 2.7 to 3?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.