Python Module Reference and compilation

Source: Internet
Author: User

Python compilation

The following are common Python files

    • Py Source File
    • PYc the source file after compiling the file
    • PYO source files optimized for compiled files
    • PYD Python libraries written in other languages

Python is not entirely an explanatory language, it is compiled, the py file is compiled into PYC or PYO, and then executed by Python, compared to the Py file, compiled into PYc and pyo in nature and the py is not much different, but for this module loading speed increased, Does not improve the execution speed of the code, usually do not have to take the initiative to compile the PYc file, the document says as long as the import is called model.py will be compiled into PYC and then load

    • If you need a special separate compilation, you only need to use the Py_complie module, as follows
import py_compilepy_compile.compile(r‘H:\\test.py‘)compile原型:compile(file[, cfile[, dfile[, doraise]]])
    • File indicates the path of the py file that needs to be compiled
    • The CFile represents the compiled PYC file name and path, which by default is a byte code that adds C or o,o directly after the file file name
    • Dfile error message saved path
    • Doraise can be two values, true or FALSE, if true, a pycompileerror will be thrown, or if there is an error in compiling the file, there will be a fault that is displayed by default in Sys.stderr without throwing an exception

    • If you want to compile all the py files under a folder, use the following command

import compileallcompileall.compile_dir(dirpath)

Dirpath is the absolute path we want to compile.

    • If you want to compile the Pyo file

Compiling into PYO is the executionpython -O -m py_compile file.py
Where file.py is the source file we're compiling.

Module Introduction

The mutual independent reference between modules is the basic ability of any programming language. The word "module" may be different in various programming languages, but we can simply assume that a program file is a module that contains the definition of a class or method. For compiled languages, such as C # A. cs file in Java, A. java or compiled. class file can be thought of as a module (but often not as a module); It is more intuitive for interpreted languages, such as PHP. php files, which in Python are. py files can be considered a module. There are "packages" on the "module", primarily to facilitate the organization and management of modules. such as C # The compiled. dll file (but is often not described as a package, but a library), Java will be the. class packaged. jar file, PHP. Phar file (mimicking the Java package), a specially defined folder in Python is a package that can be packaged as an egg file. But for the interpretive language "package" is not compiled into a low-level language and then packaged meaning, it is more convenient to modular and management of the dependencies between modules. Each programming language has certain conventions for module and package management, and without understanding these conventions, it can be a hindrance to learning this language. Now I'd like to comb through these conventions of python.

Where Java usually needs to compile files to the topmost directory when there are modules referencing each other, the Python interpreter command has some options for module references, and the following are some of the options commonly used by the interpreter.

Options function
-D Provide debug output
-O Generate optimized bytecode (generate. pyo file)
-S Do not import the site module to automatically find the Python path at startup
-V Redundant output
-M MoD To run a module in a scripted manner
-Q opt Dust removal options
-C cmd Run system commands, typically shell internal commands
Introduction of import Statement module

Once the module is well defined, we can use the import statement to introduce the module with the following syntax:

import module1[, module2[,... moduleN]

For example, to reference the module math, you can use import math to introduce it at the very beginning of the file. When you call a function in the math module, you must refer to this:

模块名.函数名

When the interpreter encounters an import statement, the module is imported if it is in the current search path.

A search path is a list of all directories that an interpreter will search first. To import the module support.py, you need to place the command at the top of the script:

support.py:

def print_func( par ):   print"Hello : ", par   return

Reference file:

#!/usr/bin/python# -*- coding: UTF-8 -*-# 导入模块import# 现在可以调用模块里包含的函数了support.print_func("Runoob")

The result of the above example output:

Hello : Runoob

A module will only be imported once, no matter how many times you execute the import. This prevents the import module from being executed over and over again.

From...import

The FROM statement of Python lets you import a specified section from the module into the current namespace. The syntax is as follows:

fromimport name1[, name2[, ... nameN]]

For example, to import the Fibonacci function of a module FIB, use the following statement:

fromimport fibonacci

This declaration does not import the entire FIB module into the current namespace, it only introduces the Fibonacci individual in the FIB to the global symbol table of the module that executes the declaration.

from...import*

It is also possible to import all the contents of a module into the current namespace, just use the following declaration:

fromimport*

This provides an easy way to import all the items in a module. However, such statements should not be used too much.

For example, we want to introduce all the things in the math module at once, with the following statements:

fromimport*
Search Path

When you import a module, the Python parser's search order for the module location is:

    • Current directory
    • If it is not in the current directory, Python searches for each directory under the shell variable PYTHONPATH.
    • If none are found, Python looks at the default path. Under UNIX, the default path is generally/usr/local/lib/python/.

The module search path is stored in the Sys.path variable of the system module. The variable contains the current directory, Pythonpath, and the default directory determined by the installation process. Can print look at Python search path

    • Windows
in [1]:ImportSYS in [2]: Sys.path out[2]:                                                          ['',' d:\\dev\\anaconda3\\scripts ',' D:\\dev\\anaconda3\\python36.zip ',' D:\\dev\\anaconda3\\dlls ',' D:\\dev\\anaconda3\\lib ',' D:\\dev\\anaconda3 ',' D:\\dev\\anaconda3\\lib\\site-packages ',' D:\\dev\\anaconda3\\lib\\site-packages\\babel-2.5.0-py3.6.egg ',' D:\\dev\\anaconda3\\lib\\site-packages\\win32 ',' D:\\dev\\anaconda3\\lib\\site-packages\\win32\\lib ',' D:\\dev\\anaconda3\\lib\\site-packages\\pythonwin ',' D:\\dev\\anaconda3\\lib\\site-packages\\ipython\\extensions ',' C:\\users\\oneto\\.ipython ']
    • Linux
In [1import sysIn [2]: sys.pathOut[2]: ['''/home/whoami/.pyenv/versions/anaconda3/envs/my-r-env/bin''/home/whoami/.pyenv/versions/anaconda3/envs/my-r-env/lib/python36.zip''/home/whoami/.pyenv/versions/anaconda3/envs/my-r-env/lib/python3.6''/home/whoami/.pyenv/versions/anaconda3/envs/my-r-env/lib/python3.6/lib-dynload''/home/whoami/.pyenv/versions/anaconda3/envs/my-r-env/lib/python3.6/site-packages''/home/whoami/.pyenv/versions/anaconda3/envs/my-r-env/lib/python3.6/site-packages/IPython/extensions''/home/whoami/.ipython']

You can also add search paths to the Python environment through the Append method of the Sys module

in [3 ]: Sys.path.append ( Span class= "st" > '/home/whoami/djangoenv/' ) in [4 ]: Sys.pathout[4 ]: [,  '/home/whoami/.pyenv/versions/anaconda3/envs/my-r-env/ Bin ' ,  '/home/whoami/.pyenv/versions/anaconda3/envs/my-r-env/lib/python36.zip ' , Span class= "CO" > '/home/whoami/.pyenv/versions/anaconda3/envs/my-r-env/lib/python3.6 ' ,  Home/whoami/.pyenv/versions/anaconda3/envs/my-r-env/lib/python3.6/lib-dynload ' ,  '/home/ Whoami/.pyenv/versions/anaconda3/envs/my-r-env/lib/python3.6/site-packages ' ,  '/home/ Whoami/.pyenv/versions/anaconda3/envs/my-r-env/lib/python3.6/site-packages/ipython/extensions ' ,  '/home/whoami/.ipython ' ,  '/home/whoami/djangoenv/' ]  
PYTHONPATH variable

As an environment variable, PYTHONPATH consists of many directories that are installed in a single list. The syntax of the PYTHONPATH is the same as the shell variable PATH.

In Windows systems, the typical PYTHONPATH are as follows:

set PYTHONPATH=c:\python27\lib;

In UNIX systems, the typical PYTHONPATH are as follows:

set PYTHONPATH=/usr/local/lib/python
Packages in Python

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 file must exist under that folder __init__.py , and the contents of the file can be empty. __int__.pyused to identify the current folder as a package.

__init__.py├── main.py├── pkg1│   ├── A.py│   __init__.py└── pkg2    ├── B.py    __init__.py
Packages that refer to subdirectories

main.pymethods that are referenced in package2/B

# main.pyfromimport BB.someMethod()

This is a bad one, but it can solve the problem at the moment. The bad part is the subtle introduction of PKG2. The better way is relative references.

fromimport BB.someMethod()

However, if the python pkg0/main.py execution will give an error, please refer to this for reasons. The reason is that the relative reference defaults as a package to run.

Correct execution method (under Linux shell):python -m pkg0.main

This is not good enough! B in the specific line of code, can not see its source. The better Way is

fromimport t2pkg2.B.someMethod()

But the operation will be an error!

Attributeerror: ' Module ' object has no attribute ' B '

The general meaning is that the module object does not have a B attribute! It java/.net may be a bit unaccustomed from turning around!

Python introduces a module (import m) <==> introduces a m/__init__.py file, so you can m/__init__.py introduce a local module

import B-or-fromimport B

This will solve the problem of referencing the downlevel module when running the upper-level file main.py.

Cross-Catalog references

In the pkg1.__init__.py import local module

fromimport A

pkg2.BYou can then refer to the module in this modulepkg1.A

fromimport pkg1pkg1.A.someMethod()

Which . and .. somewhat resemble the meaning of the Linux file path.

Python Module Reference and compilation

Related Article

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.