Understand how Python files are organized and understand the creation of source files

Source: Internet
Author: User

Referencing in Python is a very simple thing, and there are three concepts that you need to know about packages, modules, and classes. Class This is needless to say.

The module corresponds to a. py file, and then Module_name is the file. The file name after the. py file can be directly defined in the py files, with variables, functions, and classes.

Then we can think of the package as a folder containing __init__.py and a series of. py files, which is intended to differentiate between packages and ordinary strings.

Import Module_name

From package_name import module_name

From package_name Import *

How can the Python interpreter find the location of the file placed by the module when import? Python looks for modules with the following priority:

1. Current file directory

2. Environment variable Pythonpath

3. Sys.path (list type)

Sys.path is a list type, and we can add a search path to the module import by using the Insert (), append () method, such as:

Import Sys

Path = "..." # requires additional paths

Sys.path.insert (0, path)

At the import module, the top-level code for the module will be executed once. If the module is import multiple times, such as import a, import B. Where the B module itself has import a, the top-level code is executed only the first time it is import.

A module can contain some executable statements, just like a function definition. These executable statements are often used for initialization of modules. These statements are executed only when the module is imported for the first time. This is very important, some people think that these statements will be imported multiple times to execute, it is not.

When the module is imported for execution, the Python interpreter generates a. pyc file in the same directory as the module file to speed up the program startup. We know that Python is an explanatory scripting language, and. PYc is a compiled bytecode, and this work is done automatically without the programmer having to do it manually.

When should you use the From module import?

    • If you want to access the properties and methods of the module frequently, and do not want to knock the module name over and over, use the From module import.
    • If you want to selectively import certain properties and methods without wanting anything else, use the From module import.
    • If the module contains properties and methods that have the same name as one of your modules, You must use the Import module to avoid name collisions.

In addition to these situations, the rest is just a matter of style, and you'll see Python code written in two different ways.

Minimize the use of the From module import *, because it is difficult to determine where a particular function or attribute comes from, and it makes debugging and refactoring more difficult.

Other points:

Package

After creating dozens of modules, we may want to organize some of the similar files in the same folder, where the concept of the package needs to be applied. The package corresponds to the folder, and the package is similar to the module, the only thing to note is that when the folder is used as a package, the folder needs to contain the __init__.py file, primarily to avoid using the folder name as a normal string. The contents of the __init__.py can be empty, generally used for some initialization work of the package or set __all__ value, __all__ is used in the From Package-name import * This statement, all exported the defined modules.

Then we usually create a. py file (similar to Java, but Python does not have to create a class, is based on the module) as to how to organize the package will need to function

In C/c++/java, Main is the starting point for program execution, and Python has a similar operating mechanism, but in a very different way: Python uses indentation to align the execution of the Organization's code, and all non-indented code (not function definitions and class definitions) is automatically executed at load- Can be thought of as the main function of Python.

Each file (module) can write some non-indented code, and automatically when loading, in order to distinguish between the main executable file or the called file, Python introduced a variable __name__, when the file is called, the value of __name__ is the module name, when the file is executed, __ name__ is ' __main__ '. This feature provides excellent support for test-driven development, and we can write test code in each module that runs only when the module is executed directly by Python, and the code and test are perfectly combined.

Typical Python file structure:

Python Import Module method

pythonIt is relatively simple to include a module method in a subdirectory, and the key is to be able to find the path to the module file within the Sys.path.
Here are some examples of common situations:
(1) The main program and the module program are in the same directory:
such as the following program structure:
'--src
|--mod1.py
'--test1.py
If you import the module mod1 in the program test1.py, use theImportMod1 or from mod1 import *;

(2) The directory where the main program resides is the parent (or grandparent) directory of the directory where the module resides
such as the following program structure:
'--src
|--mod1.py
|--MOD2
| '--mod2.py
'--test1.py
If you import the module mod2 in the program test1.py, you need to create an empty file __init__.py file in the Mod2 folder (you can also customize the Output module interface in the file); Then use the From MOD2.MOD2 Import * or import mod2.mod2.

(3) Main program import module in upper directory or other directory (peer)
such as the following program structure:
'--src
|--mod1.py
|--MOD2
| '--mod2.py
|--Sub
| '--test2.py
'--test1.py
If you import modules Mod1 and MOD2 in the program test2.py. You first need to create a __init__.py file (same (2)) under MOD2, and you do not have to create the file under SRC. Then call the following method:
The following program execution methods are executed in the directory where the program files are located, such as test2.py is on CD sub; then execute Python test2.py
While test1.py is on CD src, then execute Python test1.py; The success of Python sub/test2.py in the SRC directory is not guaranteed.
Import Sys
Sys.path.append ("..")
Import Mod1
Import MOD2.MOD2

(4) from (3) it can be seen that the key to the import module is able to find the path of the specific module according to the value of the SYS.PATH environment variable. Here are just a few of the top three simple scenarios.

Transferred from: http://blog.chinaunix.net/uid-26602509-id-3499026.html

Understand how Python files are organized and understand the creation of source files

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.