[tutorial] PEX Unpacked: Building and Executing Python Executables with Ease
What is “PEX”?
The full name of PEX is Python Executable. This is an open-source tool for building a virtual environment to execute your Python code. However, be cautious when using PEX because it does not include a Python interpreter; thus, your computer must have a Python environment installed.
The process of executing .pex
When you run a .pex file, the system reads the shebang line at the top of the file, #!/usr/bin/env python, to invoke the Python interpreter to execute the script.
Practice
Install PEX
1
pip install pex
Enter Interpretable pex environemt
Easy way
1 2 3 4 5 6 7 8 9 10 11 12
# Enter an interpretable Pex environment without specifying a Python version pex
# Enter an interpretable Pex environment specifying a specific Python version pex --python=python3.12# specific python version
--- Pex 2.16.1 ephemeral hermetic environment with no dependencies. Exit the repl (type quit()) and run `pex -h` for Pex CLI help. Python 3.11.4 (main, Jul 52023, 08:40:20) [Clang 14.0.6 ] on darwin Type"help", "pex", "copyright", "credits"or"license"for more information. >>>
Advanced
1 2 3 4 5 6 7 8
# Specifying requirements pex pandas
--- Pex 2.16.1 ephemeral hermetic environment with1 requirement and6 activated distributions. Python 3.11.4 (main, Jul 52023, 08:40:20) [Clang 14.0.6 ] on darwin Type"help", "pex", "copyright", "credits"or"license"for more information. >>> import pandas as pd
Package .py into pex file
1 2 3 4 5 6 7 8 9
# demo.py
import pandas as pd
defmain(): print(f"Hello, World! The pandas version is {pd.__version__}")