question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use parser from pip to handle the command-line arguments gracefully?

See original GitHub issue

Currently, requirements.txt with pip CLI arguments cause a parsing error, thus need to be somehow patched before being handed to mach-nix. In particular this means e.g. that with an unmodified requirements.txt the process won’t get to checking the overrides (which may be overriding the package of interest, thus eliminating the actual need to handle the custom arguments). Handling un-patched requirements.txt is desirable because then one might get by without ever checking out the repo

Some cases where CLI arguments do appear:

A tolerably hack-ish way might be to just re-use pip._internal.commands.install.InstallCommand:

>>> from pip._internal.commands.install import InstallCommand
>>> ic = InstallCommand("install", "fake install command")
>>> options, requirements = ic.parser.parse_args(["albumentations==0.5.1", "--no-binary=imgaug,albumentations", "-e", "git+https://github.com/user/repo.git#egg=repo"])
>>> requirements
['albumentations==0.5.1']
>>> options.editables
['git+https://github.com/user/repo.git#egg=repo']

It might still be worth displaying a warning when custom options are provided, because there’s no actual guarantee that an override and the CLI arguments are in sync

P.S. I’m opening this issue not as a “feature request” but more as a request for validation and comments. Thanks

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
DavHaucommented, Jun 16, 2021

One disadvantage this approach might have is more code to maintain, which is a big deal as mach-nix appears to be a voluntarily maintained project (which I’m grateful for)

We could implement a separate IFD based python based requirements parser which is separated from the mach-nix resolver/code-generator. The main point is that I would not want to increase the code generator code base.

Nowadays pip has its own dependency solver, supports different build back-ends, those in they turn may have their own notion of dependencies, etc. Re-using what pip has already implemented and tested just seems like a more sustainable way.

We cannot just use pip directly for resolving since it does make arbitrary API request to a non-reproducible / non-pinable online DB. But what we already do is to use pip’s resolvlib library. That’s the closest we can get to pip. But I’m not even sure if trying to stay close to pip brings us any benefit. Pip’s whole model of depedency resolution is very constrained by not having an offline DB which is cheap to query. We don’t have that limitation. see https://github.com/DavHau/mach-nix/issues/278

Which alternate dependency notion other than requirements.txt does pip support?

Btw, these pip install --dry-run/pip resolve proposals could be relevant: …

Thanks for looking into this. I had a quick look and them and fail to see how those could help us. Even if pip had a --dry-run, we couldn’t use it in nix since it still requires downloading files from pypi. By design it cannot work without downloading files. Pypi doesn’t even have a proper dependency DB that could be downloaded etc. Even if you pre-compute all your requirements perfectly, you would still need online access again whenever you change a requirement.

For a solution which is completely based on pip and works with nix, maybe have a look at this: https://github.com/NixOS/nixpkgs/pull/121425

In general I agree that we should make use of existing library code wherever we can. But from an architecture point of view mach-nix has huge advantages compared to pip and I think we should use them. Trying to be closer to pip would probably require giving up some of these advantages.

1reaction
DavHaucommented, Jun 14, 2021

In my opinion, in a perfect world, mach-nix would be written in 100% nix language. Currently it would be quite difficult to implement dependency resolution in nix. But I think everything that can be done in nix, should be done in nix. Maybe one day we can get rid of the IFD python code completely.

Also I’d like to reduce the amount of nix code generation as much as possible. I plan to get rid of code generation completely and instead let the resolver dump json data which is then interpreted by nix.

We can also do the parsing in python if it makes things easier, but then again we would have python output that needs to be interpreted by nix. So does that make things easier?

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - What's the best way to parse command line arguments?
1) Initialize import argparse # Instantiate the parser parser = argparse.ArgumentParser(description='Optional app description') · 2) Add ...
Read more >
argparse — Parser for command-line options, arguments and ...
The argparse module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and argparse will figure out ......
Read more >
Plac: Parsing the Command Line the Easy Way — plac ...
Technically plac is just a simple wrapper over argparse which hides most of its complexity by using a declarative interface: the argument parser...
Read more >
10.6. Handling command-line arguments
For simple programs that only take a single argument and have no flags, you can simply use sys.argv[1] to access the argument. There's...
Read more >
Python - Command Line Arguments - Tutorialspoint
Python provided a getopt module that helps you parse command-line options and arguments. This module provides two functions and an exception to enable ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found