Four forms module
Module
Is logically organized python code (variable, function, class, logic: the realization of a function), essentially ending .py python file (the file name is test.py, then its corresponding module name is the test)
package
Logically for component modules, is essentially a directory (must have a __init__.py
file)
Import module
The essence is to explain it again python file
Import Package
The paper is essentially performed in __init__.py
the file; If you want to import the package following modules: the package needs to be imported, then from the next package __init__.py
reintroduced in the module package file
python can be seen as a mobile phone -> pip as steward Applications -> module is the application software
1. The system comes with software - "do not need to install
2. The need to download software
3. Custom Software
1.python built-in module: python interpreter activations own time / random / os / sys
module 2.pip installed, the install jieba PIP
3. Custom Module - "custom code
4. The package (module) -" Future speak in detail
import与from..import..
import
With time, for example:
Import time
1. open memory space, the memory space designated time
2. time.py to be read into all the code, and then run
3. The method of time by the time module. Method name Use
#test.py
import time
import time as t #变量名t指向time模块的名称空间
from time import sleep
from time import sleep,localtime
Advantages: Never conflict
drawback: each import more than a couple of characters, very troublesome
from...import...
For example to time in the sleep
from time import sleep
1. open memory space, the memory space designated Time
2. time.py to be read into all the code, and then run
3. The sleep () method reads the import and the import from, and therefore may be used as the method name
# *__all__=['']
#test.py
__all__ = ['f1','f2']
def f1():
print(1)
def f2():
print(2)
def f3():
print(3)
from test import *
test.f3() #无法运行
import test
test.f3() # 3
Advantages: less playing several characters
Disadvantages: easy conflict
Circulation import
#m1.py
from m2 import y
x = 10
print('m1:',x,y)
#m2.py
from m1 import x
y = 20
print('m2:',x,y)
Process: m1.py -> m2.py -> m1.py ...
Results: X / Y has not been generated in this process
A Solution
#m1.py
x = 10
from m2 import y
print('m1:',x,y)
#m2.py
y = 20
from m1 import x
print('m2:',x,y)
# m2: 10 20
# m1: 10 20
# m2: 10 20
Process: m1.py -> 20 is 10 -> m2.py -> 20 is 10 -> m1.py -> 10 = X 20 is
Solution two
# m1.py
def f1():
from m2 import y
print(y)
x = 10
# m2.py
def f1():
from m1 import x
y = 20
# y = 20
Process: F1 () -> m2.py -> Y = 20 is
4. The path search module
Start looking for memory
from m2 import y print(y) import time time.sleep(10) # 10s内删除了m2 from m2 import y print(y)
Then look from the built-in
from time import time print(time)
And from the custom look for
x = 10
Finally, look for an environment variable
Two uses Python file
As the module file, the module can have multiple
As the executable file, only one executable file
# m1.py
x = 10
if __name__ == '__main__':
print(x)
# m2.py
import m1
if __name__ == '__main__':
The code inside the pycharm just want to make a main tab can then play out, do not need a word for word play
When running m1 __name__ == '__main__'
it will output x
Run m2 time __name__ != '__main__'(if条件不成立)
being given