### Setup Virtual Environment and Install Tools Source: https://pyinstaller.org/en/stable/_sources/development/testing.rst.txt Create a virtual environment and install the necessary tools for running the test suite. ```bash pip install --user virtualenv virtualenv /tmp/venv . /tmp/venv/bin/activate pip install -r tests/requirements-tools.txt ``` -------------------------------- ### Example: Get Distribution Info for 'pkg_resources' Source: https://pyinstaller.org/en/stable/hooks.html This example shows how to use `package_distribution` to find the distribution information for the 'pkg_resources' package, illustrating that it belongs to the 'setuptools' distribution. ```python >>> package_distribution("pkg_resources") Distribution(name="setuptools", packages=['easy_install', 'pkg_resources', 'setuptools']) ``` -------------------------------- ### Multiprocessing Spawn Example (POSIX Resource Tracker) Source: https://pyinstaller.org/en/stable/_sources/common-issues-and-pitfalls.rst.txt Demonstrates the command-line arguments for starting a resource tracker process on POSIX systems when using the multiprocessing module's spawn start method. This is important for understanding how PyInstaller handles such processes. ```text python -c from multiprocessing.resource_tracker import main;main(5) ``` -------------------------------- ### Multiprocessing Spawn Example (POSIX Worker) Source: https://pyinstaller.org/en/stable/_sources/common-issues-and-pitfalls.rst.txt Illustrates the command-line arguments for starting a worker process on POSIX systems using the multiprocessing module's spawn start method. This helps in debugging issues related to process spawning in frozen applications. ```text python -c "from multiprocessing.spawn import spawn_main; spawn_main(tracker_fd=6, pipe_handle=8)" --multiprocessing-fork ``` -------------------------------- ### Install PyInstaller from source archive Source: https://pyinstaller.org/en/stable/_sources/installation.rst.txt Install PyInstaller by unpacking a source archive and running pip install from the directory. ```bash pip install . ``` -------------------------------- ### Install Documentation Dependencies and Build HTML Source: https://pyinstaller.org/en/stable/_sources/development/documentation.rst.txt Install necessary packages for documentation building and then generate the HTML documentation. This is a prerequisite for verifying local changes. ```bash pip install -r doc/requirements.txt cd doc make html ``` -------------------------------- ### Install PyInstaller from Git clone Source: https://pyinstaller.org/en/stable/_sources/installation.rst.txt Install PyInstaller after cloning the repository and navigating into the directory. ```bash git clone https://github.com/pyinstaller/pyinstaller cd pyinstaller pip install . ``` -------------------------------- ### Install Build Tools (Fedora/RedHat) Source: https://pyinstaller.org/en/stable/_sources/bootloader-building.rst.txt Installs necessary development tools and zlib development libraries for building the bootloader on Fedora, RedHat, and derivative distributions. ```shell sudo yum groupinstall "Development Tools" sudo yum install zlib-devel ``` -------------------------------- ### Install Testing Tools Source: https://pyinstaller.org/en/stable/_sources/development/quickstart.rst.txt Install the required tools for testing the PyInstaller project by using pip to install from the provided requirements file. ```bash pip install -r tests/requirements-tools.txt ``` -------------------------------- ### Install Libraries and Run Full Test Suite Source: https://pyinstaller.org/en/stable/development/testing.html Install additional Python packages for better test coverage, including hooks, and then run the full test suite. ```bash pip install -U -r tests/requirements-libraries.txt pytest tests/unit tests/functional ``` -------------------------------- ### Install Testing Tools Source: https://pyinstaller.org/en/stable/development/quickstart.html Install the required Python packages for testing PyInstaller. Ensure you have pip installed. ```bash pip install -r tests/requirements-tools.txt ``` -------------------------------- ### Start Vagrant VM for Bootloader Build Source: https://pyinstaller.org/en/stable/_sources/bootloader-building.rst.txt Starts a specified Vagrant virtual machine (e.g., 'linux64') which will automatically build the bootloader. Use 'vagrant halt' to stop it. ```bash vagrant up linux64 # will also build the bootloader ``` ```bash vagrant halt linux64 # or `destroy` ``` -------------------------------- ### Install Build Tools (Debian/Ubuntu) Source: https://pyinstaller.org/en/stable/_sources/bootloader-building.rst.txt Installs essential build tools and zlib development libraries required for building the bootloader on Debian- or Ubuntu-based systems. ```shell sudo apt-get install build-essential zlib1g-dev ``` -------------------------------- ### Install Development Tools on Fedora/RedHat Source: https://pyinstaller.org/en/stable/bootloader-building.html Installs the 'Development Tools' group and zlib development libraries required for building the bootloader on Fedora, RedHat, and derivative systems. ```bash sudo yum groupinstall "Development Tools" sudo yum install zlib-devel ``` -------------------------------- ### Install PyInstaller using pip Source: https://pyinstaller.org/en/stable/_sources/index.rst.txt Install or upgrade PyInstaller using pip. Ensure you have the necessary requirements installed beforehand. ```bash pip install -U pyinstaller ``` -------------------------------- ### Install PyInstaller Source: https://pyinstaller.org/en/stable/index.html Install or upgrade PyInstaller using pip. This command should be run in a command prompt or shell. ```bash pip install -U pyinstaller ``` -------------------------------- ### Install Development Tools on Debian/Ubuntu Source: https://pyinstaller.org/en/stable/bootloader-building.html Installs essential build tools and zlib development libraries required for building the bootloader on Debian-based systems. ```bash sudo apt-get install build-essential zlib1g-dev ``` -------------------------------- ### Install PyInstaller from GitHub repository Source: https://pyinstaller.org/en/stable/_sources/installation.rst.txt Install PyInstaller directly from its GitHub repository using pip's git checkout support. ```bash pip install git+https://github.com/pyinstaller/pyinstaller ``` -------------------------------- ### Install PyInstaller using pip Source: https://pyinstaller.org/en/stable/_sources/installation.rst.txt Use this command to install the latest stable version of PyInstaller from PyPI. ```bash pip install pyinstaller ``` -------------------------------- ### Basic Hook Configuration Example Source: https://pyinstaller.org/en/stable/hooks-config.html Demonstrates how to pass configuration options to hooks using the 'hooksconfig' argument in the Analysis object within a .spec file. This example shows configuration for 'some_hook_id' and 'another_hook_id'. ```python a = Analysis( ["program.py"], ..., hooksconfig={ "some_hook_id": { "foo": ["entry1", "entry2"], "bar": 42, "enable_x": True, }, "another_hook_id": { "baz": "value", }, }, ..., ) ``` -------------------------------- ### Force Bootloader Rebuild on Windows (Command Prompt) Source: https://pyinstaller.org/en/stable/bootloader-building.html Set environment variables to force bootloader rebuild and install PyInstaller from source. Ensure previous installations and cache are cleared. ```batch set PYINSTALLER_COMPILE_BOOTLOADER=1 set "PYINSTALLER_BOOTLOADER_WAF_ARGS=--example-option=example-value --example-flag" python -m pip uninstall PyInstaller python -m pip cache remove PyInstaller python -m pip install --verbose --no-binary=PyInstaller PyInstaller ``` -------------------------------- ### Install Vagrant Plugins Source: https://pyinstaller.org/en/stable/_sources/bootloader-building.rst.txt Installs necessary Vagrant plugins for managing virtual machines, specifically 'vagrant-reload' and 'vagrant-scp'. These are often required for PyInstaller's cross-building setup. ```bash vagrant plugin install vagrant-reload vagrant-scp ``` -------------------------------- ### Get scripts directory path on Windows Source: https://pyinstaller.org/en/stable/_sources/installation.rst.txt Python code to determine the directory where scripts are installed on Windows. ```python import sysconfig; print(sysconfig.get_path("scripts")) import site; print(site.USER_BASE + "\\Scripts") ``` -------------------------------- ### Define PyInstaller Hook Entry Points Source: https://pyinstaller.org/en/stable/_sources/hooks.rst.txt Example of setuptools entry points in setup.cfg for registering hook directories and tests with PyInstaller. ```ini [options.entry_points] pyinstaller40 = hook-dirs = pyi_hooksample.__pyinstaller:get_hook_dirs tests = pyi_hooksample.__pyinstaller:get_PyInstaller_tests ``` -------------------------------- ### Get scripts directory path on UNIX Source: https://pyinstaller.org/en/stable/_sources/installation.rst.txt Python code to determine the directory where scripts are installed on UNIX-like systems. ```python import sysconfig; print(sysconfig.get_path("scripts")) import site; print(site.USER_BASE + "/bin") ``` -------------------------------- ### Python Console Signal Handler Example Source: https://pyinstaller.org/en/stable/_sources/feature-notes.rst.txt This example demonstrates how to install a console control handler in a Python application to manage signals like Ctrl+C. It saves the application's state to a file before exiting and restores it on the next run. The handler is designed to return True, causing immediate termination after its execution, but includes a sleep to allow for cleanup. ```python # console_counter.py import sys import time import pathlib import win32api # pip install pywin32 def console_handler(signal): print(f"Console handler (signal {signal})!") global keep_running keep_running = False # Sleep until process either finishes or is killed by the OS time.sleep(20) return True if __name__ == '__main__': keep_running = True # Install console handler win32api.SetConsoleCtrlHandler(console_handler, 1) # Restore state, if available state_file = pathlib.Path.home() / 'counter_state.txt' if state_file.is_file(): print(f"Restoring state from {state_file}...", file=sys.stderr) try: with open(state_file, 'r') as fp: counter = int(fp.readline()) except Exception: print("Failed to restore state from file!", file=sys.stderr) counter = 0 else: print("State file does not exist!", file=sys.stderr) counter = 0 print(f"Initial counter value: {counter}", file=sys.stderr) # Main loop while keep_running: print(f"Counter value: {counter}") counter += 1 time.sleep(1) # Clean-up print(f"Storing state to {state_file}...", file=sys.stderr) try: with open(state_file, 'w') as fp: print(f"{counter}", file=fp) except Exception: print(f"Failed to store state to {state_file}!", file=sys.stderr) print("Goodbye!") time.sleep(1) # Delay exit for another second ``` -------------------------------- ### Basic Hook Configuration Example Source: https://pyinstaller.org/en/stable/_sources/hooks-config.rst.txt Demonstrates how to pass a dictionary of hook configuration options to the Analysis object. Keys are hook identifiers, and values are dictionaries of hook-specific settings. ```python a = Analysis( ["program.py"], ..., hooksconfig={ "some_hook_id": { "foo": ["entry1", "entry2"], "bar": 42, "enable_x": True, }, "another_hook_id": { "baz": "value", }, }, ..., ) ``` -------------------------------- ### Get Distribution Info by Distribution Name Source: https://pyinstaller.org/en/stable/hooks.html Use `distribution(name)` to retrieve information about a Conda distribution using its installation name. This function returns a `Distribution` object containing details like name, version, files, dependencies, and packages. ```python from PyInstaller.utils.hooks import conda_support dist_info = conda_support.distribution("numpy") ``` -------------------------------- ### Troubleshoot GUI Startup on Windows Source: https://pyinstaller.org/en/stable/when-things-go-wrong.html For Windows GUI applications that fail to start, re-bundle without the --windowed option and run the executable from the command line. This will display any error messages preventing initialization. ```bash my_gui.exe ``` -------------------------------- ### Enable All Debug Messages Source: https://pyinstaller.org/en/stable/when-things-go-wrong.html Use the --debug=all option to get extensive diagnostic information during development or when an app fails to start. Debug messages normally go to standard output, but are sent to an attached debugger when --windowed is used on Windows. ```bash pyinstaller --debug=all your_script.py ``` -------------------------------- ### Bundle DLLs from System32 Source: https://pyinstaller.org/en/stable/_sources/hooks.rst.txt Use the 'binaries' option to specify files or directories to bundle. This example bundles all DLLs from C:\Windows\System32 into a 'dlls' directory within the bundle. ```python binaries = [ ('C:\\Windows\\System32\\*.dll', 'dlls') ] ``` -------------------------------- ### Windows DLL Loading Example Source: https://pyinstaller.org/en/stable/_sources/common-issues-and-pitfalls.rst.txt Illustrates how Windows loader resolves DLLs using the SetDllDirectoryW API. PyInstaller's bootloader ensures the library search path includes the application directory. ```text SetDllDirectoryW ``` -------------------------------- ### Install Formatting and Linting Tools Source: https://pyinstaller.org/en/stable/development/coding-conventions.html Installs flake8 for PEP 8 validation and yapf for automatic code formatting. Ensure yapf is version 0.32.0 and toml is installed for configuration. ```bash pip install flake8 yapf==0.32.0 toml ``` -------------------------------- ### Verify PyInstaller installation Source: https://pyinstaller.org/en/stable/_sources/installation.rst.txt Check if PyInstaller is installed correctly and display its version. ```bash pyinstaller --version ``` -------------------------------- ### Troubleshoot GUI Startup on macOS Source: https://pyinstaller.org/en/stable/when-things-go-wrong.html If a GUI application fails to start on macOS, run it from the command line in Terminal instead of clicking the .app bundle. This allows you to see error messages that would otherwise be hidden. ```bash ./dist/my_gui ``` -------------------------------- ### Install PyInstaller development version Source: https://pyinstaller.org/en/stable/_sources/installation.rst.txt Install the current development version of PyInstaller directly from a tarball. ```bash pip install https://github.com/pyinstaller/pyinstaller/tarball/develop ``` -------------------------------- ### Build Bootloader with Waf Source: https://pyinstaller.org/en/stable/_sources/bootloader-building.rst.txt Build the bootloader from source using the waf build tool. This command compiles the bootloader for the current platform. ```bash python ./waf all ``` -------------------------------- ### Manual macOS Bootloader Build Setup Source: https://pyinstaller.org/en/stable/bootloader-building.html Manually set up the environment for building macOS bootloaders if not using the Vagrant build-guest. This involves extracting the osxcross toolchain and setting the PATH. ```shell mkdir -p ~/osxcross tar -C ~/osxcross --xz -xf /vagrant/sdks/osx/osxcross.tar.xz PATH=~/osxcross/bin/:$PATH ``` -------------------------------- ### Install PyInstaller from specific GitHub branch Source: https://pyinstaller.org/en/stable/_sources/installation.rst.txt Install PyInstaller from a specific branch (e.g., 'develop') in the GitHub repository. ```bash pip install git+https://github.com/pyinstaller/pyinstaller@develop ``` -------------------------------- ### Install Libraries for Better Coverage Source: https://pyinstaller.org/en/stable/_sources/development/testing.rst.txt Install additional Python packages to improve test coverage, including hooks. ```bash pip install -U -r tests/requirements-libraries.txt ``` -------------------------------- ### Build Bootloader for Specific Architecture Source: https://pyinstaller.org/en/stable/bootloader-building.html Use this command to build the bootloader for a specific architecture, such as 32-bit, on a 64-bit machine. Ensure the appropriate cross-compilation toolchain and development files are installed. ```bash python ./waf all --target-arch=32bit ``` -------------------------------- ### Install Python with Shared Libpython Source: https://pyinstaller.org/en/stable/development/venv.html Install a Python version with shared libpython enabled, which is required for PyInstaller to function correctly. ```bash env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.5.0 ``` -------------------------------- ### Install PyInstaller in editable mode Source: https://pyinstaller.org/en/stable/_sources/installation.rst.txt Install PyInstaller in editable mode to apply source code changes immediately without re-installation. ```bash pip install -e . ``` -------------------------------- ### Multiprocessing Spawn Example (Windows) Source: https://pyinstaller.org/en/stable/_sources/common-issues-and-pitfalls.rst.txt Shows the command-line arguments used by Python to spawn a worker process on Windows when using the multiprocessing module. This is relevant when PyInstaller packages an application that uses multiprocessing. ```text python.exe -c "from multiprocessing.spawn import spawn_main; spawn_main(parent_pid=6872, pipe_handle=520)" --multiprocessing-fork ```