{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Module\n", "## Name Space\n", "A module's namespace contains all the names assigned by code at the top level of the module file. \n", "Thus, nested in **def** and **class** is excluded. \n", "\n", "## Module Operations\n", "- **import**: Fetch a module\n", "- **from**: Allow importer to fetch particular names from a moudle\n", "- **imp.reload**(**reload** in Python2): Reload a module without stopping Python code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## How Imports Works\n", "Unlike C, imports are not just textual insertions of one file into another. \n", "In Python, imports do the following operations.\n", "1. Find a module file\n", "2. Compile it to byte code (if needed)\n", "3. Run the module's code to build the object it defines.\n", "\n", "Only the first time the module is imported would the above steps run. \n", "Later imports skip these steps and fetech the already loaded module.\n", "\n", "### 1. Find it\n", "Locale the module file. \n", "More about module path would be mentioned latter.\n", "\n", "### 2. Compile It (If needed)\n", "If the byte code file is older than the source file or the version is different, Python would regenerate the byte code. \n", "For different version, it has different byte code.\n", "\n", "### 3. Run it\n", "By importing, it actually runs the file's code. \n", "For example, top-level print in a module would actually print." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'IPython': ,\n", " 'IPython.core': ,\n", " 'IPython.core.alias': ,\n", " 'IPython.core.application': ,\n", " 'IPython.core.autocall': ,\n", " 'IPython.core.builtin_trap': ,\n", " 'IPython.core.compilerop': ,\n", " 'IPython.core.completer': ,\n", " 'IPython.core.completerlib': ,\n", " 'IPython.core.crashhandler': ,\n", " 'IPython.core.debugger': ,\n", " 'IPython.core.display': ,\n", " 'IPython.core.display_trap': ,\n", " 'IPython.core.displayhook': ,\n", " 'IPython.core.displaypub': ,\n", " 'IPython.core.error': ,\n", " 'IPython.core.events': ,\n", " 'IPython.core.excolors': ,\n", " 'IPython.core.extensions': ,\n", " 'IPython.core.formatters': ,\n", " 'IPython.core.getipython': ,\n", " 'IPython.core.history': ,\n", " 'IPython.core.hooks': ,\n", " 'IPython.core.inputsplitter': ,\n", " 'IPython.core.inputtransformer': ,\n", " 'IPython.core.interactiveshell': ,\n", " 'IPython.core.latex_symbols': ,\n", " 'IPython.core.logger': ,\n", " 'IPython.core.macro': ,\n", " 'IPython.core.magic': ,\n", " 'IPython.core.magic_arguments': ,\n", " 'IPython.core.magics': ,\n", " 'IPython.core.magics.auto': ,\n", " 'IPython.core.magics.basic': ,\n", " 'IPython.core.magics.code': ,\n", " 'IPython.core.magics.config': ,\n", " 'IPython.core.magics.display': ,\n", " 'IPython.core.magics.execution': ,\n", " 'IPython.core.magics.extension': ,\n", " 'IPython.core.magics.history': ,\n", " 'IPython.core.magics.logging': ,\n", " 'IPython.core.magics.namespace': ,\n", " 'IPython.core.magics.osm': ,\n", " 'IPython.core.magics.pylab': ,\n", " 'IPython.core.magics.script': ,\n", " 'IPython.core.oinspect': ,\n", " 'IPython.core.page': ,\n", " 'IPython.core.payload': ,\n", " 'IPython.core.payloadpage': ,\n", " 'IPython.core.prefilter': ,\n", " 'IPython.core.profiledir': ,\n", " 'IPython.core.pylabtools': ,\n", " 'IPython.core.release': ,\n", " 'IPython.core.shadowns': ,\n", " 'IPython.core.shellapp': ,\n", " 'IPython.core.splitinput': ,\n", " 'IPython.core.ultratb': ,\n", " 'IPython.core.usage': ,\n", " 'IPython.display': ,\n", " 'IPython.extensions': ,\n", " 'IPython.extensions.storemagic': ,\n", " 'IPython.lib': ,\n", " 'IPython.lib.backgroundjobs': ,\n", " 'IPython.lib.clipboard': ,\n", " 'IPython.lib.display': ,\n", " 'IPython.lib.pretty': ,\n", " 'IPython.lib.security': ,\n", " 'IPython.paths': ,\n", " 'IPython.terminal': ,\n", " 'IPython.terminal.debugger': ,\n", " 'IPython.terminal.embed': ,\n", " 'IPython.terminal.interactiveshell': ,\n", " 'IPython.terminal.ipapp': ,\n", " 'IPython.terminal.magics': ,\n", " 'IPython.terminal.prompts': ,\n", " 'IPython.terminal.pt_inputhooks': ,\n", " 'IPython.terminal.ptutils': ,\n", " 'IPython.terminal.shortcuts': ,\n", " 'IPython.testing': ,\n", " 'IPython.testing.skipdoctest': ,\n", " 'IPython.utils': ,\n", " 'IPython.utils.PyColorize': ,\n", " 'IPython.utils._process_common': ,\n", " 'IPython.utils._process_posix': ,\n", " 'IPython.utils._sysinfo': ,\n", " 'IPython.utils.capture': ,\n", " 'IPython.utils.colorable': ,\n", " 'IPython.utils.coloransi': ,\n", " 'IPython.utils.contexts': ,\n", " 'IPython.utils.data': ,\n", " 'IPython.utils.decorators': ,\n", " 'IPython.utils.dir2': ,\n", " 'IPython.utils.encoding': ,\n", " 'IPython.utils.frame': ,\n", " 'IPython.utils.generics': ,\n", " 'IPython.utils.importstring': ,\n", " 'IPython.utils.io': ,\n", " 'IPython.utils.ipstruct': ,\n", " 'IPython.utils.module_paths': ,\n", " 'IPython.utils.openpy': ,\n", " 'IPython.utils.path': ,\n", " 'IPython.utils.process': ,\n", " 'IPython.utils.py3compat': ,\n", " 'IPython.utils.sentinel': ,\n", " 'IPython.utils.strdispatch': ,\n", " 'IPython.utils.sysinfo': ,\n", " 'IPython.utils.syspathcontext': ,\n", " 'IPython.utils.tempdir': ,\n", " 'IPython.utils.terminal': ,\n", " 'IPython.utils.text': ,\n", " 'IPython.utils.timing': ,\n", " 'IPython.utils.tokenize2': ,\n", " 'IPython.utils.tokenutil': ,\n", " 'IPython.utils.wildcard': ,\n", " '__future__': ,\n", " '__main__': ,\n", " '__mp_main__': ,\n", " '_ast': ,\n", " '_bisect': ,\n", " '_blake2': ,\n", " '_bootlocale': ,\n", " '_bz2': ,\n", " '_codecs': ,\n", " '_collections': ,\n", " '_collections_abc': ,\n", " '_compat_pickle': ,\n", " '_compression': ,\n", " '_ctypes': ,\n", " '_curses': ,\n", " '_cython_0_25_1': ,\n", " '_datetime': ,\n", " '_frozen_importlib': ,\n", " '_frozen_importlib_external': ,\n", " '_functools': ,\n", " '_hashlib': ,\n", " '_heapq': ,\n", " '_imp': ,\n", " '_io': ,\n", " '_json': ,\n", " '_locale': ,\n", " '_lsprof': ,\n", " '_lzma': ,\n", " '_multiprocessing': ,\n", " '_opcode': ,\n", " '_operator': ,\n", " '_osx_support': ,\n", " '_pickle': ,\n", " '_posixsubprocess': ,\n", " '_random': ,\n", " '_sha3': ,\n", " '_signal': ,\n", " '_sitebuiltins': ,\n", " '_socket': ,\n", " '_sqlite3': ,\n", " '_sre': ,\n", " '_stat': ,\n", " '_string': ,\n", " '_strptime': ,\n", " '_struct': ,\n", " '_sysconfigdata_m_darwin_darwin': ,\n", " '_thread': ,\n", " '_warnings': ,\n", " '_weakref': ,\n", " '_weakrefset': ,\n", " 'abc': ,\n", " 'appdirs': ,\n", " 'argparse': ,\n", " 'array': ,\n", " 'ast': ,\n", " 'atexit': ,\n", " 'base64': ,\n", " 'bdb': ,\n", " 'binascii': ,\n", " 'bisect': ,\n", " 'builtins': ,\n", " 'bz2': ,\n", " 'cProfile': ,\n", " 'calendar': ,\n", " 'cmd': ,\n", " 'code': ,\n", " 'codecs': ,\n", " 'codeop': ,\n", " 'collections': ,\n", " 'collections.abc': ,\n", " 'concurrent': ,\n", " 'concurrent.futures': ,\n", " 'concurrent.futures._base': ,\n", " 'concurrent.futures.process': ,\n", " 'concurrent.futures.thread': ,\n", " 'contextlib': ,\n", " 'copy': ,\n", " 'copyreg': ,\n", " 'ctypes': ,\n", " 'ctypes._endian': ,\n", " 'ctypes.macholib': ,\n", " 'ctypes.macholib.dyld': ,\n", " 'ctypes.macholib.dylib': ,\n", " 'ctypes.macholib.framework': ,\n", " 'ctypes.util': ,\n", " 'curses': ,\n", " 'datetime': ,\n", " 'decorator': ,\n", " 'dis': ,\n", " 'email': ,\n", " 'email._parseaddr': ,\n", " 'email._policybase': ,\n", " 'email.base64mime': ,\n", " 'email.charset': ,\n", " 'email.encoders': ,\n", " 'email.errors': ,\n", " 'email.feedparser': ,\n", " 'email.header': ,\n", " 'email.parser': ,\n", " 'email.quoprimime': ,\n", " 'email.utils': ,\n", " 'encodings': ,\n", " 'encodings.aliases': ,\n", " 'encodings.cp437': ,\n", " 'encodings.latin_1': ,\n", " 'encodings.utf_8': ,\n", " 'enum': ,\n", " 'errno': ,\n", " 'faulthandler': ,\n", " 'fcntl': ,\n", " 'fnmatch': ,\n", " 'functools': ,\n", " 'gc': ,\n", " 'genericpath': ,\n", " 'getopt': ,\n", " 'getpass': ,\n", " 'gettext': ,\n", " 'glob': ,\n", " 'google': ,\n", " 'grp': ,\n", " 'hashlib': ,\n", " 'heapq': ,\n", " 'hmac': ,\n", " 'html': ,\n", " 'html.entities': ,\n", " 'imp': ,\n", " 'importlib': ,\n", " 'importlib._bootstrap': ,\n", " 'importlib._bootstrap_external': ,\n", " 'importlib.abc': ,\n", " 'importlib.machinery': ,\n", " 'importlib.util': ,\n", " 'inspect': ,\n", " 'io': ,\n", " 'ipykernel': ,\n", " 'ipykernel._version': ,\n", " 'ipykernel.codeutil': ,\n", " 'ipykernel.comm': ,\n", " 'ipykernel.comm.comm': ,\n", " 'ipykernel.comm.manager': ,\n", " 'ipykernel.connect': ,\n", " 'ipykernel.datapub': ,\n", " 'ipykernel.displayhook': ,\n", " 'ipykernel.heartbeat': ,\n", " 'ipykernel.iostream': ,\n", " 'ipykernel.ipkernel': ,\n", " 'ipykernel.jsonutil': ,\n", " 'ipykernel.kernelapp': ,\n", " 'ipykernel.kernelbase': ,\n", " 'ipykernel.parentpoller': ,\n", " 'ipykernel.pickleutil': ,\n", " 'ipykernel.serialize': ,\n", " 'ipykernel.zmqshell': ,\n", " 'ipython_genutils': ,\n", " 'ipython_genutils._version': ,\n", " 'ipython_genutils.encoding': ,\n", " 'ipython_genutils.importstring': ,\n", " 'ipython_genutils.path': ,\n", " 'ipython_genutils.py3compat': ,\n", " 'ipython_genutils.text': ,\n", " 'ipywidgets': ,\n", " 'ipywidgets._version': ,\n", " 'ipywidgets.widgets': ,\n", " 'ipywidgets.widgets.domwidget': ,\n", " 'ipywidgets.widgets.eventful': ,\n", " 'ipywidgets.widgets.interaction': ,\n", " 'ipywidgets.widgets.trait_types': ,\n", " 'ipywidgets.widgets.widget': ,\n", " 'ipywidgets.widgets.widget_bool': ,\n", " 'ipywidgets.widgets.widget_box': ,\n", " 'ipywidgets.widgets.widget_button': ,\n", " 'ipywidgets.widgets.widget_color': ,\n", " 'ipywidgets.widgets.widget_controller': ,\n", " 'ipywidgets.widgets.widget_float': ,\n", " 'ipywidgets.widgets.widget_image': ,\n", " 'ipywidgets.widgets.widget_int': ,\n", " 'ipywidgets.widgets.widget_layout': ,\n", " 'ipywidgets.widgets.widget_link': ,\n", " 'ipywidgets.widgets.widget_output': ,\n", " 'ipywidgets.widgets.widget_selection': ,\n", " 'ipywidgets.widgets.widget_selectioncontainer': ,\n", " 'ipywidgets.widgets.widget_string': ,\n", " 'itertools': ,\n", " 'json': ,\n", " 'json.decoder': ,\n", " 'json.encoder': ,\n", " 'json.scanner': ,\n", " 'jupyter_client': ,\n", " 'jupyter_client._version': ,\n", " 'jupyter_client.adapter': ,\n", " 'jupyter_client.blocking': ,\n", " 'jupyter_client.blocking.channels': ,\n", " 'jupyter_client.blocking.client': ,\n", " 'jupyter_client.channels': ,\n", " 'jupyter_client.channelsabc': ,\n", " 'jupyter_client.client': ,\n", " 'jupyter_client.clientabc': ,\n", " 'jupyter_client.connect': ,\n", " 'jupyter_client.jsonutil': ,\n", " 'jupyter_client.kernelspec': ,\n", " 'jupyter_client.launcher': ,\n", " 'jupyter_client.localinterfaces': ,\n", " 'jupyter_client.manager': ,\n", " 'jupyter_client.managerabc': ,\n", " 'jupyter_client.multikernelmanager': ,\n", " 'jupyter_client.session': ,\n", " 'jupyter_core': ,\n", " 'jupyter_core.paths': ,\n", " 'jupyter_core.version': ,\n", " 'keyword': ,\n", " 'linecache': ,\n", " 'locale': ,\n", " 'logging': ,\n", " 'logging.handlers': ,\n", " 'lzma': ,\n", " 'marshal': ,\n", " 'math': ,\n", " 'mimetypes': ,\n", " 'multiprocessing': ,\n", " 'multiprocessing.connection': ,\n", " 'multiprocessing.context': ,\n", " 'multiprocessing.process': ,\n", " 'multiprocessing.reduction': ,\n", " 'multiprocessing.util': ,\n", " 'ntpath': ,\n", " 'numbers': ,\n", " 'opcode': ,\n", " 'operator': ,\n", " 'optparse': ,\n", " 'os': ,\n", " 'os.path': ,\n", " 'packaging': ,\n", " 'packaging.__about__': ,\n", " 'packaging._compat': ,\n", " 'packaging._structures': ,\n", " 'packaging.markers': ,\n", " 'packaging.requirements': ,\n", " 'packaging.specifiers': ,\n", " 'packaging.version': ,\n", " 'pathlib': ,\n", " 'pdb': ,\n", " 'pexpect': ,\n", " 'pexpect.exceptions': ,\n", " 'pexpect.expect': ,\n", " 'pexpect.pty_spawn': ,\n", " 'pexpect.run': ,\n", " 'pexpect.spawnbase': ,\n", " 'pexpect.utils': ,\n", " 'pickle': ,\n", " 'pickleshare': ,\n", " 'pkg_resources': ,\n", " 'pkgutil': ,\n", " 'platform': ,\n", " 'plistlib': ,\n", " 'posix': ,\n", " 'posixpath': ,\n", " 'pprint': ,\n", " 'profile': ,\n", " 'prompt_toolkit': ,\n", " 'prompt_toolkit.application': ,\n", " 'prompt_toolkit.auto_suggest': ,\n", " 'prompt_toolkit.buffer': ,\n", " 'prompt_toolkit.buffer_mapping': ,\n", " 'prompt_toolkit.cache': ,\n", " 'prompt_toolkit.clipboard': ,\n", " 'prompt_toolkit.clipboard.base': ,\n", " 'prompt_toolkit.clipboard.in_memory': ,\n", " 'prompt_toolkit.completion': ,\n", " 'prompt_toolkit.document': ,\n", " 'prompt_toolkit.enums': ,\n", " 'prompt_toolkit.eventloop': ,\n", " 'prompt_toolkit.eventloop.base': ,\n", " 'prompt_toolkit.eventloop.callbacks': ,\n", " 'prompt_toolkit.filters': ,\n", " 'prompt_toolkit.filters.base': ,\n", " 'prompt_toolkit.filters.cli': ,\n", " 'prompt_toolkit.filters.types': ,\n", " 'prompt_toolkit.filters.utils': ,\n", " 'prompt_toolkit.history': ,\n", " 'prompt_toolkit.input': ,\n", " 'prompt_toolkit.interface': ,\n", " 'prompt_toolkit.key_binding': ,\n", " 'prompt_toolkit.key_binding.bindings': ,\n", " 'prompt_toolkit.key_binding.bindings.basic': ,\n", " 'prompt_toolkit.key_binding.bindings.completion': ,\n", " 'prompt_toolkit.key_binding.bindings.emacs': ,\n", " 'prompt_toolkit.key_binding.bindings.named_commands': ,\n", " 'prompt_toolkit.key_binding.bindings.scroll': ,\n", " 'prompt_toolkit.key_binding.bindings.utils': ,\n", " 'prompt_toolkit.key_binding.bindings.vi': ,\n", " 'prompt_toolkit.key_binding.digraphs': ,\n", " 'prompt_toolkit.key_binding.input_processor': ,\n", " 'prompt_toolkit.key_binding.manager': ,\n", " 'prompt_toolkit.key_binding.registry': ,\n", " 'prompt_toolkit.key_binding.vi_state': ,\n", " 'prompt_toolkit.keys': ,\n", " 'prompt_toolkit.layout': ,\n", " 'prompt_toolkit.layout.containers': ,\n", " 'prompt_toolkit.layout.controls': ,\n", " 'prompt_toolkit.layout.dimension': ,\n", " 'prompt_toolkit.layout.lexers': ,\n", " 'prompt_toolkit.layout.margins': ,\n", " 'prompt_toolkit.layout.menus': ,\n", " 'prompt_toolkit.layout.mouse_handlers': ,\n", " 'prompt_toolkit.layout.processors': ,\n", " 'prompt_toolkit.layout.prompt': ,\n", " 'prompt_toolkit.layout.screen': ,\n", " 'prompt_toolkit.layout.toolbars': ,\n", " 'prompt_toolkit.layout.utils': ,\n", " 'prompt_toolkit.mouse_events': ,\n", " 'prompt_toolkit.output': ,\n", " 'prompt_toolkit.reactive': ,\n", " 'prompt_toolkit.renderer': ,\n", " 'prompt_toolkit.search_state': ,\n", " 'prompt_toolkit.selection': ,\n", " 'prompt_toolkit.shortcuts': ,\n", " 'prompt_toolkit.styles': ,\n", " 'prompt_toolkit.styles.base': ,\n", " 'prompt_toolkit.styles.defaults': ,\n", " 'prompt_toolkit.styles.from_dict': ,\n", " 'prompt_toolkit.styles.from_pygments': ,\n", " 'prompt_toolkit.styles.utils': ,\n", " 'prompt_toolkit.terminal': ,\n", " 'prompt_toolkit.terminal.vt100_input': ,\n", " 'prompt_toolkit.terminal.vt100_output': ,\n", " 'prompt_toolkit.token': ,\n", " 'prompt_toolkit.utils': ,\n", " 'prompt_toolkit.validation': ,\n", " 'pstats': ,\n", " 'pty': ,\n", " 'ptyprocess': ,\n", " 'ptyprocess.ptyprocess': ,\n", " 'ptyprocess.util': ,\n", " 'pwd': ,\n", " 'pydoc': ,\n", " 'pyexpat': ,\n", " 'pyexpat.errors': ,\n", " 'pyexpat.model': ,\n", " 'pygments': ,\n", " 'pygments.filter': ,\n", " 'pygments.filters': ,\n", " 'pygments.formatter': ,\n", " 'pygments.formatters': ,\n", " 'pygments.formatters._mapping': ,\n", " 'pygments.formatters.html': ,\n", " 'pygments.lexer': ,\n", " 'pygments.lexers': ,\n", " 'pygments.lexers._mapping': ,\n", " 'pygments.lexers.python': ,\n", " 'pygments.modeline': ,\n", " 'pygments.plugin': ,\n", " 'pygments.regexopt': ,\n", " 'pygments.style': ,\n", " 'pygments.styles': ,\n", " 'pygments.styles.default': ,\n", " 'pygments.token': ,\n", " 'pygments.unistring': ,\n", " 'pygments.util': ,\n", " 'pyparsing': ,\n", " 'queue': ,\n", " 'quopri': ,\n", " 'random': ,\n", " 're': ,\n", " 'reprlib': ,\n", " 'resource': ,\n", " 'runpy': ,\n", " 'select': ,\n", " 'selectors': ,\n", " 'shlex': ,\n", " 'shutil': ,\n", " 'signal': ,\n", " 'simplegeneric': ,\n", " 'site': ,\n", " 'six': ,\n", " 'six.moves': )>,\n", " 'six.moves.urllib': )>,\n", " 'socket': ,\n", " 'sqlite3': ,\n", " 'sqlite3.dbapi2': ,\n", " 'sre_compile': ,\n", " 'sre_constants': ,\n", " 'sre_parse': ,\n", " 'stat': ,\n", " 'storemagic': ,\n", " 'string': ,\n", " 'struct': ,\n", " 'subprocess': ,\n", " 'sys': ,\n", " 'sysconfig': ,\n", " 'tarfile': ,\n", " 'tempfile': ,\n", " 'termios': ,\n", " 'textwrap': ,\n", " 'threading': ,\n", " 'time': ,\n", " 'timeit': ,\n", " 'token': ,\n", " 'tokenize': ,\n", " 'tornado': ,\n", " 'tornado.concurrent': ,\n", " 'tornado.escape': ,\n", " 'tornado.ioloop': ,\n", " 'tornado.log': ,\n", " 'tornado.platform': ,\n", " 'tornado.platform.auto': ,\n", " 'tornado.platform.interface': ,\n", " 'tornado.platform.posix': ,\n", " 'tornado.speedups': ,\n", " 'tornado.stack_context': ,\n", " 'tornado.util': ,\n", " 'traceback': ,\n", " 'traitlets': ,\n", " 'traitlets._version': ,\n", " 'traitlets.config': ,\n", " 'traitlets.config.application': ,\n", " 'traitlets.config.configurable': ,\n", " 'traitlets.config.loader': ,\n", " 'traitlets.log': ,\n", " 'traitlets.traitlets': ,\n", " 'traitlets.utils': ,\n", " 'traitlets.utils.bunch': ,\n", " 'traitlets.utils.getargspec': ,\n", " 'traitlets.utils.importstring': ,\n", " 'traitlets.utils.sentinel': ,\n", " 'tty': ,\n", " 'types': ,\n", " 'typing': ,\n", " 'typing.io': typing.io,\n", " 'typing.re': typing.re,\n", " 'unicodedata': ,\n", " 'urllib': ,\n", " 'urllib.parse': ,\n", " 'uuid': ,\n", " 'warnings': ,\n", " 'wcwidth': ,\n", " 'wcwidth.table_wide': ,\n", " 'wcwidth.table_zero': ,\n", " 'wcwidth.wcwidth': ,\n", " 'weakref': ,\n", " 'xml': ,\n", " 'xml.parsers': ,\n", " 'xml.parsers.expat': ,\n", " 'xml.parsers.expat.errors': ,\n", " 'xml.parsers.expat.model': ,\n", " 'zipfile': ,\n", " 'zipimport': ,\n", " 'zlib': ,\n", " 'zmq': ,\n", " 'zmq.backend': ,\n", " 'zmq.backend.cython': ,\n", " 'zmq.backend.cython._device': ,\n", " 'zmq.backend.cython._poll': ,\n", " 'zmq.backend.cython._version': ,\n", " 'zmq.backend.cython.constants': ,\n", " 'zmq.backend.cython.context': ,\n", " 'zmq.backend.cython.error': ,\n", " 'zmq.backend.cython.message': ,\n", " 'zmq.backend.cython.socket': ,\n", " 'zmq.backend.cython.utils': ,\n", " 'zmq.backend.select': ,\n", " 'zmq.error': ,\n", " 'zmq.eventloop': ,\n", " 'zmq.eventloop.ioloop': ,\n", " 'zmq.eventloop.zmqstream': ,\n", " 'zmq.libzmq': ,\n", " 'zmq.sugar': ,\n", " 'zmq.sugar.attrsettr': ,\n", " 'zmq.sugar.constants': ,\n", " 'zmq.sugar.context': ,\n", " 'zmq.sugar.frame': ,\n", " 'zmq.sugar.poll': ,\n", " 'zmq.sugar.socket': ,\n", " 'zmq.sugar.stopwatch': ,\n", " 'zmq.sugar.tracker': ,\n", " 'zmq.sugar.version': ,\n", " 'zmq.utils': ,\n", " 'zmq.utils.constant_names': ,\n", " 'zmq.utils.jsonapi': ,\n", " 'zmq.utils.strtypes': }" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# To see what modules are used\n", "\n", "import sys\n", "\n", "sys.modules" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Module Search Path\n", "\n", "Python searches module path in the following sequence. \n", "The latter searched modules will be overridden by the previous searched modules\n", "\n", "1. The home directory of this program\n", " - This is searched first. Be sure don't use the same name as other modules and hide them\n", "2. **PYTHONPATH**\n", "3. Standard library directories\n", " - Always searched. No need to be added to **PYTHONPATH**\n", "4. The contents of any **.pth** files\n", " - .pth (path): Path configuration files\n", " - Used for instatllation-realted feature\n", " - When such a file is present, Python will add the directories listed on each line of the file\n", " - PAth files are used more often by third-party library\n", "5. **site-packages**" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['',\n", " '/Users/LeeW/anaconda/lib/python3.6/site-packages',\n", " '/Users/LeeW/anaconda/lib/python36.zip',\n", " '/Users/LeeW/anaconda/lib/python3.6',\n", " '/Users/LeeW/anaconda/lib/python3.6/lib-dynload',\n", " '/Users/LeeW/.local/lib/python3.6/site-packages',\n", " '/Users/LeeW/anaconda/lib/python3.6/site-packages/Sphinx-1.5.1-py3.6.egg',\n", " '/Users/LeeW/anaconda/lib/python3.6/site-packages/aeosa',\n", " '/Users/LeeW/anaconda/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg',\n", " '/Users/LeeW/anaconda/lib/python3.6/site-packages/IPython/extensions',\n", " '/Users/LeeW/.ipython']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# To see the module search path\n", "\n", "import sys\n", "\n", "sys.path" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is the actual search path within Python. \n", "The empty string means current directory." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By modifying the **sys.path** list, you can modify the search path for all future imports made in a program's run. \n", "Note that such changes last only for the duration of the script." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What might imports load?\n", "\n", "Python might load the following four file types in response to an import opreation.\n", "1. Source Code (.py)\n", "2. Byte Code File (.pyc)\n", "3. Optimized Byte Code File (.pyo)\n", "4. C extension module (.so on Linux, or .dll or .pyd on Windows)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Byte Code\n", "- Python3.1 and earlier (including Python2)\n", " - Byte Code is stored in the same directory with the filename extension .pyc\n", "- Python3.2 and later\n", " - Stored in `__pycache__`\n", "\n", "Each Python version and implementation (e.g. Jython, IronPython) has its own byte code files and won't conflict." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Optimized Byte Code\n", "By add **-O** Python command-line flag. Python create **.pyo** optimized byte code files" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.0" } }, "nbformat": 4, "nbformat_minor": 1 }