Hi everyone :slightly_smiling_face: I am trying to...
# general
a
Hi everyone šŸ™‚ I am trying to figure out how to effectively use internal libraries in our Python monorepo, and running into some problems. I think Pants might be able to solve them but I'm not sure where to begin. For context, my layout looks like this:
Copy code
.
└── monorepo/
    ā”œā”€ā”€ .github/
    │   └── workflows/
    │       ā”œā”€ā”€ build_app1.yaml
    │       └── build_app2.yaml
    ā”œā”€ā”€ app/
    │   ā”œā”€ā”€ app1/
    │   │   ā”œā”€ā”€ app1/
    │   │   │   └── __init__.py
    │   │   ā”œā”€ā”€ Dockerfile
    │   │   ā”œā”€ā”€ pyproject.toml
    │   │   └── poetry.lock
    │   └── app2/
    │       ā”œā”€ā”€ app2/
    │       │   └── __init__.py
    │       ā”œā”€ā”€ Dockerfile
    │       ā”œā”€ā”€ pyproject.toml
    │       └── poetry.lock
    └── lib/
        └── lib1/
            ā”œā”€ā”€ lib1/
            │   └── __init__.py
            ā”œā”€ā”€ pyproject.toml
            └── poetry.lock
e
A lot can depend on the details of your project, but I would suggest reading through this: https://www.pantsbuild.org/blog/2021/10/13/pants-pex-and-docker#deploying-pex-files-in-your-docker-images Its what got me started with pants. The pattern that's working for us is something like this: • Set up your source roots (https://www.pantsbuild.org/stable/docs/using-pants/key-concepts/source-roots has some examples for how to do this for various project structures) • Set up your IDE https://www.pantsbuild.org/stable/docs/using-pants/setting-up-an-ide (to make sure your dev environment can understand/follow your source roots setup) • Then, you'll have a bunch of
python_source
targets (whether libs or app files mostly doesn't matter too much) • Create a
pex_binary
target that specifies the entrypoint file for your code • In your dockerfile,
COPY
the pex file (from
/dist/something/whatever.pex
which is where the build artifact ends up) • Now, when you do
pants package /path/to/app1:app1_docker_image
(I made up a path/target name here). Pants will use its dependency inference techniques to find every python file depended on by your
app1_entrypoint.py
(that you used for the
pex_binary
), build them into an executable pex file, and copy them into your dockerfile.
Good luck with your pants journey. I've found that getting over the initial hurdle is the hardest part, and then it works really smoothly from there and your investment can start to pay off. (Also, in case you missed it in the docs: https://www.pantsbuild.org/stable/docs/getting-started/incremental-adoption is a fairly good place to get started)
a
Thank you, this was very helpful!