Four forms module
Nike recommended book learning and data analysis algorithm, "Data analysis was performed using the python"
Module
The equivalent of application software
- The system comes with application software
- You need to download the application software
- Custom Software
Four forms module
Built-in module: python interpreter module comes,
pip, pycharm installed modules
Custom Module - "Custom Code
What is a module?
Module - "implement the functions -" code - "
Package (block)
import和from...import
Import module
- Open up memory space, memory space named time
- The time. py code is read into memory and then run the namespace
- A method of use of the module names by the time
from module import name method name
- Open up memory space, memory space named time
- The time. py code is read into memory and then run the namespace
- 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:
- import module from Method 1, Method 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:
It creates two files, m1.py, m2.py in the pycharm
m1 wrote the following in the file
print('from m1.py')
from m2 import x
y = 'm1'
- 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)
RAM
Internal
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:
- Script to be executed
- 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.