Four forms module, import and from ... import, import circulation problems, two uses the module search path, python files

Four forms module

Nike recommended book learning and data analysis algorithm, "Data analysis was performed using the python"

Module

The equivalent of application software

  1. The system comes with application software
  2. You need to download the application software
  3. Custom Software

Four forms module

  1. Built-in module: python interpreter module comes,

  2. pip, pycharm installed modules

  3. Custom Module - "Custom Code

    What is a module?

    Module - "implement the functions -" code - "

  4. Package (block)

import和from...import

Import module

  1. Open up memory space, memory space named time
  2. The time. py code is read into memory and then run the namespace
  3. A method of use of the module names by the time

from module import name method name

  1. Open up memory space, memory space named time
  2. The time. py code is read into memory and then run the namespace
  3. The sleep () reads the current filename .py file (that is, the current file), so you can directly use the method name

If you want to use multiple methods:

  1. import module from Method 1, Method 2
  2. from module import * in this way will import all modules all the way, easily conflict with the main variable name, it is best not to use.

The advantages and disadvantages of import

Pros: never and variable name conflicts

Disadvantages: each import multi-input words

The advantages and disadvantages from the module name import method name

Advantages: small input words

Disadvantages: prone to conflict

Custom module

To write a .py file is a custom module, the file name is the module name. Do python module name, and module comes with conflict.

Uses: file storing code division - "separation between the function and the function -" Let block clearer - "different modules doing different things

Import circulation problems

Simulation problems occur:

  1. It creates two files, m1.py, m2.py in the pycharm

  2. m1 wrote the following in the file

print('from m1.py')
from m2 import x 
y = 'm1'
  1. m2 wrote the following in the file
print('from m2.py')
from m1 import y

If you run m1 file will error 'can not import name' x ''

solution

Option One

Custom modules will be put into function calls, so that you will only be invoked when the module using the module, thus avoiding both programs appear logical to call each other.

#m1.py
print('from m1.py')
def func1():
    from m2 import x
    print(x)
    
y = 'm1'
#m2.py
print('from m2.py')
def func1():
    from m1 import y
    print(y)
x = 'm2'

Option II

Import module will be put to invoke the following variables (after all scheme a)

# m1.py
print('from m1.py')
y = 'm1'
from m2 import x
# m2.py
print('from m2.py')
x = 'm2'
from m1 import y

The module search path

Search Order module can be read by a module in the main program, so that the main program calls the module running for some time, during this period of time to remove this module to determine the order of the search program module path. (To prevent unnecessary errors occur, the file name after the program name in English as much as possible)

  1. RAM

  2. Internal

  3. customize

    Memory - "Built -" Custom

Two uses Python file

python file There are two purposes, one is executable file, the second is to import this as a module.

Writing good a python file may have two purposes:

  1. Script to be executed
  2. Module to be introduced

if __name__ == '__main__': using

if __name__ == '__main__': when a call module, a function or do not want the execution of code modules may also be used to only allow a certain function or block of code. __name__ is unique to each file, when the file is run as an execution file, name__ equal __ '__main__'; when the file is imported as a module file, the file name is equal __ name__, IF statement is just an ordinary judgment.

Guess you like

Origin www.cnblogs.com/ghylpb/p/11592034.html