⬆️ ⬇️

Setting up the environment on Mac OS and building a standalone application with PySide / PyQt

image

Now it is becoming more and more convenient to use HTML and JavaScript in application interfaces. And just such a task has recently appeared before me, with one important condition - the output should be a completely standalone application that does not require the installation of additional libraries and is able to run in the usual user environment.



Due to its natural charm, Python + PySide with WebKit was selected as the main language on board, and py2app was used to build the app bundle.



Problems appeared, at the first attempt to run the application on a clean system - a large number of external dependencies were discovered, which py2app could not resolve on its own. Under the cat step by step instructions how this problem was solved.



In an attempt to understand why py2app does not include all the necessary libraries in the bundle, in one article the idea was found that creating a completely autonomous application with an interpreter built into Mac OS is impossible and you need to use MacPort. So do.

')

Training



So, we need:

  1. Xcode
  2. Macports
  3. Python
  4. virtualenv
  5. Qt4
  6. PySide
  7. py2app


Xcode



It carries both Mac OS development tools and the gcc we need.

I used the latest version - 4.2, but with earlier (3.X) problems should not be.

If not, take it from the Mac OS installation discs or from the Apple website (free registration is required)



Macports



A package manager that allows you to put a lot of useful tools and libraries on a poppy.

Installed in the standard way (pkg). Take with MacPorts.org .

After installation, you can upgrade just in case:

$ sudo port selfupdate



Python



Install from MacPorts:

$ sudo port install python27

If you are using a different version of python, simply replace here and below.



virtualenv



A tool that allows you to create isolated environments for python. It is very convenient when you need to have several versions of python or different projects require different libraries, simplifies further deployment. In our case, using virtualenv, we will create an “autonomous sandbox” from which the standalone application will appear.

$ sudo easy_install virtualenv



In the load we put virtualenvwrapper, which simplifies working with the environment:

$ sudo easy_install virtualenvwrapper



Qt4



We put from the office. the site , I used Cocoa: Mac binary package for Mac OS X 10.5 - 10.6



PySide



A library for Python that allows you to use all the power and strength of Qt from a familiar programming language.

I tried to install PySide immediately into a virtual environment via pip or easy_install (which would be more logical), but it turned out that PySide is not supplied as needed for these package managers, and as a result a lot of time was killed trying to compile the library ... it stopped after a couple of days, when I caught myself sitting and rules the source code of some third-party lib =)

As a result, we go through the installation provided by developers pekedzh .



Setting up the environment



The preparatory part of this is completed, go directly to the creation of a virtual environment and its content.



Create a virtual environment



Choose a place where the environment will live, for this, in ~ / .bash_profile we add

export WORKON_HOME=~/Envs

source /usr/local/bin/virtualenvwrapper.sh




Create an environment

$ . ~/.bash_profile

$ mkdir -p $WORKON_HOME

$ mkvirtualenv --no-site-packages --python=/opt/local/bin/python2.7 py27

# --no-site-packages - ,

# --python=/opt/local/bin/python2.7 -

# py27 -




After that, the basic directory structure and the necessary files will be created in $ WORKON_HOME / py27. In addition, the environment will become active, at the beginning of the command line will appear (py27). In the future, to activate you will need to perform

$ workon py27



You can call a python, make sure that the correct version is running.

(py27) $ python

Python 2.7.2 (default, Jul 21 2011, 01:27:20)

[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>>




Now you need to transfer all the libraries and modules to the new environment:

# PySide packages

$ cd /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

$ mv PySide/ ~/Envs/py27/lib/python2.7/site-packages/

$ mv pysideuic/ ~/Envs/py27/lib/python2.7/site-packages/



# PySide libs

$ cd /usr/lib/

$ sudo mv libpyside-python2.7.* ~/Envs/py27/lib/



# PySide

$ sudo mv libshiboken-python2.7.* ~/Envs/py27/lib/



# Qt4 libs

$ cd /Library/Frameworks/

$ cp -pR Qt* ~/Envs/py27/lib/

$ cp -pR phonon.framework ~/Envs/py27/lib/ # framework!



# Qt ,

$ sudo /Developer/Tools/uninstall-qt.py




So, almost everything is ready, there is a final polishing and checking.



In addition to the convenient way to create and activate environments, virtualenvwrapper provides hooks to various events - preactivate , postactivate , predeactivate , postdeactivate, and others ( full list ). We are now interested in the postactivate , in which we will add variables that will tell us where to look for libraries and different modules in our environment.

$ vi ~/Envs/py27/bin/postactivate

#

export DYLD_FRAMEWORK_PATH=~/Envs/py27/lib/

export DYLD_LIBRARY_PATH=~/Envs/py27/lib/




Quick check:

$ workon py27

(py27) $ python

Python 2.7.2 (default, Jul 21 2011, 01:27:20)

[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> import PySide

>>> PySide

<module 'PySide' from '/Users/smaant/Envs/py27/lib/python2.7/site-packages/PySide/__init__.pyc'>

>>>




If you did everything right - the result should be something like this. If you caught a bunch of mistakes, then ... options are possible :)



py2app and battle check



Now everything is ready to write a test application - qt_test.py :

#!/usr/bin/env python



import sys

from PySide.QtCore import *

from PySide.QtGui import *

from PySide.QtWebKit import *



app = QApplication(sys.argv)



web = QWebView()

web.load(QUrl("http://www.pyside.org/"))

web.show()



sys.exit(app.exec_())




Check

$ workon py27

(py27) $ python qt_test.py


image



py2app



It was the turn of py2app , which will help build our application into a full bundle. Among other things, the virtualenv developers carefully placed another pip package manager in our virtual environment, with which we will install py2app :

$ workon py27

(py27) $ pip install py2app




To generate the bundle, py2app uses a special file - setup.py . How to create it from scratch is well told in the dock , and we will use it already ready:

 from setuptools import setup APP = ['qt_test.py'] OPTIONS = {'argv_emulation': False, 'includes' : ('PySide.QtNetwork', ), 'semi_standalone': 'False', 'compressed' : 'True', 'frameworks' : ('libpyside-python2.7.1.0.dylib', 'libshiboken-python2.7.1.0.dylib'), } setup( app=APP, options={'py2app': OPTIONS}, setup_requires=['py2app'], ) 




An app-bundle will appear in the dist folder, which you can run without fail! )

The entire algorithm was tested on Mac OS X 10.6.8, and the resulting bundle was tested on the same, but clean system.



If you want to use PyQt , then the sequence of actions will be exactly the same, just put pyqt and in the source files all PySide are replaced with PyQt. You may also need to install SIP , transfer it to virtual environment in the same way and add it to includes in setup.py .



Links



Below is a complete list of the tools and libraries that I used, with their versions:



Two articles that helped me a lot:

How to make a standalone OS X application bundle from PyQt apps using py2app

Multiple Python Versions on OSX with Virtualenv and Macports

Source: https://habr.com/ru/post/124573/



All Articles