pex-tool / pex

A tool for generating .pex (Python EXecutable) files, lock files and venvs.
https://docs.pex-tool.org/
Apache License 2.0
2.84k stars 266 forks source link

Force pex to use a wheel of a library but resolve other dependencies from index #2567

Closed maxkoretskyi closed 1 month ago

maxkoretskyi commented 1 month ago

I'm trying to build a pex executable but ensure that the library is resolved from a custom index, not PyPi. One risky way to do is to switch places --index-url and --extra-index-url:

pex my-package -o my-executable.pex --index-url https://my.custom.index --extra-index-url https://pypi.org/simple

but I'm thinking of a better way of simply using my-package.whl like I can do with pip in a venv:

pip install my-package.whl

is there any way for me to do the same with pex?

jsirois commented 1 month ago

You should have just tried it! This should work, ~same as Pip.

pex my-package.whl -o my-executable.pex

Does that work for you @maxkoretskyi?

maxkoretskyi commented 1 month ago

thanks John! It does work 😊 just to not get embarrassed too much, I did try, just in the wrong way:

pex -f my-package.whl -o my-executable.pex

😀

jsirois commented 1 month ago

Aha - ok. So, -f / --find-links should also work just like Pip. It needs to be a directory containing a flat set of wheels and / or sdists.

So this-too should work[^1]:

mkdir fl-repo
ln -s ../my-package.whl fl-repo/
pex -f fl-repo my-package -o my-executable.pex

[^1]: Well, maybe work. This re-introduces the question of index precedence. Does Pip resolve from --find-links repos before PyPI? Who knows - they have never guaranteed ordering of resolution when there are multiple indexes / find-links repos in play and that ordering has in face changed over time.

jsirois commented 1 month ago

Ok @maxkoretskyi - it sounds like I can close this as an answered question. Let me know though if you have more questions.

jsirois commented 1 month ago

@maxkoretskyi is your package local? If so, more simply you can just:

pex . -o my-executable.pex

Here I assume you're in the project's root directory. If not pex path/to/project -o my-executable.pex works too. Pex understands how to build your project into a wheel no matter your project's build system.

maxkoretskyi commented 1 month ago

thanks, pretty much answered, just a few clarifications

pex -f fl-repo my-package -o my-executable.pex

I was trying to avoid specifying my-package b/c I wasn't sure if pex/pip prefers index over -f

also, when I specify path to a wheel like this mydir/k-*.whl:

pex mydir/k-*.whl -o f.pex

is the expansion supposed to work or not?

Pex understands how to build your project into a wheel no matter your project's build system.

that's a good addition, thanks! so it can do the same as python -m build ?

jsirois commented 1 month ago

I was trying to avoid specifying my-package b/c I wasn't sure if pex/pip prefers index over -f

Yeah, you're right to be unsure: https://github.com/pex-tool/pex/issues/2567#user-content-fnref-1-c269f1869919f2e2e04982d9887dcad6

is the expansion supposed to work or not?

Well, the expansion is handled by your shell; so its up to your shell! But for bash anyway, the answer is yes. But you should try it!

that's a good addition, thanks! so it can do the same as python -m build

Yes. Just like Pip can. Pex should treat all requirements (positional arguments) on the CLI just like Pip. There is a fair bit of code and tests surrounding Pex's emulation of Pip requirement parsing:

maxkoretskyi commented 1 month ago

Awesome, thanks!

But you should try it!

PS. I do try everything before asking, just probably in a different way :)

jsirois commented 1 month ago

Did pex mydir/k-*.whl -o f.pex fail?

maxkoretskyi commented 1 month ago

no, it's good! once I got confirmation from you, I found where I got confused... it's probably too late here

jsirois commented 1 month ago

Ok, excellent. Thanks @maxkoretskyi!