Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between 'import a.b as b' and 'from a import b' in python [duplicate]

Tags:

python

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?

like image 638
Tom Tanner Avatar asked Jun 20 '18 08:06

Tom Tanner


People also ask

What is the difference between import and from import in Python?

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.

What are the two types of import in Python?

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)

What is import from in Python?

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.

What is the use of from in Python?

The from keyword is used to import only a specified section from a module.

What is the difference between import B and import as B?

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.

When to use import and when to use from in Python?

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.

Why can’t I import a module in Python?

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.

What is the difference between importing a module and a function?

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.


2 Answers

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.

like image 180
Sraw Avatar answered Sep 29 '22 15:09

Sraw


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).

like image 24
John Ladasky Avatar answered Sep 29 '22 15:09

John Ladasky