Two uses to compile python file called cycle module of four forms of import module module search path issue py file package

Four forms module

What is a module

A module is a collection of some of the listed features, is a bunch of functions in a .pyfile, the module name is based on the file name taken, and the name of the file is the same

Four forms module

Custom Modules

And literally, as is their definition module, which is to write a py file in the file which is written a lot of function

Third party modules

It has been compiled as a shared library or DLL c or c ++ extensions

Built-in module

Use the c and link to the python interpreter built-in module

package

Some rank modules grouped together, give a "package" together

The benefits of using modules

You can improve development efficiency

Then is the clear division of labor, can clearly to write code, and understand the code

The calling module

import

from...import

Same point:

  1. Both modules will execute the corresponding file, the two modules will have a namespace
  2. When both calling function to find the scope of the relationship, and calls need to go to define the position has nothing to do

difference

  1. import need to add the prefix; from ... import ... no need to add the prefix

Import circulation problems

That is, when there will be another call py file in the file will be calling us with the situation error will occur.

I thought then that the package has been executed in the learning guide this process

In fact, only once lead pack

So I do not think the same as I always will guide the cycle forever

There is a way to solve the problem is to avoid circulation import import cycle

The module search path

When import modules lookup module sequence is:

  1. Start with the memory module has been imported looking
  2. Built-in module
  3. Environment variable sys.path looking in

Two uses py file

There are two uses python file a file is performed; the other is being introduced as a module.

Good to write a python file may have two purposes:

1. The script file is a whole program, to be executed (that is, there are statements executed within the package function in the file which, directly importcall the file can be executed directly execute statement file, print the file in the local call)
2. module, a bunch of files stored in the function used to be imported to use (that is stored with a number of functions written in the document, but did not execute the statement, you can use from 文件名 import 某个函数名a function call modules need to use)

There is a call will run into their own little secret when operating

If you run directly run.py will run directly aaa.py in f1()and f2(), but if we add in aaa.py in if __name__ == '__main__':this sentence, you can prevent run run.py execution f1()and f2(). Because when aaa.py executed directly, i.e., as the execution file time __name__ == '__main__'; when the module is used as running directly aaa.py __name__ == 'aaa'. It can make aaa.py have different usage under different scenarios.

# aaa.py

x = 1


def f1():
    print('from f1')


def f2():
    print('from f2')


if __name__ == '__main__':
    f1()
    f2()

Intuitive understanding:

For the purposes of the currently running program test.py, name value of the variable " main ."

If run.py called test.py, namely import test, so in terms of value for test.py __name__ variable is " the Test ", for the purposes of run.py __name__ value of the variable " main ."

The reason behind:

Sometimes we write a module (.py files) can be executed directly, but when you call it in another program, we really just want to use a function inside-written with, but not all perform again. Then we can perform part into IF name == ' main judge apos.

If the IF name == ' main ' is true, it means that we are in the direct implementation of this module, all operations must be run again; but if it is false, it means we quoted this module, only need to use it when the function will be called.

Compiled python files

(To understanding)

https://www.cnblogs.com/nickchen121/p/10802465.html

package

What is the package

Package is a form of a module, the essence is a packet containing the file of the folder .py

I understand

In fact, the package is to give us a written integration module, which can include py files, and other packages, there is a package inside the __init__.pyfile, which is equivalent to the following package of documents a catalog of the form so that the outside of the package files can directly call the package name, to the role calls package inside the module, there is I found that if there is a no import init file in, pack the documents in the time of the call if the words from the package name import method module name calling it will be performed in the init call again, if said to contain something similar to the printing of the print module init imported, it will lead to not only print what you call the file on the terminal, also called init print content, but also there is another init call in to write statements within the module package, calls will be more convenient. (Personal understanding, there is no system to learn, just looked after themselves, sum up the)

Guess you like

Origin www.cnblogs.com/xiongchao0823/p/11360525.html