Select a custom python environment/version in pex file

1.9k Views Asked by At

in my environment I have python 3.8.5 and pex 2.1.20 I'm bundling some libraries in a pex file with this command:

pex module1 module2 module3 -o custom_env.pex

The reason being that I want to transfer this pex file on another machine and then run a Python script with the command ./custom_env.pex python_script.py in order to provide it all the required dependencies.

The problem is that pex is currently using /usr/bin/env python3.8 while on that machine python 2.6.6 is running. Therefore when I run the pex file I receive the error

/usr/bin/env: python3.8: No such file or directory

How can I build the pex file so that it matches the destination python environment?

1

There are 1 best solutions below

0
Michael Seifert On

The comments to the above question have already warned about the incompatibility between Python 2 and Python 3.

The interpreter compatibility of the resulting PEX file can be controlled using the --python option of the pex command (see Invoking the PEX utility). For example, the command pex --python=python3.6 -o py36.pex will create a PEX file that can be run using Python 3.6. The Shebang of the result PEX file will be #!/usr/bin/env python3.6.

PEX is also capable of supporting multiple Python versions or even multiple platforms in a single PEX executable. You can achieve this by specifying the --python or --platform options to the PEX command multiple times with different values.

Note that PEX is using Pip to resolve the dependencies. When specifying the --python or --platform options, Pip requires that all your dependencies are available as binary wheels for the target platform and Python version. If any of the dependency is only available as a source tarball, Pip's dependency resolution will fail.