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

Source: Internet
Author: User
I have just started to learn python, for import and from import I think there should be a difference, but on the internet search a circle, still did not answer my doubts.
PS: I am using the python3.5 version.
For example, DateTime:

 from datetime Import datetime Print (datetime.  Now ())

Reply content:

From import: Take the mineral water out of the car and give it to me.
Import: Give me the car.

The main question may be confused, why someone let oneself "car inside the car" take out, because this is two different car ah, inside that is toy car I think landlord easy wrong reason is to use a datetime package.
There is also a DateTime subkey with the same name in the package that has the now function under this subkey.
Because there is a child with the same name, beginners may get confused.
The landlord's question to change a package answer landlord should be able to clear point:
The following three are all correct, calling the join function of the path subkey in the OS package to add the path to a full path:

from os.path import joinprint(join('C:\\windows', 'system32'))
Two import semantics are different
importdatetimeprint(datetime.datetime.now())
You import datetime, but now is the now method of the DateTime class under the DateTime module, so you have to write Datetime.datetime.now ...

The From datetime import datetime is due to the introduction of the DateTime class from DateTime, not the module but the class, which is naturally DateTime.Now
from module import Something
This is purely a datetime named design problem of this library, too silly to look at the logic of name space to understand the general situation to use import a, when accessing B, you can pass a. The form of B.

Use from a import B sparingly, so that you can use B directly.

Avoid using the From a import *, which pollutes the namespace and does not visually display which objects you have a Toolbox, in the box there ScissorsYes PliersYes ScrewdriverWait a minute.
Now you want to screw the screw.
Then one of your actions is: from ToolboxGet it. Screwdriver, and then use ScrewdriverTo unscrew the Screw (from import).
One of the actions is: Take the Toolbox, not open it, go straight to Screw (Import)。 Import XX imports the module for functions in the module, each call requires a "module. function" to use.
From the XX import fun directly into the module of a function, direct fun () is available.
Tell you Dafa: from XX import * All functions in this module can be used directly.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.