py2app Documentation

repository·main·Indexed 19 days ago

https://github.com/ronaldoussoren/py2app

A tool for creating standalone macOS application bundles or plugin bundles from Python GUI applications, allowing distribution to users without Python installed. Supports macOS 10.9+, Python 3.7+, and x86_64/arm64 architectures. Features include configuration via pyproject.toml or a legacy setuptools interface, support for Dark Mode, and utilities for handling C extensions, Cython, and SSL certificate validation via truststore.

Tokens
18.4K
Snippets
57
Records
77
Agent score
60%

What's inside py2app

  1. Overview of py2app

    main
    py2app is a tool used to package Python GUI applications as standalone macOS application bundles or plugin bundles. This process allows you to distribute your Python applications to users who do not have Python installed on their macOS systems.
  2. Configure application arguments and environment

    main

    Argument Emulation

    Use --argv-emulation to intercept file-open and url-open events, adding the target resources to sys.argv.

    WARNING: Do not use this with GUI toolkits, as the emulator can cause conflicts. Most GUI toolkits have their own APIs for handling these events.

    To inject specific values into sys.argv immediately after argv[0], use --argv-inject <values>. The values are split using shlex.split.

    Environment and Paths

    • --emulate-shell-environment: Use this if your application needs to access environment variables typically set in a user's shell profile (e.g., .bash_profile). By default, applications launched via Finder do not inherit these variables.
    • --use-pythonpath: Allows the PYTHONPATH environment variable to affect the interpreter's search path. Note that this is generally not useful for standalone bundles as PYTHONPATH is not included in the minimal shell environment used by the application launcher.
    • --site-packages: Includes system and user site-packages in sys.path. This makes the bundle less standalone as it becomes dependent on packages installed on the user's system.
  3. Access the application's Resources folder via RESOURCEPATH

    main
    When running a bundled application, py2app provides the RESOURCEPATH environment variable. This variable contains the filesystem path to the 'Resources' folder located inside the application bundle. Use this variable to locate assets, data files, or other resources packaged with your app.
  4. Develop using alias mode

    main

    Alias mode (-A or --alias) is designed for rapid development. Instead of creating a standalone, portable application, it builds an application bundle that uses your source and data files in-place.

    Key benefits:

    • Changes to your source code do not require rebuilding the application.
    • It is similar to setuptools develop mode or Xcode's zero-link feature.

    Limitations:

    • The resulting application is not portable and cannot be moved to other machines.

    To use alias mode, run setup.py with the py2app command and the -A flag. The application bundle will be located in the dist/ folder.

    $ python setup.py py2app -A
  5. What are py2app recipes and why are they used?

    main

    Recipes are a mechanism in py2app used to work around package incompatibilities and automatically strip unwanted dependencies. They are necessary for packages that:

    1. Use __import__ or other methods to import code without using the standard import statement.
    2. Require specific in-package data files to function.

    Currently, py2app searches for recipes only within the py2app.recipes module.

  6. Create a setup.py file for your application

    main

    To convert a Python script into a macOS application, you must first create a setup.py file. This file acts as the project configuration for setuptools. You can generate this file automatically using the py2applet command. If your application requires specific icon files (.icns) or additional data files, include them as arguments to the py2applet command.

    $ py2applet --make-setup MyApplication.py
  7. Create a basic py2app build script

    main

    To build a simple py2app application, create a setup.py file that uses setuptools.setup with the app parameter pointing to your main script and setup_requires set to ['py2app']. Run the build using python setup.py py2app.

    """
    py2app build script for MyApplication
    
    Usage:
        python setup.py py2app
    """
    from setuptools import setup
    setup(
        app=["MyApplication.py"],
        setup_requires=["py2app"],
    )
  8. Configure py2app using the legacy setuptools interface

    main

    Note: Using the setuptools.setup() command interface is deprecated as of version 2.0. However, it is still supported for legacy configurations.

    To use this method, create a setup.py script that calls setuptools.setup() with app or plugin arguments and a py2app configuration dictionary inside the options argument.

    from setuptools import setup
    
    setup(
        app=...
        options={
            "py2app": {
                ...
            }
        }
    )
  9. Build a redistributable application for deployment

    main

    Once development is complete, you can build a standalone, redistributable version of your application.

    Steps:

    1. Clean your environment: rm -rf build dist.
    2. Run the py2app command without the alias flag: python setup.py py2app.
    3. The self-contained bundle will be created in dist/MyApplication.app.

    Note: Unlike alias mode, any changes to source code, data files, or options require you to run the py2app command again to rebuild the bundle.

    To package the final application for distribution, you can right-click the .app bundle in Finder and select "Create Archive".

    $ python setup.py py2app