Modules in Python (2)

Source: Internet
Author: User

1. Built-in Modules
2. Extended For example: Django
3. Custom-Defined

File
Import Demo
def read ():
Print (' My read Func ')
Demo.read ()
Print (Demo.money)

Check to see if it has been imported from Sys.modules first.
If not imported, the search module is based on the Sys.path path
When you find it, import it.
Create namespaces for this module
Execute the file and put the name in the file in the namespace



Import Sys
Print (Sys.modules.keys ())
Print (Sys.path)

Import time as T
Print (T.time ())

Import * * * for:
Oracle
Mysql
If database = = ' Oracle ':
Import Oracle as DB
elif Database = = ' MySQL ':
Import MySQL as db
Connecting to a database Db.connect
Login Authentication
Change and delete
Close the database
Like a database, you can do this by matching and selecting the as * * * and finally using a variable name.

All module imports should be written as far as possible.
Built-in Modules
Expansion modules
Custom Modules
The module will not be imported repeatedly: Sys.moudles
Where to import the module: Sys.path

Mport Module Name
Module name. The variable name does not conflict with the variable name in this file
Import module name as renamed Module name: Improved code compatibility
Import Module 1, Module 2

From module name import variable name
Use the variable name directly to complete the operation
If there is a conflict with the same variable name in this file

From Module name Import *
Put all the variable names in the module into memory
If there is a conflict with the same variable name in this file

From Module name import * and __all__ are a pair
Without this variable, all the names will be imported.
If you have all only import names from the all list

__name__
There is a variable __name__ in the module,
When we execute this module directly, __name__ = = ' __main__ '
When we execute other modules and refer to this module in other modules, the __name__ = = ' Module name ' in this module

Modules in Python (2)

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.