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.

Document rules for conditional imports

See original GitHub issue

Conditional imports. in our case, optional 3rd party dependencies, are not covered by PEP-8 and gives us some flexibility. At the same time, this could cause ambiguity. How about agreeing on some consensus and document it where and how they should appear in the code?

Conditional imports in Optuna look something like the following.

import numpy 

import optuna

# The conditional import comes in a separate island after standard imports.
try:
    import cma
    _available = True
except ImportError:
    _available = False

Note that we additionally often assign a “available” variable depending on the condition. They frequently appear under visualization/* and integration/*.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
hvycommented, Dec 11, 2019

For simple cases such as the one in the description, however, I’m wondering if the following isn’t cleaner. I.e. import at the PEP-8 suggested location, surround with try, except and allow a single assignment.

try:
    import cma
except ImportError:
    cma = None
import numpy 

import optuna

This does obviously not catch the error object but I believe that the reason is obvious in most of the cases for ImportErrors.

1reaction
hvycommented, Jun 3, 2020

Yes, I think it’s fine to close this ticket. It’s non-trivial to establish proper guidelines and the merits aren’t that significant.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I conditionally import an ES6 module? - Stack Overflow
This basically returns a promise. Resolution of promise is expected to have the module. The proposal also have other features like multiple dynamic...
Read more >
Dynamic, Conditional Imports - CSS-Tricks
There is a pattern to do it conditionally though. It's like this: (async () => { if (condition) { // await import("stuff. js");...
Read more >
Conditional Formatting import/export or other means
Conditional Formatting import/export or other means. Trying to copy multiple rules from one sheet to many sheets within the same workbook ...
Read more >
import - JavaScript - MDN Web Docs - Mozilla
The static import declaration is used to import read-only live bindings which are exported by another module. The imported bindings are ...
Read more >
Conditional imports across Flutter and Web - Medium
The file will only have a getter which returns an instance of AuthManager which is the abstract class our two managers inherit from....
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