Import instructions for Python packages

Source: Internet
Author: User


Import module fromimport module

What is the difference between the above code?

From module Import * This kind of import imagination and the code inside the module is copied into the current module (that is, the location of the statement), you can directly use the module inside the variable name and method, etc.
So this approach does not take effect if the variables and methods have the same name as the current module.

Import Module If you use variables or methods inside, you must prefix the module name. The essence of import is to explain the module in advance and assign it to a module with the same name
variables, so that's why you use modules or variables inside to add the module name.

"Package" can be imported because it has a __init__.py file, so the process of importing the package is to explain the process of the file. Before executing this file, however, it is the first to search
Find out where this bag is? Just go sys.path the search path defined in this definition. This first contains the current pavement, the project's Sources roots directory, and other system-built paths. So Sys.path
This is the list, and if you sometimes can't find the package you imported, you can add the package path to the Sys.path list. The first element in a Sys.path list is always the current path.

Although the import Package and Import module are import statements but the logic behind it is not, the import package is the execution package under the __init__.py, but the import module is to copy the contents of the module to the same name of the module
becomes a variable.

Look at the following example

Import instructions for Python packages

Related Article

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.