Python Import Module

Source: Internet
Author: User

1, the definition of the module:
Module definition: Used to logically organize Python code (variables, functions, classes, logic; The purpose is to implement a function), which is essentially a Python file at the end of the. Py.

Add:
Package definition: Used from the logical organization module, the essence is a directory (must have a __init__.py file)
2, import method:

Ways to import modules:
1.import module_name Importing a module
Prefix is required when using Module_name.name
2.import Module_name,module2_name Importing multiple modules
3.from module_name Import * Imports all variables inside the Module_name module, not recommended!
4.from module_name Import name only imports Module_name inside the name explained again.
When using the direct name call can be
the difference between import and from
1.import Time Module_name.logger ()
2.from time logger () direct execution; From is equal to the copy of all the code, execute it.

interpreter: The thing to do is to specify a variable and point to a memory address, placing the contents of the code inside the memory address.

How to import packages:
The name of the import package

  Multi-level Catalog Import method:
To view the current path:
Import Sys
Sys.path
Find Parent and Directory
Import OS
Os.path.abspath (__file__)
Abspath () absolute path to the current file
__file__ filename of the current file
Os.path.dirname (Os.path.abspath (__file__))
DirName () Get directory Name
Os.path.dirname (Os.path.dirname (Os.path.abspath (__file__)))
Path Join
Sys.path.append () This will add the path to the end of the Python system path list, which is not conducive to quick lookups.
Sys.path.insert () This will add the path to the front of the Python system path list.
Example:
Import Sys,os
Print (Sys.path)
A = Os.path.dirname (Os.path.dirname (Os.path.abspath (__file__)))
Sys.path.insert (a)
Print (Sys.path)
from xxx import xxx

3. Import Essence(Path search and search path)

   the nature of the import module: Just explain the python file again.
Analysis: Import module_name--"is to explain module_name.py--" first to find module_name.py location--"Find the system path Sys.path, the current path first.

   the nature of the import package:is to execute the __init__.py file under the package

Import the test1.py file below the package
1. Modify the __init__.py file to import the py file below the package
From. Imprt test1
#import test1
2. Import the module test1.py file below the package in the Py file

4. Import Optimization
For example:
Before optimization:
module_test.pydefTest ():Print("In the test!") test.pyImportmodule_testdeflogger (): Module_test.test ()Print("In the logger!")            defsearch (): Module_test.test ()Print("In the search!")
View Code  
    After optimization:
module_test.pydefTest ():Print("In the test!") test.py fromModule_testImportTestdeflogger (): Test ()Print("In the logger!")            defsearch (): Test ()Print("In the search!")
View Code
   Attention:
If the method of importing the module is in conflict with the current environment, you can set an alias when importing:
From module_test import test as test01

Python Import Module

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.