Interpretation of the Use and Difference between Python import and from import

  • 2021-12-04 10:38:04
  • OfStack

The system comes with its own module (library)


```cpp
import re
target = 'abc1234xyz'
re.search('(\d+)', target)

 But sometimes, you may see someone writing code like this: 

```python
from re import search
target = 'abc1234xyz'
search('(\d+)', target)

So what is the difference between these two import methods?

Let's look at their types using the type function:


>>> import re
>>> type(re)
<class 'module'>
>>> from re import search
>>> type(search)
<class 'function'>

As you can see, re imported directly using import re is an module class, which is a module. We call it a regular expression module. When we are from re import search, this search is an function class, which we call the search function.
A module can contain multiple functions.
If, in your code, you have decided to use only the search function and not the other functions in the regular expression, then you can use either method, no difference.
However, if you want to use multiple functions under a regular expression or a few constants, it will be more concise and clear to use the first scheme.

For example:


import re
re.search('c(.*?)x', flags=re.S)
re.sub('[a-zA-Z0-9]', '***', target, flags=re.I)

In this example, you used re. search, re. sub, re. S, and re. I. The latter two are constants, which are used to ignore newline characters and case.
But if you use from re import search, sub, S, I to write the code, then the code will look like this:


import re
search('c(.*?)x', flags=S)
sub('[a-zA-Z0-9]', '***', target, flags=I)

Although it seems concise, it is easy to forget what S and I variables are once you have more lines of code. Moreover, our own defined functions are likely to be named sub or search, thus overriding the two functions with the same name under the regular expression module. This will lead to many undetectable potential bug.
Give another example. datetime module of Python, we can directly import datetime, at this time we import an datetime module,


 The output is: class ' module'

But if you write from datetime import datetime, then the datetime you import is an type class:


 The output is: class ' type'

Because datetime is imported in this way, it is a type in Python, which is used to represent data containing date and time.

datetime imported by these two import methods, although the name is 1, their meaning is completely different. Please observe the following two writing methods:


import datetime
now = datetime.datetime.now()
one_hour_ago = now - datetime.timedelta(hours=1)
from datetime import datetime, timedelta
now = datetime.now()
one_hour_ago = now - timedelta(hours=1)

The second writing seems simple, but in fact it is more troublesome to change. For example, I also need to add a variable today to record today's date.

For the first code, we only need to add one line:


today = datetime.date.today()

But for line 2, we need to modify the code in the import section first:

from datetime import datetime, timedelta, date
You can then change the code: today = date. today ()

In this way, you have to modify two places, but it increases the burden.

Third square library

In the code that uses some third-party libraries, we will see something like this:

from lxml.html import fromstring

selector = fromstring(HTML)
But we can also write:

from lxml import html

selector = html.fromstring(HTML)
However, the following writing will cause errors:

import lxml
selector = lxml.html.fromstring(HTML)
So what is lxml. html here?

This is often the case with a particularly large third-party repository that can handle multiple types of data. For example, lxml can process both xml data and html data, so this library will be divided into sub-modules, and lxml. html module is responsible for html related data.

Try it

In the code that uses some third-party libraries, we will see something like this:


 from lxml.html import fromstring
 selector = fromstring(HTML)

But we can also write:


 But sometimes, you may see someone writing code like this: 

```python
from re import search
target = 'abc1234xyz'
search('(\d+)', target)
0

However, the following writing will cause errors:


 But sometimes, you may see someone writing code like this: 

```python
from re import search
target = 'abc1234xyz'
search('(\d+)', target)
1

So what is lxml. html here?
This is often the case with a particularly large third-party repository that can handle multiple types of data. For example, lxml can process both xml data and html data, so this library will be divided into sub-modules, and lxml. html module is responsible for html related data.

Implement multiple import methods yourself
Let's write our own code to implement these import methods.
We create a folder, DocParser, in which we create two files, main. py and util. py, with the following contents:

util. py file:


 But sometimes, you may see someone writing code like this: 

```python
from re import search
target = 'abc1234xyz'
search('(\d+)', target)
2

Now let's modify the import method of main. py by 1 (the result is the same as above):


from util import write
write()

Now, let's create a folder microsoft and add another file parse. py:


 But sometimes, you may see someone writing code like this: 

```python
from re import search
target = 'abc1234xyz'
search('(\d+)', target)
4

At this point we call it in main. py:


 But sometimes, you may see someone writing code like this: 

```python
from re import search
target = 'abc1234xyz'
search('(\d+)', target)
5

We can also use another method:


 But sometimes, you may see someone writing code like this: 

```python
from re import search
target = 'abc1234xyz'
search('(\d+)', target)
6

However, you cannot import microsoft directly

import microsoft
microsoft.parse.read

Whether you use import xxx or from xxx. yyy. zzz. www import qqq, what you import is either a module (corresponding to the file name of the. py file) or a function name, class name and variable name in an. py file.

import xxx or from xxx import yyy, you cannot import a folder name.

There may be one situation where a function name is the same as the file name, for example:

In the microsoft folder, there is an microsoft. py file with a function called microsoft, so your code can be written as:

from microsoft import microsoft`
microsoft.microsoft()
However, please note that you are still importing a module here, except that the file name of microsoft. py is exactly the same as the folder name in which it is located.

Conclusion

Whether using import or from import, the first requirement is that the code can run normally. Secondly, it is determined which scheme to choose according to code maintainability and team coding style.

If we only use one function (or constant, class) under a module and the name is not confused and highly recognizable, then from module name import function name is no problem.

If we will use multiple functions under a module, or if the function name, constant name and class name we will use may be confusing (for example, re. S, re. I), then in this case, calling import module name and then module name. xxx will make the code clearer and better maintained.

However, no matter what the circumstances, it is forbidden to use from xxx import *, it will bring you endless nightmares.


Related articles: