Python Dynamic Import Module

Source: Internet
Author: User

Dynamic Import Module

Directory structure:

zhangsandemacbook-air:1110 zhangsan$ tree. ├──lib│└──aa.py├──test1.py

  

There is a aa.py file under the Lib directory, where the C class has an attribute name:

Class C:    def __init__ (self):        self.name = ' Zhangsan '

There are two ways to import the AA module dynamically in the test1.py file:

The first type:

lib = __import__ (' lib.aa ') obj = LIB.AA.C () print (obj.name)

Output Result:

Zhangsan

 

Using Python __import__ built-in methods

 

The second (officially recommended method):

Import Importliblibaa = Importlib.import_module (' lib.aa ') obj = Libaa. C () print (obj.name)

Output Result:

Zhangsan

 

Using the Import_module () method of the Pythonlib module

Python Dynamic 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.