Python on Mac

Python comes pre-installed on Mac OS X but, due to Apple's release cycle, this python can be up to two years old, and customized such that it may not behave exactly like other pythons yet fiddling with it may break your system.

Fortunately, Apple makes it possible to install additional Frameworks alongside any native ones:
  • Apple-shipped frameworks reside mostly in /System/Library/Frameworks
  • third-party frameworks live in some other […]/Library/Frameworks

Troubleshooting

When communicating on the gnumed-devel list about possible problems, please run your Terminal application in /Applications/Utilities/ and try launching python and to test if mx.DateTime is accessible
  • $ python
  • >>> import mx.DateTime
  • >>> from mx.DateTime import now
  • >>> now()
  • you should see something like <mx.DateTime.DateTime object for '2011-07-31 01:22:37.97' at 100530c38>

also, enter the following commands and be prepared to include (copy / paste) the relevant output in your communication:

  • $ which python
  • $ python -V
  • $ echo $PATH
  • $ ls /opt/local/var/macports/software/ | grep py
  • $ ls -al /opt/local/Library/Frameworks/Python.framework/Versions/
  • $ port select --list python
  • $ ls -al /System/Library/Frameworks/Python.framework/Versions/
  • $ ls -al /Library/Frameworks/Python.framework/Versions/

Python distributions

The python that would load in a Mac OS could come from any among

   /usr/bin <--- Apple's
   /opt/local/bin/ <--- MacPorts'
   /usr/local/bin/ <--- MacPython's
   /Library/Frameworks/Python.framework/Versions/Current/bin <--- MacPython's
   /sw/bin/ <--- Fink's

Apple's build of Python is installed in
   /usr/bin/python
   /System/Library/Frameworks/Python.framework

MacPorts – which we are recommending for GNUmed – installs in
   /opt/local/bin/
   /opt/local/Library/Frameworks/

MacPython installs in
   /Applications/MacPython\ 2.5 <--- folder
   /usr/local/bin/python <--- symlink
   /Library/Frameworks/Python.framework <--- Python executable and libraries.
   The installer adds this location to your shell path.
   To uninstall MacPython, you can simply remove these three things.

External links:

Miscellany

When it would be necessary to install a non-default Python, the alternative python's directory (for the MacPython distribution, it is /usr/local/bin) needs to appear in your path before /usr/bin

    • use the commands echo $PATH
    • if needed (assuming that you are using bash whereas tsch requires the setenv command):
      export PATH=/usr/local/bin:$PATH
    • then run ./gm-from-vcs.sh as in the following example:

iMac:~/Desktop/GNUmed-0.2.7.0/client jb$ echo $PATH
/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
iMac:~/Desktop/GNUmed-0.2.7.0/client jb$ export PATH=/usr/local/bin:$PATH
iMac:~/Desktop/GNUmed-0.2.7.0/client jb$ echo $PATH
/usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
iMac:~/Desktop/GNUmed-0.2.7.0/client jb$ ./gm-from-vcs.sh
Gnumed -> client
gm-from-vcs.log
...

also, you should add this same export line to your startup file (.bash_profile if you're using bash) so that this is the default for new terminals. Create the following text file and save it as ".bash_profile" in your home directory:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=/usr/local/bin:/$PATH;export PATH

# End of File

Apparently, on the Mac, environment variables in your .profile aren't visible to applications outside of the terminal so items like PATH, PYTHONPATH to be available (e.g.) to xcode apps, must be added to a new plist file that you create at ~/.MacOSX/environment.plist, see the first link below for more.

Credit for the above to

from a post to gnumed-devel from Sep 28, 2007:

A potentially useful site for Python on the Macintosh: http://www.nabble.com/Python---pythonmac-sig-f2970.html

Pasted forums notes:

I'll add a note in bold on the python.org Macintosh downloads page saying not to remove the Apple-provided version. http://www.nabble.com/forum/ViewPost.jtp?post=3957711&framed=y

--The "Recommended Option" -- for users without special needs or newbies that don't know what they need ----- use binary Python 2.4.* installer ----- ... after installing: -- Install TigerPython24Fix? (dunno if still necessary in 2007) -- Add /usr/local/bin to your PATH http://www.nabble.com/forum/ViewPost.jtp?post=2788866&framed=y

Yes. It works after I type export PATH=/usr/local/bin:$PATH. But next time if I open the terminal, I need do it again. How can I make it permanent? http://www.nabble.com/forum/ViewPost.jtp?post=2780641&framed=y

The python you installed lives in /usr/local/bin. If you want it to run when you type 'python' at the prompt you need to make sure that /usr/local/bin is before /usr/bin in your path. http://www.nabble.com/forum/ViewPost.jtp?post=745652&framed=y

To make sure /usr/local/bin is in your path before /usr/bin if your shell is bash, type... http://www.nabble.com/forum/ViewPost.jtp?post=2778505&framed=y

Create the following text file and save it as ".bash_profile" in your home directory. http://www.nabble.com/forum/ViewPost.jtp?post=2781399&framed=y

The thing I'm slightly unclear about is the best way to change my path on OSX. Changing things like ~/.profile and ~/.bash_profile change it for the shell, but do not change it systemwide. I've modified my ~/.MacOSX/environment.plist, which seems to do the job ...Yes. It is the way to change it system wide for any applications the do not check the $PATH environment variable. I only change the environment in those two ways (shell vars and environment.plist) and have no problems. And if I want a specific Python, /usr/bin/python or /usr/local/bin/python finds the one I intend to access. Maybe some of the more sage members of this community have more detailed or better advice, but that's what I recommend from my experience. http://www.nabble.com/forum/ViewPost.jtp?post=3934949&framed=y

Add /Library/Frameworks/Python.framework/Versions/Current/bin to your shell's PATH, which the macpython installer should do automatically. The major advantages of that are that all python stuff is nicely self-contained in the python framework and it is possible to do side-by-side installations of multiple versions of python (such as Python 2.4 and 2.5). http://www.nabble.com/forum/ViewPost.jtp?post=7735746&framed=y

'>What's the "framework"? If that's the entire Python interpreter and
'>library, installed in a way that can't interfere with whatever the
'>user is using for whatever other Python needs they have on their
'>machine -- that's what I'd need if I were to use something other than
'>the system Python.
And that is what py2app does. It will add the subsection of Python.framework that is needed for your application (based on the contents of setup.py and static analysis of the application) into the app bundle. Loader commands of binaries in the app bundle are rewriten to make sure they refer to the framework (and other libraries) inside the bundle instead of versions outside of the bundle. http://www.nabble.com/forum/ViewPost.jtp?post=3416888&framed=y

Why do I need MacPython?? What does it do? http://www.nabble.com/forum/ViewPost.jtp?post=6400598&framed=y

MacPython-specific programming info available at

For a long time I've been building apps with MacPython? and wxPython that run just fine on Mac and Windows, using py2app and py2exe to build the executables, and packaging the Win version with InnoSetup? and for Mac just making a double-clickable .dmg -- both dead simple for users (whom I expect to know even less than I do) http://www.nabble.com/Recurring-question---which-python-should-I-use--tf1278715.html#a3400430

You can use native extensions with the universal build, but you cannot use PPC extensions on Intel. To answer you're question: you're going to need a universal or Intel build of wxPython to create wx gui's on an intel mac. http://www.nabble.com/Recurring-question---which-python-should-I-use--tf1278715.html#a3400430

'> What do I need to hack to get the frameworks to install in my preferred
'> location, instead of the default? And why doesn't Python pick up the
'> ./configure flags correctly anyway?
It does pick up the configure flags correctly, but you need to pass the correct configure flags. If you'd have used --help you'd have seen that --enable-framework takes a path, which defaults to /Library/Frameworks. If you want it to go somewhere else, give it a different path. http://www.nabble.com/forum/ViewPost.jtp?post=6837806&framed=y

Older Mac OS Python-related information posted on the gm-devel list on Sep 15, 2005

Mac OS 10.3 Panther keeps the mxDateTime and pyPgSQL modules installed in
/Library/Python/2.3/
Topic revision: 01 Aug 2011, JamesBusser
 
Download.png
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback
Powered by Olark