19

I read here about sorting your import statements in Python, but what if the thing you are importing needs dependencies that have not been imported yet? Is this the difference between compiled languages and interpreted? I come from a JavaScript background and the order in which you load your scripts matter, whereas Python appears not to care. Thanks.

asked Nov 7, 2014 at 15:35
1
  • 1
    So this holds true for importing methods to? Like: from x import xx Commented Nov 7, 2014 at 16:32

4 Answers 4

28

Import order does not matter. If a module relies on other modules, it needs to import them itself. Python treats each .py file as a self-contained unit as far as what's visible in that file.

(Technically, changing import order could change behavior, because modules can have initialization code that runs when they are first imported. If that initialization code has side effects it's possible for modules to have interactions with each other. However, this would be a design flaw in those modules. Import order is not supposed to matter, so initialization code should also be written to not depend on any particular ordering.)

answered Nov 7, 2014 at 15:41
Sign up to request clarification or add additional context in comments.

2 Comments

If one uses the (not recommended) from package import * paradigm, one could also potentially overwrite definitions. This for instance happens with from math import * and from numpy import *.
Import order can matter if the __init__ behavior of the module has side-effects. For example, importing TensorFlow will consume the total of GPU memory available. If you try to import Caffe after importing TensorFlow, Caffe will be unable to claim any memory resources from the GPU(s). There can be simpler things with extension modules that define conflicting settings or place locks on files. "Import" just means "execute some code". This is so ubiquitous among widely used libraries that it's not useful to say import order shouldn't matter because on-import side-effects are a bad practice.
8

Python Import order doesnot matter when you are importing standard python libraries/modules. But, the order matters for your local application/library specific imports as you may stuck in circular dependency loop, so do look before importing.

answered Apr 21, 2017 at 6:20

Comments

6

No, it doesn't, because each python module should be self-contained and import everything it needs. This holds true for importing whole modules and only specific parts of it.

answered Nov 7, 2014 at 15:39

2 Comments

As already commented below: The import order should not matter, but there are packages where it does.
the "should" there is not real in practice. do not count on it
2

Order can matter for various nefarious reasons, including monkey patching.

answered Jun 11, 2022 at 3:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.