pypa / flit

Simplified packaging of Python modules
https://flit.pypa.io/
BSD 3-Clause "New" or "Revised" License
2.14k stars 130 forks source link

Include different set of files for feature variants (optional-dependencies)? #659

Closed jdmansour closed 10 months ago

jdmansour commented 10 months ago

With project.optional-dependencies, it is possible to define additional sets of dependencies to be installed. I wonder if it is possible to restrict which files are included in such an installation. For example, I have one feature that requires tornado, but I would like my library to be usable even without tornado installed. So I put the code into mypackage/tornado.py, and would define a feature like this:

[project.optional-dependencies]
tornado = [
    "tornado >=6.3.2"
]

However, if I install without that feature, the file is still installed. Is there a way to omit it, or what should I do? One option is to just ignore it - whether I get an ImportError for the tornado package or for my module doesn't make a difference. Another option is to wrap the whole module in a try block. But then I don't really need to define my optional dependency in pyproject.toml. If it is there (because it is used by some other module) I can use it, if not, then not. How is this typically solved?

takluyver commented 10 months ago

That's not possible, I'm afraid. It's not a limitation specific to Flit, either - the wheel format doesn't have any way to tie the files inside to particular optional dependencies.

The usual way to do it, as far as I know, is to have the integration code always installed but only usable when the optional dependency is present. You could try to catch the import error and raise a more 'helpful' error message, but it's easy for that to obscure the source of unexpected errors.

If that's unsatisfactory, you could also split out a separate mypackage-tornado which depends on both mypackage & tornado. But then you have to think about what version ranges are compatible between mypackage and mypackage-tornado.

On Wed, 20 Sept 2023, 16:00 Jason Mansour, @.***> wrote:

With project.optional-dependencies, it is possible to define additional sets of dependencies to be installed. I wonder if it is possible to restrict which files are included in such an installation. For example, I have one feature that requires tornado, but I would like my library to be usable even without tornado installed. So I put the code into mypackage/tornado.py, and would define a feature like this:

[project.optional-dependencies]tornado = [ "tornado >=6.3.2" ]

However, if I install without that feature, the file is still installed. Is there a way to omit it, or what should I do? One option is to just ignore it - whether I get an ImportError for the tornado package or for my module doesn't make a difference. Another option is to wrap the whole module in a try block. But then I don't really need to define my optional dependency in pyproject.toml. If it is there (because it is used by some other module) I can use it, if not, then not. How is this typically solved?

— Reply to this email directly, view it on GitHub https://github.com/pypa/flit/issues/659, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACQB5NJYNFQLA4ZDIJXM23X3LZJJANCNFSM6AAAAAA477KOK4 . You are receiving this because you are subscribed to this thread.Message ID: @.***>

jdmansour commented 10 months ago

Thanks for the answer, that is what I suspected. It is no big deal to leave the extra code around since it is not many lines, and if it becomes bigger one could split it off into another package as you said.