I've always used from a import b
but recently a team at work decided to move a module into a new namespace, and issued a warning notice telling people to replace import b
with import a.b as b
.
I've never used import as
and the only documentation I can find seems to suggest it doesn't support import a.b as b
, though clearly it does.
but is there actually a difference, and if so what?
The difference between import and from import in Python is: import imports the whole library. from import imports a specific member or members of the library.
There are generally three groups: standard library imports (Python's built-in modules) related third party imports (modules that are installed and do not belong to the current application) local application imports (modules that belong to the current application)
import Python: Using the from Statement The import statement allows you to import all the functions from a module into your code. Often, though, you'll only want to import a few functions, or just one. If this is the case, you can use the from statement.
The from keyword is used to import only a specified section from a module.
There is a more important difference which the duplicate does not mention. from a import b imports any object, import a.b as b will only import a module/package/namespace. I have added an answer to the duplicate.
A No ... use import most of the time, but use from is you want to refer to the members of a module many, many times in the calling code; that way, you save yourself having to write "feather." (in our example) time after time, but yet you don't end up with a cluttered namespace.
Since there are no finders, Python can’t find or import new modules. However, Python can still import modules that are already in the module cache since it looks there before calling any finders. In the example above, importlib was already loaded under the hood before you cleared the list of finders.
Whether you import a module or import a function from a module, Python will parse the whole module. Either way the module is imported. "Importing a function" is nothing more than binding the function to a name. In fact import module is less work for interpreter than from module import func.
As far as I know, correct me if I am wrong.
First, import a.b
must import a module(a file) or a package(a directory contains __init__.py
).
For example, you can import tornado.web as web
but you cannot import flask.Flask as Flask
as Flask
is an object in package flask
.
Second, import a.b
also import the namespace a
which from a import b
won't. You can check it by globals()
.
So what's the influence? For example:
import tornado.web as web
Now you have access to namespace tornado
, but you cannot access tornado.options
even though tornado
has this module. But as python's global package management, if you from tornado import options
, you will not only get access to options
but also add it to namespace tornado
. So now you can also access options
by tornado.options
.
I am going to provide only a partial answer, and I will speculate a bit.
1) Sometimes I have observed that the second way works while the first does not. On my system:
Python 3.6.3 (default, Oct 3 2017, 21:45:48)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from tensorflow import keras # <- this works
>>>
>>> import tensorflow.keras as K # <- this fails
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tensorflow.keras'
>>>
2) I usually don't see a difference between these two approaches to importing. I haven't investigated WHY there is a difference with TensorFlow. It may have to do with what names are imported to the top level by the various TensorFlow subfolder init.py files (which are completely empty in most cases, but the one in ../dist_packages/tensorflow/python is pretty long).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With