I’m currently looking into the `export` functional...
# general
g
I’m currently looking into the
export
functionality and I came up with this pair of just recipes to create the venv and the include a
pth
file with paths that are source roots within the repo and are considered a dependency even at the transitive level:
Copy code
pants-find-dep-roots +targets:
    #!/usr/bin/env python
    import subprocess
    import pathlib

    roots = subprocess.check_output(["pants", "roots"]).decode().split()
    tgt_deps = subprocess.check_output(
        ["pants", "dependencies", "--transitive"] + "{{ targets }}".split()
     ).decode().split()
    used_roots = set()
    for root in roots:
        for d in tgt_deps:
            if d.startswith(root):
                used_roots.add(root)
    base_dir = pathlib.Path("{{ justfile_directory() }}")
    for r in sorted(used_roots):
        print(str(base_dir / r))

pants-export-venv-with-dep-roots $resolver +targets:
    #!/usr/bin/env bash
    set -eu
    venv_path=$(pants export --resolve=$resolver | tail -n 1 |  awk '{print($NF)}')
    pants_roots_pth="$(echo $venv_path/lib/python*/site-packages)/pants-roots.pth"
    just pants-find-dep-roots {{ targets }} >> $pants_roots_pth
    echo "========================================================"
    echo "Virtualenv ready for resolver: $resolver"
    echo "With roots:"
    cat $pants_roots_pth
    echo "========================================================"
    echo "Activate with: . $venv_path/bin/activate"
Question: Is there something like this already available in pants or in the form of a plugin? Any opinion against this approach?
b
Huh, nifty. I don't know enough about the details of venvs and .pth to have an opinion on whether it's "good practice" or not, but it'd be kinda cool if
pants export
could do that automatically to avoid needing to configure
PYTHONPATH
env var manually.
p
Pants can create an export with PEP-660 editable installs of any
python_distribution
targets in the resolve. Assuming you have a python_distribution for each of your source roots, this should do what you want. https://www.pantsbuild.org/2.21/reference/goals/export#py_editable_in_resolve
g
Thanks for the suggestion Jacob, yeah I was aware of that part. I just didn't want to create all of those targets for things that are not going to be distributed outside the repo. The end result should be roughly the same, I think that functionality relies exactly in pth files.
I do already have a lot of setup.py files, but I don't want to mess with them (yet), I'm experimenting with migrating a monorepo that usually was managed via internal pypi and docker images.