Python imports custom modules and packages

Source: Internet
Author: User

Resources

52084144

Http://www.runoob.com/python/python-modules.html

Python Basic Concept Python module

The Python module, which is a python file that ends with a. Py, contains Python object definitions and Python statements.

Modules allow you to logically organize your Python code snippets.

Assigning the relevant code to a module will make your code better and easier to understand.

Modules can define functions, classes, and variables, and modules can also contain executable code.

Python Package

A package is a hierarchical file directory structure that defines a Python application environment consisting of a module and a sub-package, and a sub-package under a sub-package.

In a nutshell, a package is a folder, but a __init__.py file must exist under the folder, and the contents of the file can be empty. __init__.py is used to identify the current folder as a package.

Scene app import sibling directory file

If you need to introduce a file in a sibling directory, you can invoke it in the form of an import module.

Considering the two Python files in the same directory, test.py needs to call the functions in support.py, the directory structure is as follows:

|--test.py|--support.py

The code in support.py is as follows:

def print_func (PAR):    Print "  ", par   return

The code for the test.py call is as follows:

# !/usr/bin/python # -*-coding:utf-8-*- # Import Module Import  # The functions contained in the module can now be called Support.print_func ("runoob")

To import a sub-catalog file

If you need to introduce a file under a subdirectory, you can call it in the form of an import package that encapsulates a subdirectory into a package.

Consider a runoob1.py, runoob2.py, __init__.py file under the package_runoob directory, test.py the code for the Test call package, with the following directory structure:

test.pypackage_runoob|--__init__.py|--runoob1.py|--runoob2.py

__init__.py can be an empty file.

The test.py calling code is as follows:

# !/usr/bin/python # -*-coding:utf-8-*- # Import Phone Package  from Import Runoob1  from Import runoob2 runoob1 () runoob2 ()

You can also use:

# !/usr/bin/python # -*-coding:utf-8-*- # Import Phone Package Import Package_runoob.runoob1 Import package_runoob.runoob2 package_runoob.runoob1.runoob1 () package_runoob.runoob2.runoob2 ()

Python imports custom modules and packages

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.