### Install Setuptools Source: https://setuptools.pypa.io/en/latest/userguide/quickstart.html Install or upgrade setuptools to the latest version using pip. The '[core]' extra installs essential components. ```bash pip install --upgrade setuptools[core] ``` -------------------------------- ### Platform and Version Specific Dependencies in setup.py Source: https://setuptools.pypa.io/en/latest/userguide/dependency_management.html Declare platform-specific dependencies with version requirements using environment markers in setup.py. This example installs 'pywin32' version 1.0 or higher only on Windows. ```python setup( ..., install_requires=[ "enum34;python_version<'3.4'", "pywin32 >= 1.0;platform_system=='Windows'", ], ) ``` -------------------------------- ### Example setup.cfg for Declarative Configuration Source: https://setuptools.pypa.io/en/latest/userguide/declarative_config.html This snippet shows a comprehensive example of a `setup.cfg` file, demonstrating how to define package metadata, options, package data, entry points, and extras_require. ```ini [metadata] name = my_package version = attr: my_package.VERSION author = Josiah Carberry author_email = josiah_carberry@brown.edu description = My package description long_description = file: README.rst, CHANGELOG.rst, LICENSE.rst keywords = one, two license = BSD-3-Clause classifiers = Framework :: Django Programming Language :: Python :: 3 [options] zip_safe = False include_package_data = True packages = find: python_requires = >=3.8 install_requires = requests importlib-metadata; python_version<"3.10" [options.package_data] * = *.txt, *.rst hello = *.msg [options.entry_points] console_scripts = executable-name = my_package.module:function [options.extras_require] pdf = ReportLab>=1.2; RXP rest = docutils>=0.3; pack ==1.1, ==1.3 [options.packages.find] exclude = examples* tools* docs* my_package.tests* ``` -------------------------------- ### Example pyproject.toml for setuptools Source: https://setuptools.pypa.io/en/latest/userguide/pyproject_config.html This example demonstrates a comprehensive pyproject.toml file, specifying build system requirements, core project metadata, optional dependencies, and script entry points. ```toml [build-system] requires = ["setuptools", "setuptools-scm"] build-backend = "setuptools.build_meta" [project] name = "my_package" authors = [ {name = "Josiah Carberry", email = "josiah_carberry@brown.edu"}, ] description = "My package description" readme = "README.rst" requires-python = ">=3.8" keywords = ["one", "two"] license = "BSD-3-Clause" classifiers = [ "Framework :: Django", "Programming Language :: Python :: 3", ] dependencies = [ "requests", 'importlib-metadata; python_version<"3.10"', ] dynamic = ["version"] [project.optional-dependencies] pdf = ["ReportLab>=1.2", "RXP"] rest = ["docutils>=0.3", "pack ==1.1, ==1.3"] [project.scripts] my-script = "my_package.module:function" # ... other project metadata fields as listed in: # https://packaging.python.org/en/latest/guides/writing-pyproject-toml/ ``` -------------------------------- ### Project Structure Example Source: https://setuptools.pypa.io/en/latest/build_meta.html Illustrates the basic file and directory structure for a package intended for distribution using setuptools. ```text ~/meowpkg/ pyproject.toml setup.cfg meowpkg/ __init__.py module.py ``` -------------------------------- ### Platform and Version Specific Dependencies in setup.cfg Source: https://setuptools.pypa.io/en/latest/userguide/dependency_management.html Declare platform-specific dependencies with version requirements using environment markers in setup.cfg. This example installs 'pywin32' version 1.0 or higher only on Windows. ```ini [options] #... install_requires = enum34;python_version<'3.4' pywin32 >= 1.0;platform_system=='Windows' ``` -------------------------------- ### Configuring Package Data in setup.py Source: https://setuptools.pypa.io/en/latest/userguide/datafiles.html Set package data using the 'package_data' argument in the setup() function. This example includes *.txt for 'mypkg' and *.rst for 'mypkg.data'. ```python from setuptools import setup, find_namespace_packages setup( # ..., packages=find_namespace_packages(where="src"), package_dir={"": "src"}, package_data={ "mypkg": ["*.txt"], "mypkg.data": ["*.rst"], } ) ``` -------------------------------- ### Bootstrap Setuptools Installation Source: https://setuptools.pypa.io/en/latest/history.html Include ez_setup.py in your package and add this to setup.py to automatically bootstrap setuptools installation. ```python from ez_setup import use_setuptools use_setuptools() from setuptools import setup # etc... ``` -------------------------------- ### Install with Legacy Editable Mode Source: https://setuptools.pypa.io/en/latest/userguide/development_mode.html Use this command to perform an editable installation that emulates the legacy `python setup.py develop` behavior. This mode is transitional and will be removed in future versions. ```bash pip install -e . --config-settings editable_mode=compat ``` -------------------------------- ### Define Setup Keywords Entry Point Source: https://setuptools.pypa.io/en/latest/userguide/extension.html Configure entry points in setup.cfg to add custom arguments to the setup() function. The 'distutils.setup_keywords' group specifies a function to validate the argument if it's supplied. ```ini # setup.cfg ... [options.entry_points] distutils.commands = foo = mypackage.some_module:foo distutils.setup_keywords = bar_baz = mypackage.some_module:validate_bar_baz ``` -------------------------------- ### Install Source Distribution Source: https://setuptools.pypa.io/en/latest/build_meta.html Shows how to install a built source distribution archive (.tar.gz) using pip. ```bash $ pip install dist/meowpkg-0.0.1.tar.gz ``` -------------------------------- ### Project Structure Example Source: https://setuptools.pypa.io/en/latest/userguide/ext_modules.html Illustrates a basic project folder structure for a project that includes a C extension module. ```text ├── pyproject.toml └── foo.c ``` -------------------------------- ### Install Wheel Distribution Source: https://setuptools.pypa.io/en/latest/build_meta.html Shows how to install a built wheel distribution file using pip. ```bash $ pip install dist/meowpkg-0.0.1.whl ``` -------------------------------- ### Project Metadata in setup.cfg Source: https://setuptools.pypa.io/en/latest/userguide/quickstart.html An alternative way to define project metadata using the `setup.cfg` file. It includes metadata and options for installation. ```ini [metadata] name = mypackage version = 0.0.1 [options] install_requires = requests importlib-metadata; python_version<"3.10" ``` -------------------------------- ### Minimal setup.py for Editable Installs Source: https://setuptools.pypa.io/en/latest/references/keywords.html When using declarative configs with setuptools<64.0.0, a minimal setup.py is needed to ensure editable installs are supported. Versions of setuptools >=64.0.0 do not require this. ```python from setuptools import setup setup() ``` -------------------------------- ### Install Project in Development Mode Source: https://setuptools.pypa.io/en/latest/userguide/development_mode.html Use this command to perform an editable installation of your project. Ensure you are in your project's root directory and have activated your virtual environment. ```bash cd your-python-project python -m venv .venv # Activate your environment with: # `source .venv/bin/activate` on Unix/macOS # or `.venv\Scripts\activate` on Windows pip install --editable . ``` -------------------------------- ### MANIFEST.in Example Source: https://setuptools.pypa.io/en/latest/userguide/datafiles.html Specifies which data files to include in the source distribution using MANIFEST.in. ```text include src/mypkg/*.txt include src/mypkg/*.rst ``` -------------------------------- ### Defining Entry Points for Custom Commands Source: https://setuptools.pypa.io/en/latest/userguide/extension.html This snippet shows how to define custom commands as entry points in setup.cfg. These commands become available after installation. ```ini [options.entry_points] distutils.commands = foo = mypackage.some_module:foo ``` -------------------------------- ### Declaring Optional Dependencies in setup.cfg Source: https://setuptools.pypa.io/en/latest/userguide/dependency_management.html Use 'install_requires' in setup.cfg to specify core dependencies, including optional extras like '[PDF]'. This ensures that when 'Package-A[PDF]' is installed, ReportLab is also installed if needed. ```ini [metadata] name = Package-B #... [options] #... install_requires = Package-A[PDF] ``` -------------------------------- ### Project Structure Example Source: https://setuptools.pypa.io/en/latest/userguide/datafiles.html Illustrates a typical project directory structure for including data files within a package. ```text project_root_directory ├── setup.py # and/or setup.cfg, pyproject.toml └── src └── mypkg ├── __init__.py ├── data1.rst ├── data2.rst ├── data1.txt └── data2.txt ``` -------------------------------- ### Optional Dependencies in setup.cfg Source: https://setuptools.pypa.io/en/latest/userguide/dependency_management.html Declare optional dependencies for a package using the 'extras_require' option in setup.cfg. This example defines 'PDF' as an optional dependency. ```ini [metadata] name = Package-A [options.extras_require] PDF = ReportLab>=1.2 RXP ``` -------------------------------- ### Optional Dependencies in setup.py Source: https://setuptools.pypa.io/en/latest/userguide/dependency_management.html Declare optional dependencies for a package using the 'extras_require' dictionary in setup.py. This example defines 'PDF' as an optional dependency. ```python setup( name="Package-A", ..., extras_require={ "PDF": ["ReportLab>=1.2", "RXP"], }, ) ``` -------------------------------- ### Declaring Optional Dependencies in setup.py Source: https://setuptools.pypa.io/en/latest/userguide/dependency_management.html In setup.py, the 'install_requires' argument within the setup function is used to declare dependencies, including optional extras. This example shows how 'Package-A[PDF]' ensures necessary downstream packages are installed. ```python setup( name="Package-B", install_requires=["Package-A[PDF]"], ..., ) ``` -------------------------------- ### Custom Setup Argument Validation Function Source: https://setuptools.pypa.io/en/latest/userguide/extension.html Example of a validation function for a custom setup() argument. It accepts the Distribution object, attribute name, and value, raising SetupError for invalid boolean values. ```python def assert_bool(dist, attr, value): """Verify that value is True, False, 0, or 1""" if bool(value) != value: raise SetupError( f"{attr!r} must be a boolean value (got {value!r}" ) ``` -------------------------------- ### Define Entry Point in setup.py Source: https://setuptools.pypa.io/en/latest/userguide/entry_point.html Configure an entry point named 'excl' under the 'timmins.display' group in setup.py. ```python from setuptools import setup setup( # ..., entry_points = { 'timmins.display': [ 'excl = timmins_plugin_fancy:excl_display' ] } ) ``` -------------------------------- ### Platform Specific Dependencies in setup.py Source: https://setuptools.pypa.io/en/latest/userguide/dependency_management.html Declare platform-specific dependencies using environment markers in setup.py. This example installs 'enum34' only for Python versions older than 3.4. ```python setup( ..., install_requires=[ "enum34;python_version<'3.4'", ], ) ``` -------------------------------- ### GUI Script Entry Point in setup.cfg Source: https://setuptools.pypa.io/en/latest/userguide/entry_point.html Sets up a GUI script 'hello-world' to execute the 'hello_world' function in the 'timmins' package. ```INI [options.entry_points] gui_scripts = hello-world = timmins:hello_world ``` -------------------------------- ### Platform Specific Dependencies in setup.cfg Source: https://setuptools.pypa.io/en/latest/userguide/dependency_management.html Declare platform-specific dependencies using environment markers in setup.cfg. This example installs 'enum34' only for Python versions older than 3.4. ```ini [options] #... install_requires = enum34;python_version<'3.4' ``` -------------------------------- ### Platform Specific Dependencies in pyproject.toml Source: https://setuptools.pypa.io/en/latest/userguide/dependency_management.html Declare platform-specific dependencies using environment markers in pyproject.toml. This example installs 'enum34' only for Python versions older than 3.4. ```toml [project] # ... dependencies = [ "enum34; python_version<'3.4'", ] # ... ``` -------------------------------- ### Define EGG-INFO File Writer Extension Source: https://setuptools.pypa.io/en/latest/userguide/extension.html Example of an extension defining a setup argument 'foo_bar' to write custom data to a 'foo_bar.txt' file in the EGG-INFO directory. ```python # Example of an extension defining a setup argument `foo_bar`, # which is a list of lines that will be written to `foo_bar.txt` # in the EGG-INFO directory of any project that uses the argument: ``` -------------------------------- ### Define Entry Point in setup.cfg Source: https://setuptools.pypa.io/en/latest/userguide/entry_point.html Configure an entry point named 'excl' under the 'timmins.display' group in setup.cfg. ```ini [options.entry_points] timmins.display = excl = timmins_plugin_fancy:excl_display ``` -------------------------------- ### Define Multiple Entry Points in setup.py Source: https://setuptools.pypa.io/en/latest/userguide/entry_point.html Configure multiple entry points, 'excl' and 'lined', under the 'timmins.display' group in setup.py. ```python from setuptools import setup setup( # ..., entry_points = { 'timmins.display': [ 'excl = timmins_plugin_fancy:excl_display', 'lined = timmins_plugin_fancy:lined_display', ] } ) ``` -------------------------------- ### GUI Script Entry Point in setup.py Source: https://setuptools.pypa.io/en/latest/userguide/entry_point.html Defines a GUI script entry point 'hello-world' which executes the 'timmins.hello_world' function. ```Python from setuptools import setup setup( # ..., entry_points={ 'gui_scripts': [ 'hello-world = timmins:hello_world', ] } ) ``` -------------------------------- ### Platform and Version Specific Dependencies in pyproject.toml Source: https://setuptools.pypa.io/en/latest/userguide/dependency_management.html Declare platform-specific dependencies with version requirements using environment markers in pyproject.toml. This example installs 'pywin32' version 1.0 or higher only on Windows. ```toml [project] # ... dependencies = [ "enum34; python_version<'3.4'", "pywin32 >= 1.0; platform_system=='Windows'", ] # ... ``` -------------------------------- ### Build with Cython and setup_requires Source: https://setuptools.pypa.io/en/latest/history.html Example of configuring setuptools to build extensions using Cython, with Cython specified as a setup_requires dependency. This allows Cython to be downloaded on demand. ```python ext = setuptools.Extension('mylib', ['src/CythonStuff.pyx', 'src/CStuff.c']) setuptools.setup( ... ext_modules=[ext], setup_requires=['cython'], ) ``` -------------------------------- ### Exclude Specific Data Files in setup.py Source: https://setuptools.pypa.io/en/latest/userguide/datafiles.html Define `exclude_package_data` directly in the `setup()` function call within `setup.py` to exclude files like `.gitattributes` from the installed package. This provides fine-grained control over package contents. ```python from setuptools import setup, find_packages setup( # ..., packages=find_packages(where="src"), package_dir={"" : "src"}, include_package_data=True, exclude_package_data={"mypkg": [".gitattributes"]}, ) ``` -------------------------------- ### Defining a Custom Command Entry Point Source: https://setuptools.pypa.io/en/latest/userguide/extension.html This example shows how to define an entry point for a custom command named 'foo'. The entry point specifies the module and attribute to import for the command handler. Your package must be installed for this entry point to be recognized. ```ini [metadata] name = my-package version = 0.1 [options] packages = find: [options.entry_points] distutils.commands = foo = my_commands:FooCommand ``` -------------------------------- ### Package Discovery with setup.py Source: https://setuptools.pypa.io/en/latest/userguide/quickstart.html Use `find_packages()` in `setup.py` to discover packages. Keyword arguments like `where`, `include`, and `exclude` can be used to customize discovery. ```python from setuptools import setup, find_packages setup( # ... packages=find_packages( # All keyword arguments below are optional: where='src', include=['mypackage*'], exclude=['mypackage.tests'], ), # ... ) ``` -------------------------------- ### Dependency Management (setup.cfg) Source: https://setuptools.pypa.io/en/latest/userguide/quickstart.html List project dependencies in `setup.cfg` using the `install_requires` option under the `[options]` section. ```ini [options] install_requires = docutils requests <= 0.4 ``` -------------------------------- ### Install venv Module on Debian/Ubuntu Source: https://setuptools.pypa.io/en/latest/userguide/development_mode.html If the 'venv' module is not installed by default with Python on Debian/Ubuntu systems, use this command to install it via the OS package manager. ```bash sudo apt install python3-venv ``` -------------------------------- ### Configure Package Discovery with `setup.cfg` Source: https://setuptools.pypa.io/en/latest/userguide/package_discovery.html Specify package discovery options in `setup.cfg` using `[options]` and `[options.packages.find]`. Use `where` to set the source directory and `include` or `exclude` with glob patterns to filter packages. ```ini [options] packages = find: package_dir = =src [options.packages.find] where = src include = pkg* # alternatively: `exclude = additional*` ``` -------------------------------- ### Configuring Package Data in setup.cfg Source: https://setuptools.pypa.io/en/latest/userguide/datafiles.html Define package data using the [options.package_data] section in setup.cfg. This example specifies *.txt for 'mypkg' and *.rst for 'mypkg.data'. ```ini [options.package_data] mypkg = *.txt mypkg.data = *.rst ``` -------------------------------- ### Install Python Package in Editable Mode Source: https://setuptools.pypa.io/en/latest/userguide/development_mode.html Use this command to install a package in editable mode, allowing for live code changes to be reflected without reinstallation. The '--config-settings editable_mode=strict' flag enables a stricter mode that mimics regular installations more closely. ```bash pip install -e . --config-settings editable_mode=strict ``` -------------------------------- ### Import Project in Development Mode Source: https://setuptools.pypa.io/en/latest/userguide/development_mode.html This command demonstrates how to import your Python project when it's installed in development mode, typically after an editable installation. ```python python -c "import your_python_project" ``` -------------------------------- ### Install Build Tool Source: https://setuptools.pypa.io/en/latest/userguide/quickstart.html Install the 'build' tool, which automates the download of setuptools and other build dependencies. This tool is recommended for creating new Python packages. ```bash pip install --upgrade build ``` -------------------------------- ### Entry Points for Script Creation (setup.py) Source: https://setuptools.pypa.io/en/latest/userguide/quickstart.html Specify entry points in `setup.py` using the `entry_points` argument to `setup()`. The `console_scripts` list defines command-line executables. ```python setup( # ... entry_points={ 'console_scripts': [ 'cli-name = mypkg.mymodule:some_func', ] } ) ``` -------------------------------- ### Install Package in Development Mode Source: https://setuptools.pypa.io/en/latest/userguide/quickstart.html Use this command to install a package in editable mode, allowing for immediate reflection of source code changes without reinstallation. ```bash pip install --editable . ```