Document rules for conditional imports
See original GitHub issueConditional 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:
- Created 4 years ago
- Comments:9 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.This does obviously not catch the error object but I believe that the reason is obvious in most of the cases for
ImportError
s.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.