Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to dynamically import packages? #335

Open
rusmux opened this issue Jul 28, 2023 · 3 comments
Open

How to dynamically import packages? #335

rusmux opened this issue Jul 28, 2023 · 3 comments
Labels
question Further information is requested

Comments

@rusmux
Copy link

rusmux commented Jul 28, 2023

I'm designing a package that have main functionality and optional functionality that requires extra packages to be installed. I want to build a CLI for the package. jsonargparse dissuades use of parse_known_args(), so I cannot import packages only when needed.

For example, I want to add optional function arguments. I cannot use parser.add_function_args because it would need me to import the function. I must add arguments manually, but I can't always do this, because some arguments may depend on classes in my optional functionality. How to manage this case? Thank you

@mauvilsa
Copy link
Member

I cannot use parser.add_function_args because it would need me to import the function.

You could do like:

if optional_package_available:
    import optional_package
...
if optional_package_available:
    parser.add_function_args(optional_package.some_function, ...)

I can't always do this, because some arguments may depend on classes in my optional functionality

Not really sure what you mean by this.

@mauvilsa mauvilsa added the question Further information is requested label Jul 28, 2023
@rusmux
Copy link
Author

rusmux commented Jul 28, 2023

Yes, but if I try to run a command with arguments, it won't recognize them and exit, and the only way to show a message about installing additional packages, as I understand, is to catch SystemExit, which I think is not the most elegant solution.

@mauvilsa
Copy link
Member

mauvilsa commented Jul 31, 2023

I guess what you need is an Action that always raises an argument exception with some message. But this action must be triggered if nested arguments are given. That is, something like:

if optional_package_available:
    from optional_package import some_function
...
if optional_package_available:
    parser.add_function_arguments(some_function, "group")
else:
    parser.add_argument("--group", action=ActionNotAvaliable("please install optional_package"))

The CLI would exit with the given message if any option --group.* is given. This action would be useful for the case of optional packages, but also for deprecated options that were removed.

jsonargparse internally importing optional packages, I am not sure it is a good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants