Hello everyone. I have a pex_ binary target. Is th...
# general
s
Hello everyone. I have a pex_ binary target. Is there an easy way to get a requirements.txt of this pex binary target? When I say get I mean produce from its 3rd party dependencies.
p
./pants dependencies --dependencies-transitive <pex-target> | grep 3rdparty
Is probably a good starting point, then you can write some basic logic in a simple python script to look up those reqs in your repo's requirements.txt or lock file to get their versions and write that to an actual requirements.txt file that u need.
🙏 1
s
thanks I will go this route then.
e
If you specify
include_tools=True
or
execution_mode="venv"
in your
pex_binary
you get access to introspection tools for the PEX file. For example, 1st creating a PEX file directly - Pants does this for you:
Copy code
$ pex requests --include-tools -o requests.pex
You can then get full detail on the PEX's 3rdparty dependencies:
Copy code
$ PEX_TOOLS=1 ./requests.pex repository info -v --indent 2
{
  "project_name": "requests",
  "version": "2.28.1",
  "requires_python": "<4,>=3.7",
  "requires_dists": [
    "charset-normalizer<3,>=2",
    "idna<4,>=2.5",
    "urllib3<1.27,>=1.21.1",
    "certifi>=2017.4.17",
    "PySocks!=1.5.7,>=1.5.6; extra == \"socks\"",
    "chardet<6,>=3.0.2; extra == \"use_chardet_on_py3\""
  ],
  "location": "/home/jsirois/.pex/installed_wheels/8fefa2a1a1365bf5520aac41836fbee479da67864514bdb821f31ce07ce65349/requests-2.28.1-py3-none-any.whl"
}
{
  "project_name": "charset-normalizer",
  "version": "2.1.0",
  "requires_python": ">=3.6.0",
  "requires_dists": [
    "unicodedata2; extra == \"unicode_backport\""
  ],
  "location": "/home/jsirois/.pex/installed_wheels/5189b6f22b01957427f35b6a08d9a0bc45b46d3788ef5a92e978433c7a35f8a5/charset_normalizer-2.1.0-py3-none-any.whl"
}
{
  "project_name": "idna",
  "version": "3.3",
  "requires_python": ">=3.5",
  "requires_dists": [],
  "location": "/home/jsirois/.pex/installed_wheels/84d9dd047ffa80596e0f246e2eab0b391788b0503584e8945f2368256d2735ff/idna-3.3-py3-none-any.whl"
}
{
  "project_name": "urllib3",
  "version": "1.26.10",
  "requires_python": "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,<4,>=2.7",
  "requires_dists": [
    "brotlicffi>=0.8.0; ((os_name != \"nt\" or python_version >= \"3\") and platform_python_implementation != \"CPython\") and extra == \"brotli\"",
    "brotli>=1.0.9; ((os_name != \"nt\" or python_version >= \"3\") and platform_python_implementation == \"CPython\") and extra == \"brotli\"",
    "brotlipy>=0.6.0; (os_name == \"nt\" and python_version < \"3\") and extra == \"brotli\"",
    "pyOpenSSL>=0.14; extra == \"secure\"",
    "cryptography>=1.3.4; extra == \"secure\"",
    "idna>=2.0.0; extra == \"secure\"",
    "certifi; extra == \"secure\"",
    "ipaddress; python_version == \"2.7\" and extra == \"secure\"",
    "PySocks!=1.5.7,<2.0,>=1.5.6; extra == \"socks\""
  ],
  "location": "/home/jsirois/.pex/installed_wheels/8298d6d56d39be0e3bc13c1c97d133f9b45d797169a0e11cdd0e0489d786f7ec/urllib3-1.26.10-py2.py3-none-any.whl"
}
{
  "project_name": "certifi",
  "version": "2022.6.15",
  "requires_python": ">=3.6",
  "requires_dists": [],
  "location": "/home/jsirois/.pex/installed_wheels/fe86415d55e84719d75f8b69414f6438ac3547d2078ab91b67e779ef69378412/certifi-2022.6.15-py3-none-any.whl"
}
See
PEX_TOOLS=1 ./requests.pex
for help on subcommands, you can drill in from there and produce dependency graphs dot files / pictures and more.
👍 1
h
./pants dependencies path/to:pex_binary | xargs ./pants --filter-target-type=python_requirement peek
will also show you each
python_requirement
and specifically the
requirements
field, which will have values like
django==3.0
from your requirements.txt. (This command assumes you're on Pants 2.12+) You can pipe that into JQ if you want to further process the JSON
e
N.B, as @hundreds-father-404 alluded, the methods he and @polite-garden-50641 described get you direct requirements of the PEX binary (I'm pretty sure anyhow). The PEX_TOOLS way gets you the actual transitive dependency set.