3 ms·
It's a topic close to my heart since that is one of the key thing that we solve in the OSS project windmill [1], letting users define simple single-file scripts
by rubenfiszel 4y ago
It's a topic close to my heart since that is one of the key thing that we solve in the OSS project windmill [1], letting users define simple single-file scripts in python/deno/go and handle the dependencies for them.
For deno, as the article points out, it's easy, deno has this feature baked in and we just have to deno run the scripts, similar for go where the imports are pretty much a fully qualified pointer to their source and version.
For python, we have to do a bit of machinery: we parse the AST, look at the imports, use the heuristics that most import names correspond to their pypi package (we maintain also a list of exception-mappings), pip-compile [2] all the imports, and then get a requirement file that we attach to the script, pip-install it before running the script (then we do a LOT of magic to cache the dependencies so one doesn't actually have to install them 99.9% of the time).
Note: if you're interested in a collection of single-file scripts to many APIs, that you can just copy-paste and call the main function of, that's what windmill's hub is for [3]
[1]: https://github.com/windmill-labs/windmill https://github.com/windmill-labs/windmill
[2]: https://github.com/jazzband/pip-tools https://github.com/jazzband/pip-tools
[3]: https://hub.windmill.dev/ https://hub.windmill.dev/
- agoose77 4y agoPython actually has support for this with `pip-run`. There's a PR to get similar behaviour into `pipx`, which is effectively the `npx` of Python world. I think the single-script-with-dependencies workflow has been somewhat ignored until now; it would be nice to see that become easier.
- rubenfiszel 4y agoIf I'm not mistaken, looking at the docs of: https://pypi.org/project/pip-run/ https://pypi.org/project/pip-run/ it doesn't parse the AST to infer the dependencies, you still have to declare them (either in script with # Requirements comment (which we also use as an escape hatch https://docs.windmill.dev/docs/reference#dependency-management https://docs.windmill.dev/docs/reference#dependency-manageme...)) or externally declared. So it's close but not exactly the same.