Python3 three ways to import custom modules

Source: Internet
Author: User
This article mainly introduces to you about Python3 Import Custom module Three kinds of methods, the article through the sample code introduced in very detailed, to everyone's study or work has a certain reference learning value

Previous words

Recently followed Liaoche's tutorial to learn the module section. about how to customize a module, if you do not understand the first to see the basic introduction:

Module

In the development of computer programs, as the program code more and more, in a file code will be more and more long, more and more difficult to maintain.

In order to write maintainable code, we grouped many functions into separate files so that each file contained relatively few code, and many programming languages used this way of organizing code. In Python, a. py file is called a module.

What are the benefits of using modules?

The greatest benefit is that the maintainability of the code is greatly improved. Second, writing code does not have to start from scratch. When a module is written, it can be referenced elsewhere. When we write programs, we often refer to other modules, including Python built-in modules and modules from third parties.

The use of modules also avoids conflicting function names and variable names. Functions and variables of the same name can exist in different modules individually, so we do not have to think about names that conflict with other modules when we write our own modules. But also be aware that try not to conflict with the built-in function name. Click here to see all of Python's built-in functions.

You may also think, what if different people write the same module name? To avoid module name collisions, Python introduces a way to organize modules by directory, called packages.

For example, a abc.py file is a module named ABC, and a xyz.py file is a module named XYZ.

Now, assuming that our ABC and XYZ two module names conflict with the other modules, we can organize the modules through the package to avoid conflicts. The method is to select a top-level package name, such as MyCompany, to be stored in the following directory:

MyCompany
├─__init__.py
├─abc.py
└─xyz.py

Once the package is introduced, all modules will not conflict with others as long as the package name in the top layer does not conflict with others. Now, the name of the abc.py module becomes MYCOMPANY.ABC, and similarly, the xyz.py module name becomes mycompany.xyz.

Please note that each package directory will have a __init__.py file, which must exist, otherwise Python will use this directory as a normal directory instead of a package. __init__.py can be either an empty file or a Python code, because __init__.py itself is a module, and its module name is MyCompany.

Similarly, there can be multi-level catalogs, which form a multi-level package structure. For example, the following directory structure:

MyCompany
├─web
│├─__init__.py
│├─utils.py
│└─www.py
├─__init__.py
├─abc.py
└─xyz.py

The module name of the file www.py is Mycompany.web.www, and the module names of the two utils.py files are mycompany.utils and mycompany.web.utils respectively.

When you create a module, you should be careful about naming it and not conflict with the module name that comes with Python. For example, the system comes with the SYS module, its own module is not named sys.py, otherwise it will not be able to import the system's own SYS module.

Mycompany.web is also a module, please indicate the module corresponding to the. py file.

Summary

A module is a set of Python code that can be used by other modules or by other modules.

When creating your own module, be aware of:

    • Module name to follow the Python variable naming specification, do not use Chinese, special characters;

    • The module name does not conflict with the system module name, it is best to see if the system already exists the module, the check method is to execute import ABC in the python interactive environment, if successful, it indicates that the system exists this module.

Module is a good thing ah, Daniel Open source sharing many modules also speed up the development of the speed of the people

Because just get started all have a lot of details do not understand, in search of information on the Internet when the great God's tutorial is too streamlined, so I this rookie very hard to operate successfully.

So write it down here.

Begin

First, direct import

Here's a major premise. The py executable file and module belong to the same directory (parent directory), such as:

    • main.py and Pwcong modules in Python directory

    • Execute file as main.py

    • The Pwcong folder is a single module

I write the functions provided by the Pwcong module in __init__.py, which only provides a hi function:

# Pwcong Module __init__.py#-*-coding:utf-8-*-def hi (): Print ("HI")

Execute file main.py Direct Import module:

# main.py#-*-coding:utf-8-*-import Pwcongpwcong.hi ()

Then we run the main.py can see the command line window output a Hi, the first way to complete.

Use the module as: Import First-"then enter the module. Variables | functions, as in the example above Pwcong.hi ()

Second, the path of the custom module is imported through the SYS module

If the execution file and module are not in the same directory, then the direct import cannot find the custom module. Such as:

    • Execute file main.py in main directory

    • Pwcong module in Python directory

SYS module is built into Python, so the steps to import a custom module are as follows:

    1. Import the SYS module first

    2. Then use sys.path.append(path) the function to import the directory where the custom module resides

    3. Import the Custom module.

At this time main.py wrote:

# main.py#-*-coding:utf-8-*-import syssys.path.append (r "C:\Users\Pwcong\Desktop\python") Import Pwcongpwcong.hi ()

The final execution of the main.py file, the final output hi, the second way to complete.

Third, the custom module is found through the PTH file

The principle of this method is that the system variable is used, and Python scans the path of the path variable to import the module, which can be added in the system path. But I still recommend using PTH files to add.

The module and the execution file directory structure are the same:

    • Execute file main.py in main directory

    • Pwcong module in Python directory

We create a module_pwcong.pth file that contains the directory where the Pwcong module is located:

C:\Users\Pwcong\Desktop\python

Place the module_pwcong.pth file here:

Python installation directory \python35\lib\site-packages

For example, my:

Then main.py import and use the custom module:

#-*-Coding:utf-8-*-import Pwcongpwcong.hi ()

Finally executes the main.py file, can output hi, the third way to complete.




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.