Skip to content

Future as "extras_require" (or how to conditionally import future) #592

@maxdup

Description

@maxdup

Unless I'm mistaken, there's is no way to conditionally import python-future

I have a couple of pasteurised libraries. When in modern environments, my libs don't really require future to run which results in some amount of bloat. To avoid that, I've been trying to leverage setup.py's "extras_require", which would only install future when deemed necessary.

setup(
    ...
    install_requires=[...],
    extras_require={
        ":python_version<='2.7'":  ["future"]
    },
    ...
)

The conventional way of doing a conditional import would look something like this:

try:
    from __future__ import absolute_import
    from __future__ import division
    from __future__ import print_function
    from __future__ import unicode_literals

    from builtins import open
    from future import standard_library
    standard_library.install_aliases()
except ImportError:
    pass

Sadly, with future, this results in SyntaxError: from __future__ imports must occur at the beginning of the file.

I was wondering if conditional imports could be made possible (Or if there's an alternative approach that I'm unaware of here).

Note:
Implementing issue #266 could make this issue irrelevant. Feel free to close if #266 is being prioritised.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions