Python Custom Module Import

Source: Internet
Author: User

In Python, the code for different functions is often written in different package, and when a package needs to import a method from another packageg (or call a function), it needs to import the module, otherwise python does not know the path of the imported module.


For example:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/88/5E/wKiom1fyUqaA8kAEAAAZ__d_uWw513.png "title=" 1.png " alt= "Wkiom1fyuqaa8kaeaaaz__d_uww513.png"/>

Below the Day4 object-oriented folder, there are three modules (backend, logic, config) and a python file (user.main.py)

The DB module and Logic module are included under the backend module. The logic module has a handle.py file with three functions defined in it:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/88/5E/wKiom1fyU3zBc166AABO-OuRB8U716.png "title=" 2.png " alt= "Wkiom1fyu3zbc166aabo-ourb8u716.png"/>


Now, user.main.py this file wants to invoke the home () function in the handle.py file below the logic module, how do I do it?


This can be written in the user.main.py file:

Import module:

From backend.logic import handle

Call Method:

Handle.home ()
#!/usr/bin/env python#-*-coding:utf-8-*-from backend.logic Import handlep1 = Handle.home () print P1 output: Welcome to Homepagenone



Attention:

Here you can import the handle file under the logic module, there is a precondition that there is a __init__ python file under the logic module, although the file content is empty, but if you delete the file, you cannot import it.

For an introduction to __init__, please refer to: http://blog.csdn.net/yxmmxy7913/article/details/4233420
















This article is from the "Zengestudy" blog, make sure to keep this source http://zengestudy.blog.51cto.com/1702365/1858622

Python Custom Module Import

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.