Import module
When the folder has a command module such custom
In its internal wrote the following code:
Print ( ' This file is called py! ' ) DEF Fuc (): Print ( ' ! This function is called ' )
We then execute the following code in the command module:
Import the Command # This file is called py!
If we repeated this many times a piece of code in this program, this file will only be introduced once the results
Import the Command # This file is called py! Import the Command Import the Command Import the Command Import the Command Import the Command
Calling method fuc command module ()
Import the Command # This file is called py! command.fuc () # This function is called!
When we write such a code, the computer will go to find this one module, and then create a namespace of this module after the find, the name of the folder are placed namespace
If we write a fuc () function in temp_py.py in:
Import the Command # This file is called py! DEF Fuc (): Print ( ' the Hello __fuc__ ' ) command.fuc () # This function is called!
Well, in fact it calls a function within the module or command
If the addition of the same variable in the command module and temp_py.py:
command module code:
Print ( ' This file is called py! ' ) Number The = 150 DEF Fuc (): Print ( ' ! This function is called ' , Number The) #number get into a memory address from which to get the value again
temp_py.py execute the code:
Import the Command # This file is called py! Number The 300 = Print (command.number) # 150 Print (Number The) # 300
When you want to import a module, the computer will first sys.modules () to find you in this module is imported in the inside
import command #这个py文件被调用! import sys print(sys.modules.keys()) #dict_keys(['sys', 'builtins', '_frozen_importlib', '_imp', '_thread', '_warnings', '_weakref', 'zipimport', '_frozen_importlib_external', '_io', 'marshal', 'nt', 'winreg', 'encodings', 'codecs', '_codecs', 'encodings.aliases', 'encodings.utf_8', '_signal', '__main__', 'encodings.latin_1', 'io', 'abc', '_abc', 'site', 'os', 'stat', '_stat', 'ntpath', 'genericpath', 'os.path', '_collections_abc', '_sitebuiltins', '_bootlocale', '_locale', 'encodings.gbk', '_codecs_cn', '_multibytecodec', 'encodings.cp437', 'command'])
command module before last we can see have been successfully imported
If we are looking for this module sys.modules () can not be found, then according to sys.path () to find the path module, if you find this module to create a namespace then the file name Chen in memory and executed If you will not be found error
Alias to the module:
Syntax: import 'module name' as 'Alias'
Import the Command COMD AS # ! py This file is called comd.fuc () # This function is called 150! Import Time t AS Print (t.time ()) # 1567359703.0011516 Print (time.time ()) # NameError: name ' time 'is not defined
Suppose there are two modules xmlreader.py and csvreader.py, which defines a function read_data (filename):. For reading some data from the file, but with a different input formats can write code to selectively pick read module, e.g.
if file_format == 'xml': import xmlreader as reader elif file_format == 'csv': import csvreader as reader data=reader.read_date(filename)
Line acquisition module:
import sys,os,pickle,shelve,json
The latter is not recommended write poor maintenance, it is recommended to write line by line at the beginning, can people at a glance
Writing order of the modules:
Built-in module (re, time, etc.), the extension module (Django, etc.), custom module
Usage from ... import ...
from time import time print(time()) #1567360779.0056956 from sys import version print(version) #3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)]
In pycharm, if you use a custom module from ... import ... a call that has a red wavy line error is subjective because pycharm think to look for this module from the root directory, but can not find the results, but in fact this module can be found in sys.path () in
If the definition of a function and fuc same name in temp_py.py, let's look at the results:
from the Command Import Fuc # This file is called py! DEF Fuc (): Print ( ' !!!!!!! ' ) # !!!!!!! Fuc ()
Also supports getting multiple methods:
import * from module name
It can name within the module call
Disadvantages: unsafe afraid of the same name
from time import * sleep = 10 sleep(0.1) #TypeError: 'int' object is not callable
__all__
只和from 模块名 import *有关
如果有被__all__约束住那么才能够执行