Both import and from are statements, not compile-time declarations.
Thus, they can be nested in if like other statements.
Assigning an entire module object to a single name
Get copies of all names assigned at the top level of referenced module.
Note that only * works. Other pattern matching rule won't work.
In Python3, from ... import *
can only be used at the top level of a module file, not within functions.
In Python2, it's allowed, but will issue a warning
Using `from ... import ` statement might hide variables that have the same names without notice*
Thus, the following design is recommanded.
from moduel import x, y, z
from module import *
to at most once per fileWhen you must use the same name defined in two different modules
e.g.
# a.py and b.py exist in the same folder to be used as a sample
# a.py
def func():
print("This is a")
# b.py
def func():
print("This is b")
# main.py
from a import func
from b import func
func()
This is b
# main2.py
import a, b
a.func()
b.func()
This is a This is b
Another way to solve this dilemma is using as statement
from a import func as afunc
from b import func as bfunc
afunc()
bfunc()
This is a This is b
Module namespaces can be accessed via the attribute __dict__
or dir(M)
import os
for key, value in os.__dict__.items():
print(key)
print("\t", value)
SEEK_END 2 pardir .. execl <function execl at 0x01732A98> execve <built-in function execve> curdir . link <built-in function link> _execvpe <function _execvpe at 0x01732C48> sys <module 'sys' (built-in)> access <built-in function access> O_TEXT 16384 chdir <built-in function chdir> urandom <built-in function urandom> _wrap_close <class 'os._wrap_close'> devnull nul W_OK 2 spawnl <function spawnl at 0x01749A98> P_NOWAITO 3 supports_fd {<built-in function stat>} O_SHORT_LIVED 4096 O_BINARY 32768 pathsep ; O_RDWR 2 SEEK_CUR 1 execlp <function execlp at 0x01732B28> lstat <built-in function lstat> dup2 <built-in function dup2> popen <function popen at 0x01749BB8> read <built-in function read> set_inheritable <built-in function set_inheritable> defpath .;C:\bin __builtins__ {'bool': <class 'bool'>, 'EOFError': <class 'EOFError'>, 'sum': <built-in function sum>, 'enumerate': <class 'enumerate'>, 'dict': <class 'dict'>, 'Exception': <class 'Exception'>, 'ascii': <built-in function ascii>, 'map': <class 'map'>, 'DeprecationWarning': <class 'DeprecationWarning'>, 'ord': <built-in function ord>, 'IsADirectoryError': <class 'IsADirectoryError'>, 'BytesWarning': <class 'BytesWarning'>, 'list': <class 'list'>, 'exec': <built-in function exec>, 'OverflowError': <class 'OverflowError'>, 'float': <class 'float'>, 'id': <built-in function id>, 'locals': <built-in function locals>, 'BlockingIOError': <class 'BlockingIOError'>, 'FileExistsError': <class 'FileExistsError'>, 'SystemExit': <class 'SystemExit'>, 'tuple': <class 'tuple'>, 'BaseException': <class 'BaseException'>, 'hasattr': <built-in function hasattr>, 'AttributeError': <class 'AttributeError'>, '__IPYTHON__active': 'Deprecated, check for __IPYTHON__', 'all': <built-in function all>, 'max': <built-in function max>, 'InterruptedError': <class 'InterruptedError'>, 'oct': <built-in function oct>, 'GeneratorExit': <class 'GeneratorExit'>, 'NotImplementedError': <class 'NotImplementedError'>, 'NotADirectoryError': <class 'NotADirectoryError'>, 'set': <class 'set'>, 'eval': <built-in function eval>, 'NameError': <class 'NameError'>, 'ConnectionResetError': <class 'ConnectionResetError'>, 'EnvironmentError': <class 'OSError'>, 'Ellipsis': Ellipsis, 'repr': <built-in function repr>, 'IOError': <class 'OSError'>, 'help': Type help() for interactive help, or help(object) for help about object., 'staticmethod': <class 'staticmethod'>, 'FloatingPointError': <class 'FloatingPointError'>, 'FutureWarning': <class 'FutureWarning'>, 'BrokenPipeError': <class 'BrokenPipeError'>, 'IndexError': <class 'IndexError'>, 'SystemError': <class 'SystemError'>, 'ConnectionRefusedError': <class 'ConnectionRefusedError'>, 'Warning': <class 'Warning'>, 'RuntimeWarning': <class 'RuntimeWarning'>, 'BufferError': <class 'BufferError'>, 'slice': <class 'slice'>, 'license': See https://www.python.org/psf/license/, 'object': <class 'object'>, 'UnboundLocalError': <class 'UnboundLocalError'>, 'len': <built-in function len>, 'any': <built-in function any>, 'UnicodeEncodeError': <class 'UnicodeEncodeError'>, 'ResourceWarning': <class 'ResourceWarning'>, 'UserWarning': <class 'UserWarning'>, 'None': None, '__package__': '', 'reversed': <class 'reversed'>, 'frozenset': <class 'frozenset'>, 'FileNotFoundError': <class 'FileNotFoundError'>, 'dir': <built-in function dir>, 'callable': <built-in function callable>, 'hex': <built-in function hex>, '__doc__': "Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is the `nil' object; Ellipsis represents `...' in slices.", 'type': <class 'type'>, 'setattr': <built-in function setattr>, 'abs': <built-in function abs>, 'zip': <class 'zip'>, 'SyntaxWarning': <class 'SyntaxWarning'>, '__name__': 'builtins', 'str': <class 'str'>, 'memoryview': <class 'memoryview'>, '__import__': <built-in function __import__>, 'compile': <built-in function compile>, 'vars': <built-in function vars>, 'SyntaxError': <class 'SyntaxError'>, 'open': <built-in function open>, 'property': <class 'property'>, 'NotImplemented': NotImplemented, 'AssertionError': <class 'AssertionError'>, 'min': <built-in function min>, 'ZeroDivisionError': <class 'ZeroDivisionError'>, 'int': <class 'int'>, 'isinstance': <built-in function isinstance>, 'ConnectionAbortedError': <class 'ConnectionAbortedError'>, 'format': <built-in function format>, 'globals': <built-in function globals>, 'hash': <built-in function hash>, 'copyright': Copyright (c) 2001-2015 Python Software Foundation. All Rights Reserved. Copyright (c) 2000 BeOpen.com. All Rights Reserved. Copyright (c) 1995-2001 Corporation for National Research Initiatives. All Rights Reserved. Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam. All Rights Reserved., 'super': <class 'super'>, 'UnicodeDecodeError': <class 'UnicodeDecodeError'>, 'PermissionError': <class 'PermissionError'>, 'TimeoutError': <class 'TimeoutError'>, 'ConnectionError': <class 'ConnectionError'>, 'sorted': <built-in function sorted>, 'print': <built-in function print>, 'round': <built-in function round>, 'MemoryError': <class 'MemoryError'>, 'credits': Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands for supporting Python development. See www.python.org for more information., 'ChildProcessError': <class 'ChildProcessError'>, 'ProcessLookupError': <class 'ProcessLookupError'>, 'False': False, 'getattr': <built-in function getattr>, 'TabError': <class 'TabError'>, 'ArithmeticError': <class 'ArithmeticError'>, 'dreload': <function reload at 0x02B23AE0>, 'bin': <built-in function bin>, 'chr': <built-in function chr>, 'iter': <built-in function iter>, '__IPYTHON__': True, 'True': True, 'IndentationError': <class 'IndentationError'>, 'LookupError': <class 'LookupError'>, 'ImportWarning': <class 'ImportWarning'>, 'OSError': <class 'OSError'>, 'WindowsError': <class 'OSError'>, 'ImportError': <class 'ImportError'>, 'delattr': <built-in function delattr>, 'get_ipython': <bound method ZMQInteractiveShell.get_ipython of <IPython.kernel.zmq.zmqshell.ZMQInteractiveShell object at 0x02B0B390>>, '__build_class__': <built-in function __build_class__>, 'bytes': <class 'bytes'>, 'KeyError': <class 'KeyError'>, 'ValueError': <class 'ValueError'>, 'ReferenceError': <class 'ReferenceError'>, 'bytearray': <class 'bytearray'>, 'UnicodeWarning': <class 'UnicodeWarning'>, 'PendingDeprecationWarning': <class 'PendingDeprecationWarning'>, '__spec__': ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>), 'divmod': <built-in function divmod>, 'StopIteration': <class 'StopIteration'>, 'range': <class 'range'>, 'next': <built-in function next>, 'RuntimeError': <class 'RuntimeError'>, 'filter': <class 'filter'>, '__debug__': True, 'UnicodeTranslateError': <class 'UnicodeTranslateError'>, 'TypeError': <class 'TypeError'>, 'complex': <class 'complex'>, 'pow': <built-in function pow>, 'UnicodeError': <class 'UnicodeError'>, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, 'classmethod': <class 'classmethod'>, 'issubclass': <built-in function issubclass>, 'input': <bound method IPythonKernel.raw_input of <IPython.kernel.zmq.ipkernel.IPythonKernel object at 0x02B0B3B0>>, 'KeyboardInterrupt': <class 'KeyboardInterrupt'>} rename <built-in function rename> device_encoding <built-in function device_encoding> P_WAIT 0 O_CREAT 256 spawnve <built-in function spawnve> linesep execv <built-in function execv> name nt spawnv <built-in function spawnv> listdir <built-in function listdir> get_inheritable <built-in function get_inheritable> __all__ ['altsep', 'curdir', 'pardir', 'sep', 'pathsep', 'linesep', 'defpath', 'name', 'path', 'devnull', 'SEEK_SET', 'SEEK_CUR', 'SEEK_END', 'fsencode', 'fsdecode', 'get_exec_path', 'fdopen', 'popen', 'extsep', '_exit', 'F_OK', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'R_OK', 'TMP_MAX', 'W_OK', 'X_OK', 'abort', 'access', 'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'device_encoding', 'dup', 'dup2', 'environ', 'error', 'execv', 'execve', 'fstat', 'fsync', 'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'link', 'listdir', 'lseek', 'lstat', 'mkdir', 'open', 'pipe', 'putenv', 'read', 'readlink', 'remove', 'rename', 'replace', 'rmdir', 'set_handle_inheritable', 'set_inheritable', 'spawnv', 'spawnve', 'startfile', 'stat', 'stat_float_times', 'stat_result', 'statvfs_result', 'strerror', 'symlink', 'system', 'terminal_size', 'times', 'times_result', 'umask', 'uname_result', 'unlink', 'urandom', 'utime', 'waitpid', 'write', 'makedirs', 'removedirs', 'renames', 'walk', 'execl', 'execle', 'execlp', 'execlpe', 'execvp', 'execvpe', 'getenv', 'supports_bytes_environ', 'spawnl', 'spawnle'] getppid <built-in function getppid> write <built-in function write> isatty <built-in function isatty> stat_float_times <built-in function stat_float_times> execvpe <function execvpe at 0x01732C00> get_exec_path <function get_exec_path at 0x01732C90> MutableMapping <class 'collections.abc.MutableMapping'> fstat <built-in function fstat> remove <built-in function remove> supports_effective_ids set() get_terminal_size <built-in function get_terminal_size> chmod <built-in function chmod> __package__ O_RANDOM 16 R_OK 4 makedirs <function makedirs at 0x01717B70> X_OK 1 O_WRONLY 1 __doc__ OS routines for NT or Posix depending on what system we're on. This exports: - all functions from posix, nt or ce, e.g. unlink, stat, etc. - os.path is either posixpath or ntpath - os.name is either 'posix', 'nt' or 'ce'. - os.curdir is a string representing the current directory ('.' or ':') - os.pardir is a string representing the parent directory ('..' or '::') - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\') - os.extsep is the extension separator (always '.') - os.altsep is the alternate pathname separator (None or '/') - os.pathsep is the component separator used in $PATH etc - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n') - os.defpath is the default search path for executables - os.devnull is the file path of the null device ('/dev/null', etc.) Programs that import and use 'os' stand a better chance of being portable between different platforms. Of course, they must then only use functions that are defined by all platforms (e.g., unlink and opendir), and leave all pathname manipulation to os.path (e.g., split and join). cpu_count <built-in function cpu_count> _putenv <built-in function putenv> O_TEMPORARY 64 terminal_size <class 'os.terminal_size'> P_DETACH 4 __name__ os dup <built-in function dup> getlogin <built-in function getlogin> fsdecode <function _fscodec.<locals>.fsdecode at 0x01749B28> get_handle_inheritable <built-in function get_handle_inheritable> close <built-in function close> altsep / utime <built-in function utime> open <built-in function open> execlpe <function execlpe at 0x01732B70> fdopen <function fdopen at 0x01749C00> supports_bytes_environ False unlink <built-in function unlink> execle <function execle at 0x01732AE0> system <built-in function system> renames <function renames at 0x01732A08> spawnle <function spawnle at 0x01749B70> O_RDONLY 0 stat_result <class 'os.stat_result'> fsencode <function _fscodec.<locals>.fsencode at 0x01749AE0> removedirs <function removedirs at 0x017329C0> uname_result <class 'nt.uname_result'> _unsetenv <function <lambda> at 0x01732CD8> abort <built-in function abort> umask <built-in function umask> _exit <built-in function _exit> error <class 'OSError'> getpid <built-in function getpid> O_SEQUENTIAL 32 walk <function walk at 0x01732A50> readlink <built-in function readlink> supports_dir_fd set() __file__ C:\Users\smszw\Anaconda3\lib\os.py F_OK 0 P_OVERLAY 2 times_result <class 'nt.times_result'> set_handle_inheritable <built-in function set_handle_inheritable> kill <built-in function kill> getcwd <built-in function getcwd> fsync <built-in function fsync> pipe <built-in function pipe> TMP_MAX 32767 statvfs_result <class 'os.statvfs_result'> O_EXCL 1024 getcwdb <built-in function getcwdb> extsep . st <module 'stat' from 'C:\\Users\\smszw\\Anaconda3\\lib\\stat.py'> symlink <built-in function symlink> supports_follow_symlinks {<built-in function stat>} sep \ rmdir <built-in function rmdir> closerange <built-in function closerange> stat <built-in function stat> errno <module 'errno' (built-in)> O_NOINHERIT 128 __loader__ <_frozen_importlib.SourceFileLoader object at 0x0171E650> P_NOWAIT 1 environ environ({'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_60', 'FP_NO_HOST_CHECK': 'NO', 'USERDOMAIN': 'user-PC', 'PATH': 'C:\\ProgramData\\Oracle\\Java\\javapath;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Git\\cmd;C:\\Program Files\\Microsoft SQL Server\\110\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\110\\DTS\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\110\\Tools\\Binn\\ManagementStudio\\;C:\\Program Files\\nodejs\\;C:\\Program Files\\Brackets\\command;%USERPROFILE%\\.dnx\\bin;C:\\Program Files\\Microsoft DNX\\Dnvm\\;C:\\Users\\smszw\\Anaconda2\\Library\\bin;C:\\Program Files\\IronPython 2.7;C:\\Users\\smszw\\Anaconda3;C:\\Users\\smszw\\Anaconda3\\Scripts; C:\\apache-ant-1.9.6\\bin;C:\\Users\\smszw\\AppData\\Roaming\\npm', 'CLICOLOR': '1', 'HOMEPATH': '\\Users\\smszw', 'PAGER': 'cat', 'COMPUTERNAME': 'USER-PC', 'LOGONSERVER': '\\\\USER-PC', 'IPY_INTERRUPT_EVENT': '956', 'SESSIONNAME': 'Console', 'PROMPT': '$P$G', 'PROCESSOR_IDENTIFIER': 'x86 Family 6 Model 15 Stepping 6, GenuineIntel', 'COMSPEC': 'C:\\Windows\\system32\\cmd.exe', 'PROCESSOR_REVISION': '0f06', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC', 'VS140COMNTOOLS': 'C:\\Program Files\\Microsoft Visual Studio 14.0\\Common7\\Tools\\', 'NUMBER_OF_PROCESSORS': '2', 'TEMP': 'C:\\Users\\smszw\\AppData\\Local\\Temp', 'VBOX_MSI_INSTALL_PATH': 'C:\\Program Files\\Oracle\\VirtualBox\\', 'USERPROFILE': 'C:\\Users\\smszw', 'WINDOWS_TRACING_FLAGS': '3', 'JPY_PARENT_PID': '904', 'USERNAME': 'smszw', 'OS': 'Windows_NT', 'SYSTEMDRIVE': 'C:', 'SYSTEMROOT': 'C:\\Windows', 'JPY_INTERRUPT_EVENT': '956', 'APPDATA': 'C:\\Users\\smszw\\AppData\\Roaming', 'PUBLIC': 'C:\\Users\\Public', 'PROGRAMFILES': 'C:\\Program Files', 'PROCESSOR_ARCHITECTURE': 'x86', 'TERM': 'xterm-color', 'WINDOWS_TRACING_LOGFILE': 'C:\\BVTBin\\Tests\\installpackage\\csilogfile.log', 'COMMONPROGRAMFILES': 'C:\\Program Files\\Common Files', 'TMP': 'C:\\Users\\smszw\\AppData\\Local\\Temp', 'GIT_PAGER': 'cat', 'WINDIR': 'C:\\Windows', 'LOCALAPPDATA': 'C:\\Users\\smszw\\AppData\\Local', 'PROCESSOR_LEVEL': '6', 'ASL.LOG': 'Destination=file', 'PROGRAMDATA': 'C:\\ProgramData', 'ALLUSERSPROFILE': 'C:\\ProgramData', 'HOMEDRIVE': 'C:', 'PSMODULEPATH': 'C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\;C:\\Program Files\\Microsoft SQL Server\\110\\Tools\\PowerShell\\Modules\\'}) _exists <function _exists at 0x01717A98> __cached__ C:\Users\smszw\Anaconda3\lib\__pycache__\os.cpython-34.pyc SEEK_SET 0 times <built-in function times> replace <built-in function replace> getenv <function getenv at 0x017499C0> strerror <built-in function strerror> execvp <function execvp at 0x01732BB8> _Environ <class 'os._Environ'> lseek <built-in function lseek> O_TRUNC 512 O_APPEND 8 mkdir <built-in function mkdir> path <module 'ntpath' from 'C:\\Users\\smszw\\Anaconda3\\lib\\ntpath.py'> putenv <built-in function putenv> __spec__ ModuleSpec(name='os', loader=<_frozen_importlib.SourceFileLoader object at 0x0171E650>, origin='C:\\Users\\smszw\\Anaconda3\\lib\\os.py') startfile <built-in function startfile> _get_exports_list <function _get_exports_list at 0x01717AE0> waitpid <built-in function waitpid>
reload currently only works on modules written in Python.
import os
from imp import reload # Python3
reload(os)
<module 'os' from 'C:\\Users\\smszw\\Anaconda3\\lib\\os.py'>
reload expects a module object.
Thus, the module you want to reload must first be successfully imported..
reload change the module object in-place.
Thus, every reference to it will be updated by a reload
from won't be affected by reload
reload applys to a single module, not recursive
reload returns the module object
reload also accepts a dotted path name