Project

General

Profile

Actions

Bundle-python » History » Revision 18

« Previous | Revision 18/44 (diff) | Next »
Saúl Ibarra Corretgé, 09/28/2016 02:32 PM


Building a Python Framework to bundle inside Blink

In order to avoid using the system Python a custom Framework build is needed. Using a bundled Python version will make the package bigger in size, but all package versions are controlled and not up to the environment. Also, we can use the latest Python version, with latest bugfixes and features, since Apple only updates the system Python version on every major OS release.

The following instructions only apply for 64bit builds, 32bit builds are no longer supported.

Building the Python Framework itself

  • Install it using Homebrew
brew install python

The framework will be installed and linked with Homebrew supplied OpenSSL and SQLite versions. Those libraries will need to be copied too.

NOTE: Be careful when copying the framework around, it contains symlinks and if cp -r is used the size will we doubled, use cp -a instead.

  • Reduce the size of the Python Framework:

There are a number of things that can (and must when submitting a sandbox app to Mac App Store) be removed from the framework directory to make it smaller in size:

*.pyc
*.pyo
Versions/Current/lib/python2.7/config/python.o
Versions/Current/Mac
Versions/Current/bin
Versions/Current/share
Versions/Current/Resources/*
Versions/Current/Resources/*.app
Versions/Current/lib/python2.7/test
Versions/Current/lib/python2.7/plat-*
Versions/Current/lib/python2.7/idlelib
Versions/Current/lib/python2.7/curses
Versions/Current/lib/python2.7/lib2to3
Versions/Current/lib/python2.7/lib-tk
Versions/Current/lib/python2.7/bsddb
Versions/Current/lib/python2.7/lib-dynload/gdbm.so
Versions/Current/lib/python2.7/lib-dynload/readline.so
  • Prevent system paths from being used with this bundle

Replace Versions/Current/lib/python2.7/site.py with an empty file.

Compiling PyObjC

In order to get a PyObjC version that will work with the framework created above (Python 2.7, 64bits) an equivalent Python must be used to compile it. That is, if has to be a Python 2.7 version (it doesn't have to be the exact version) and it has to be a 64bit version. The MACOSX_DEPLOYMENT_TARGET must also be set to the appropriate value.

PyObjcC can be installed with easy_install or pip. We install it in 2 steps to save some compilation time due to a bug in the build system:

pip install pyobjc-core
pip install pyobjc

When compiling PyObjC a Python package will be created for every system framework, but not all of them are needed (at the moment), so just pick the ones we use:

AddressBook
AppKit
Cocoa
CoreFoundation
Foundation
JavaScriptCore
LaunchServices
PyObjCTools
Quartz
ScriptingBridge
StoreKit
WebKit
objc

NOTE: The objc package is located inside a PyObjC directory, just copy it from there, without the parent directory.

NOTE: PyObjCTools is not a valid Python package, as it lacks a __init__.py file, an empty one needs to be manually created.

Module exceptions

When copying built Python modules into the distribution folder, care must be taken with the 2 following packages:

  • zope: an empty __init__.py file must be created in the zope directory
  • cryptography: the *-dist.info must be copied too

Creating a sandbox (Python virtualenv)

sudo easy_install pip
sudo pip install virtualenv virtualenvwrapper

Add to ~.bashrc

# Virtualenv
export WORKON_HOME=$HOME/.virtualenvs
export PIP_VIRTUALENV_BASE=$WORKON_HOME
export PIP_RESPECT_VIRTUALENV=true
export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh
[[ -f /usr/local/bin/virtualenvwrapper_lazy.sh ]] && source /usr/local/bin/virtualenvwrapper_lazy.sh

Creating a sandbox:

mkvirtualenv -p $(which python2.7) sandbox

Exiting the sandbox:

deactivate

Entering the sandbox:

workon sandbox

Updated by Saúl Ibarra Corretgé about 8 years ago · 18 revisions