- Getting Started
- Installing Pants
- Setting Up Pants
- Tutorial
- Common Tasks
- Pants for Organizations
- Pants Basics
- Why Use Pants?
- Pants Concepts
- BUILD files
- Target Addresses
- Third-Party Dependencies
- Pants Options
- Invoking Pants
- Reporting Server
- IDE Support
- JVM Support
- Python Support
- Go support for Pants
- Node.js Support
- Code & Doc Generation
- Thrift
- Python gRPC + protobufs
- Markdown
- Getting Help
- Troubleshooting
- Community
- Reference
- Pants BUILD Dictionary
- Pants Reference
- Release Notes
- Developer
- Pants Developer Center
- Export Format
- Architecture
- Blogs
- Twitter's Coursier Migration
Build a Python Executable (PEX)
Problem
You need to create an executable .pex Python binary (aka a "PEX") out of Python source code.
For more on PEX files see: https://github.com/pantsbuild/pex
If you need to create a Python library target instead, see Define a Python Library Target.
Solution
Define a python_binary target that you can build as a PEX using the binary goal:
$ ./pants binary src/python/myproject/example:my-python-binary
Discussion
In a python_ binary target, you should specify:
- A
namefor the PEX - A
sourcePython file that contains amainfunction - A list of
dependencies(optional). This list should include only Python library targets within the same project, not third-party dependencies. Any third-party dependencies should be specified in library targets.
Note: As an alternative to specifying a source file, you can define an entry_point function in your python_binary target. For example, specifying entry_point='main:run' would mean that the main function for the binary is the run() function contained in main.py.
Here's an example python_binary target:
python_library( name='myproject-lib', # Other parameters ) python_binary( name='myproject-bin', source='main.py', dependencies=[ ':myproject-lib', ], )
See Also
Generated by publish_docs
from dist/markdown/html/src/docs/common_tasks/pex.html 2022-12-03T01:09:00.288440
