white-address-70112
06/10/2021, 7:31 AM[vagrant@localhost ~]$ ./my_webapp.pex
/usr/bin/env: python3.9: No such file or directory
[vagrant@localhost ~]$ python --version
Python 2.7.5
So how do i ship venv with the project as a single PEX file, so that this can be executed in any system we want to without any python version requirement?
Some expert opinion/advise is much appreciated!!
TIAenough-analyst-54434
06/10/2021, 2:57 PMPEX_TOOLS=1
in PEX_TOOLS=1 ./pants my_webapp:my_webapp
?
+ Can you provide your BUILD file target?
You should be able to configure your BUILD file target to both package your PEX file for multiple target Python versions and use a neutral shebang. For modern Pants that would be by using the interpreter_constraints
(or platforms
) and shebang
fields of the pex_binary
target.
So, part 1 - build the PEX file for multiple Pythons:
1.) You need to run Pants on a machine with all the Python versions you're targeting if the PEX will contain any non-universal wheels.
2.) Add interpreter_constraints=[">=2.7,<4"]
to your pex_binary
target: https://www.pantsbuild.org/docs/reference-pex_binary#codeinterpreter_constraintscode
Part 2 - ensure the resulting PEX file shebang is #!/usr/bin/env python
instead of `#!/usr/bin/python3.9`:
1.) Add shebang="/usr/bin/env python"
to your pex_binary
target.
You can check the result of this step by running Pants to generate the PEX file and executing head -1 <path to my PEX file>
. You should get #!/usr/bin/env python
white-address-70112
06/14/2021, 10:34 AM$./my_app_play.pex
Traceback (most recent call last):
File "/home/sancbane/my_app_play.pex/.bootstrap/pex/pex.py", line 488, in execute
File "/home/sancbane/my_app_play.pex/.bootstrap/pex/pex.py", line 144, in activate
File "/home/sancbane/my_app_play.pex/.bootstrap/pex/pex.py", line 131, in _activate
File "/home/sancbane/my_app_play.pex/.bootstrap/pex/environment.py", line 427, in activate
File "/home/sancbane/my_app_play.pex/.bootstrap/pex/environment.py", line 775, in _activate
File "/home/sancbane/my_app_play.pex/.bootstrap/pex/environment.py", line 601, in resolve
File "/home/sancbane/my_app_play.pex/.bootstrap/pex/environment.py", line 686, in resolve_dists
pex.environment.ResolveError: Failed to resolve requirements from PEX environment @ /home/sancbane/my_app_play.pex.
Needed manylinux_2_17_x86_64-cp-27-cp27mu compatible dependencies for:
1: psutil
Required by:
ansible-runner 1.4.7
But this pex had no u'psutil' distributions.
2: PyYAML
Required by:
ansible-runner 1.4.7
But this pex had no u'PyYAML' distributions
Please find our pants.toml file below:
[GLOBAL]
pants_version = "2.4.1"
backend_packages = [
'pants.backend.python',
'pants.backend.python.lint.black',
'pants.backend.python.lint.isort',
'pants.backend.python.lint.flake8',
'pants.backend.python.typecheck.mypy',
]
[source]
root_patterns = [
'/',
]
[python-setup]
# The default interpreter compatibility for code in this repo. Individual targets can override
# this with the `interpreter_constraints` field. See
# <https://www.pantsbuild.org/docs/python-interpreter-compatibility>.
interpreter_constraints = [">=2.7"]
# Use a constraints file. See <https://www.pantsbuild.org/docs/python-third-party-dependencies>.
requirement_constraints = "my_app_playbooks/python/constraints.txt"
# We search for interpreters on both on the $PATH and in the `$(pyenv root)/versions` folder.
# If you're using macOS, you may want to leave off the <PATH> entry to avoid using the
# problematic system Pythons. See
# <https://www.pantsbuild.org/docs/python-interpreter-compatibility#changing-the-interpreter-search-path>.
interpreter_search_paths = ["<PATH>", "<PYENV>"]
Could you please advise us on the samewhite-address-70112
06/14/2021, 10:55 AMpython_library(
dependencies = [
'my_app_playbooks/configs',
'my_app_playbooks/configs/casper_rba_config.py',
'my_app_playbooks/telemetry',
'my_app_playbooks/clients',
'my_app_playbooks/playbooks',
'./ansible_runner',
':all',
'./configs:json_resources',
'my_app_playbooks/python:requirements.txt',
],
)
pex_binary(
#shebang="/usr/bin/env python",
name='my_app_play',
entry_point='my_app_play.py',
zip_safe=False,
#execution_mode="venv",
interpreter_constraints=[">=2.7"],
)
archive(
name="app_with_config",
packages=[":my_app_play"],
files=[":allfiles"],
format="tar.gz",
)
resources(
name='all',
sources=['**'],
)
files(
name="allfiles",
sources=['hosts','ansible_runner/**','!*.py','!**/BUILD'],
)
enough-analyst-54434
06/14/2021, 1:13 PMwhite-address-70112
06/14/2021, 2:25 PMenough-analyst-54434
06/14/2021, 2:33 PMplatforms
instead of interpreter constraints: https://www.pantsbuild.org/docs/reference-pex_binary#codeplatformscode
Also, you'll need to have pre-built wheels available for all distributions. Neither Pants nor Pex has cross-building magic. This is confusing enough that I should provide an example. I'll prepare an example-python branch for you to look at.white-address-70112
06/14/2021, 2:38 PMplatforms=[
"manylinux_2_17_x86_64-cp-27-cp27mu",
"linux_x86_64-cp-27-cp27mu",
"macosx_10.12_x86_64-cp-36-cp36m",
],
but when i try to package it i m getting the below error:
sanchayan@sanchayan-mac pex_build % ./pants package my_app_playbooks:my_app_play
19:27:42.68 [INFO] Completed: Resolving 2 requirements: ansible-runner, requests
19:27:42.68 [ERROR] Exception caught: (pants.engine.internals.scheduler.ExecutionError)
File "/Users/sanchayan/.cache/pants/setup/bootstrap-Darwin-x86_64/2.4.1_py37/lib/python3.7/site-packages/pants/bin/local_pants_runner.py", line 229, in _run_inner
return self._perform_run(goals)
File "/Users/sanchayan/.cache/pants/setup/bootstrap-Darwin-x86_64/2.4.1_py37/lib/python3.7/site-packages/pants/bin/local_pants_runner.py", line 168, in _perform_run
return self._perform_run_body(goals, poll=False)
File "/Users/sanchayan/.cache/pants/setup/bootstrap-Darwin-x86_64/2.4.1_py37/lib/python3.7/site-packages/pants/bin/local_pants_runner.py", line 190, in _perform_run_body
poll_delay=(0.1 if poll else None),
File "/Users/sanchayan/.cache/pants/setup/bootstrap-Darwin-x86_64/2.4.1_py37/lib/python3.7/site-packages/pants/init/engine_initializer.py", line 136, in run_goal_rules
goal_product, params, poll=poll, poll_delay=poll_delay
File "/Users/sanchayan/.cache/pants/setup/bootstrap-Darwin-x86_64/2.4.1_py37/lib/python3.7/site-packages/pants/engine/internals/scheduler.py", line 530, in run_goal_rule
self._raise_on_error([t for _, t in throws])
File "/Users/sanchayan/.cache/pants/setup/bootstrap-Darwin-x86_64/2.4.1_py37/lib/python3.7/site-packages/pants/engine/internals/scheduler.py", line 500, in _raise_on_error
wrapped_exceptions=tuple(t.exc for t in throws),
Exception message: 1 Exception encountered:
ProcessExecutionFailure: Process 'Resolving 2 requirements: ansible-runner, requests' failed with exit code 1.
stdout:
stderr:
ERROR: Could not find a version that satisfies the requirement ansible-runner
ERROR: No matching distribution found for ansible-runner
ERROR: Could not find a version that satisfies the requirement ansible-runner
ERROR: No matching distribution found for ansible-runner
pid 84331 -> /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python /Users/sanchayan/.cache/pants/named_caches/pex_root/pip.pex/cf4106b4c7898b462f7c172dde686a9747103f1a --disable-pip-version-check --no-python-version-warning --exists-action a --use-feature 2020-resolver --isolated -q --cache-dir /Users/sanchayan/.cache/pants/named_caches/pex_root --log /private/var/folders/py/mw_r2yw93rv65p8zp60whqqw0000gn/T/process-executiontYLj2S/.tmp/tmpjXFBnp/pip.log download --dest /private/var/folders/py/mw_r2yw93rv65p8zp60whqqw0000gn/T/process-executiontYLj2S/.tmp/tmpZ4uxsO/linux_x86_64-cp-27-cp27mu --platform manylinux2014_x86_64 --platform linux_x86_64 --implementation cp --python-version 27 --abi cp27mu --only-binary :all: --constraint my_app_playbooks/python/constraints.txt ansible-runner requests --index-url <https://pypi.org/simple/> --retries 5 --timeout 15 exited with 1 and STDERR:
None
(Use --print-stacktrace to see more error details.)
enough-analyst-54434
06/14/2021, 2:41 PM[python-repos] repos
enough-analyst-54434
06/14/2021, 3:12 PMwhite-address-70112
06/15/2021, 6:13 PMwhite-address-70112
06/15/2021, 6:13 PM/Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8: can't find '__main__' module in './dist//my_playbooks/play.pex'
⠂ 196.46s Resolving 5 requirements: ansible, ansible-runner, psutil, requests, sshtunnel
enough-analyst-54434
06/15/2021, 6:31 PMwhite-address-70112
06/15/2021, 6:42 PMenough-analyst-54434
06/15/2021, 7:10 PM__main__.py
. The uncompressed size of the PEX zip is ~700MB which is not interesting. It has 120,950 files (entries) though and that is potentially interesting since storing more than ~2^16 files requires zip64 extensions. .... and - lo and behold: https://bugs.python.org/issue32959enough-analyst-54434
06/15/2021, 7:11 PMfull-intern-53951
06/15/2021, 7:11 PMenough-analyst-54434
06/15/2021, 7:12 PMfull-intern-53951
06/15/2021, 7:13 PMinterpreter_constraints=[">=3.9.0","<=3.9.9"],
enough-analyst-54434
06/15/2021, 7:14 PMmkdir test && for i in `seq 1 120950`; do touch test/example_$i.py; done
zip -q numerous_but_small.zip test/
$ PYTHONPATH=numerous_but_small.zip python3.8 -c 'import test.example_1; print(test.example_1.__file__)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'test.example_1'
$ PYTHONPATH=numerous_but_small.zip python3.9 -c 'import test.example_1; print(test.example_1.__file__)'
/home/jsirois/Downloads/test/example_1.py
enough-analyst-54434
06/15/2021, 7:15 PMfull-intern-53951
06/15/2021, 7:17 PMenough-analyst-54434
06/15/2021, 7:17 PMenough-analyst-54434
06/15/2021, 7:18 PMenough-analyst-54434
06/15/2021, 7:20 PMinterpreter_constraints=[">=3.9,<4"],
The comma embedded in that string acts as ANDenough-analyst-54434
06/15/2021, 7:22 PMfull-intern-53951
06/15/2021, 7:28 PMenough-analyst-54434
06/15/2021, 7:29 PMenough-analyst-54434
06/15/2021, 7:30 PMhead -1 my.pex
after you use Pants to create your PEX file. You should notice a python 3.9 shebang line.full-intern-53951
06/15/2021, 7:32 PMpex_binary(
shebang="/usr/bin/env python3.9",
name='my_app_play',
entry_point='my_app_play.py',
zip_safe=False,
execution_mode="unzip",
interpreter_constraints=[">=3.9,<4"],
)
also, I see it not packaging ansible, please check the below
7.96s Resolving 4 requirements: ansible-runner, psutil, requests, sshtunnelfull-intern-53951
06/15/2021, 7:33 PMfull-intern-53951
06/15/2021, 7:33 PMenough-analyst-54434
06/15/2021, 7:34 PMenough-analyst-54434
06/15/2021, 7:34 PMfull-intern-53951
06/15/2021, 7:35 PMenough-analyst-54434
06/15/2021, 7:36 PMimport ansible
? If not, Pants can't know ansible should be included.full-intern-53951
06/15/2021, 7:36 PMimport ansible
white-address-70112
06/15/2021, 7:37 PMenough-analyst-54434
06/15/2021, 7:39 PMwhite-address-70112
06/15/2021, 7:44 PMpython_library(
dependencies = [
'my_app_playbooks/configs',
'my_app_playbooks/configs/my_rba_config.py',
'my_app_playbooks/telemetry',
'my_app_playbooks/clients',
'my_app_playbooks/playbooks',
'./ansible_runner',
':all',
'./configs:json_resources',
'my_app_playbooks/python:requirements.txt',
],
)
white-address-70112
06/15/2021, 7:44 PMcat my_app_playbooks/python/requirements.txt
ansible
requests
sshtunnel
psutil
ansible-runner
enough-analyst-54434
06/15/2021, 7:46 PMwhite-address-70112
06/15/2021, 7:49 PMwhite-address-70112
06/15/2021, 7:49 PMenough-analyst-54434
06/15/2021, 7:50 PMwhite-address-70112
06/15/2021, 7:51 PMwhite-address-70112
06/15/2021, 7:52 PMcat my_app_playbooks/python/requirements.txt
ansible=4.1.0
requests
sshtunnel
psutil
ansible-runner
white-address-70112
06/15/2021, 7:52 PM3.45s Resolving 4 requirements: ansible-runner, psutil, requests, sshtunnel
enough-analyst-54434
06/15/2021, 7:55 PM./pants dependencies --transitive --type=source-and-3rdparty your/pex_binary:target
full-intern-53951
06/15/2021, 8:03 PMenough-analyst-54434
06/15/2021, 8:07 PMenough-analyst-54434
06/15/2021, 8:07 PMenough-analyst-54434
06/15/2021, 8:10 PMfull-intern-53951
06/15/2021, 8:20 PMresources(
name='all',
sources=['**', '!*.py'],
)
enough-analyst-54434
06/15/2021, 8:30 PMfull-intern-53951
06/15/2021, 8:35 PMfull-intern-53951
06/15/2021, 8:36 PMfull-intern-53951
06/15/2021, 8:36 PMfull-intern-53951
06/15/2021, 8:36 PMfull-intern-53951
06/15/2021, 8:39 PMgtripura@gtripura-mac pex_build % pip3.8 freeze | grep ansible
ansible==4.1.0
ansible-core==2.11.1
ansible-runner==1.4.7
gtripura@gtripura-mac pex_build % pip3.9 freeze | grep ansible
ansible==3.1.0
ansible-base==2.10.7
ansible-runner==1.4.7
enough-analyst-54434
06/15/2021, 8:42 PMenough-analyst-54434
06/15/2021, 8:57 PMfull-intern-53951
06/15/2021, 9:16 PMfull-intern-53951
06/15/2021, 9:17 PMenough-analyst-54434
06/15/2021, 9:17 PMfull-intern-53951
06/15/2021, 9:17 PMenough-analyst-54434
06/15/2021, 9:18 PMenough-analyst-54434
06/15/2021, 9:19 PMacoustic-computer-61887
06/15/2021, 9:20 PMfull-intern-53951
06/15/2021, 9:21 PMfull-intern-53951
06/15/2021, 9:22 PMenough-analyst-54434
06/15/2021, 9:23 PMfull-intern-53951
06/15/2021, 9:23 PMzip_safe=False,
execution_mode="unzip",
full-intern-53951
06/15/2021, 9:23 PMenough-analyst-54434
06/15/2021, 9:23 PMfull-intern-53951
06/15/2021, 9:25 PMfull-intern-53951
06/15/2021, 9:25 PMenough-analyst-54434
06/15/2021, 9:25 PMfull-intern-53951
06/15/2021, 9:26 PMfull-intern-53951
06/15/2021, 9:53 PMenough-analyst-54434
06/15/2021, 9:56 PMfull-intern-53951
06/15/2021, 10:03 PMfull-intern-53951
06/15/2021, 10:04 PMenough-analyst-54434
06/15/2021, 10:06 PMwhite-address-70112
06/16/2021, 4:05 PMfull-intern-53951
06/16/2021, 4:18 PMfull-intern-53951
06/16/2021, 4:28 PMenough-analyst-54434
06/16/2021, 4:44 PM