{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Варианты иморта модулей" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'contextlib', 'g1'}" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g1 = set(globals().keys())\n", "import contextlib\n", "g2 = set(globals().keys())\n", "g2.difference(g1)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "contextlib.contextmanager" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'_1', '_2', '_i2', '_i3', 'g2', 'np'}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "g3 = set(globals().keys())\n", "g3.difference(g2)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'_3', '_i4', 'closing', 'g3'}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from contextlib import closing\n", "g4 = set(globals().keys())\n", "g4.difference(g3)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'ContextDecorator',\n", " 'ExitStack',\n", " '_4',\n", " '_i5',\n", " 'contextmanager',\n", " 'g4',\n", " 'redirect_stdout',\n", " 'suppress'}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from contextlib import *\n", "g5 = set(globals().keys())\n", "g5.difference(g4)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'AF_APPLETALK',\n", " 'AF_DECnet',\n", " 'AF_INET',\n", " 'AF_INET6',\n", " 'AF_IPX',\n", " 'AF_LINK',\n", " 'AF_ROUTE',\n", " 'AF_SNA',\n", " 'AF_SYSTEM',\n", " 'AF_UNIX',\n", " 'AF_UNSPEC',\n", " 'AI_ADDRCONFIG',\n", " 'AI_ALL',\n", " 'AI_CANONNAME',\n", " 'AI_DEFAULT',\n", " 'AI_MASK',\n", " 'AI_NUMERICHOST',\n", " 'AI_NUMERICSERV',\n", " 'AI_PASSIVE',\n", " 'AI_V4MAPPED',\n", " 'AI_V4MAPPED_CFG',\n", " 'AddressFamily',\n", " 'CAPI',\n", " 'CMSG_LEN',\n", " 'CMSG_SPACE',\n", " 'EAI_ADDRFAMILY',\n", " 'EAI_AGAIN',\n", " 'EAI_BADFLAGS',\n", " 'EAI_BADHINTS',\n", " 'EAI_FAIL',\n", " 'EAI_FAMILY',\n", " 'EAI_MAX',\n", " 'EAI_MEMORY',\n", " 'EAI_NODATA',\n", " 'EAI_NONAME',\n", " 'EAI_OVERFLOW',\n", " 'EAI_PROTOCOL',\n", " 'EAI_SERVICE',\n", " 'EAI_SOCKTYPE',\n", " 'EAI_SYSTEM',\n", " 'INADDR_ALLHOSTS_GROUP',\n", " 'INADDR_ANY',\n", " 'INADDR_BROADCAST',\n", " 'INADDR_LOOPBACK',\n", " 'INADDR_MAX_LOCAL_GROUP',\n", " 'INADDR_NONE',\n", " 'INADDR_UNSPEC_GROUP',\n", " 'IPPORT_RESERVED',\n", " 'IPPORT_USERRESERVED',\n", " 'IPPROTO_AH',\n", " 'IPPROTO_DSTOPTS',\n", " 'IPPROTO_EGP',\n", " 'IPPROTO_EON',\n", " 'IPPROTO_ESP',\n", " 'IPPROTO_FRAGMENT',\n", " 'IPPROTO_GGP',\n", " 'IPPROTO_GRE',\n", " 'IPPROTO_HELLO',\n", " 'IPPROTO_HOPOPTS',\n", " 'IPPROTO_ICMP',\n", " 'IPPROTO_ICMPV6',\n", " 'IPPROTO_IDP',\n", " 'IPPROTO_IGMP',\n", " 'IPPROTO_IP',\n", " 'IPPROTO_IPCOMP',\n", " 'IPPROTO_IPIP',\n", " 'IPPROTO_IPV4',\n", " 'IPPROTO_IPV6',\n", " 'IPPROTO_MAX',\n", " 'IPPROTO_ND',\n", " 'IPPROTO_NONE',\n", " 'IPPROTO_PIM',\n", " 'IPPROTO_PUP',\n", " 'IPPROTO_RAW',\n", " 'IPPROTO_ROUTING',\n", " 'IPPROTO_RSVP',\n", " 'IPPROTO_SCTP',\n", " 'IPPROTO_TCP',\n", " 'IPPROTO_TP',\n", " 'IPPROTO_UDP',\n", " 'IPPROTO_XTP',\n", " 'IPV6_CHECKSUM',\n", " 'IPV6_JOIN_GROUP',\n", " 'IPV6_LEAVE_GROUP',\n", " 'IPV6_MULTICAST_HOPS',\n", " 'IPV6_MULTICAST_IF',\n", " 'IPV6_MULTICAST_LOOP',\n", " 'IPV6_RECVTCLASS',\n", " 'IPV6_RTHDR_TYPE_0',\n", " 'IPV6_TCLASS',\n", " 'IPV6_UNICAST_HOPS',\n", " 'IPV6_V6ONLY',\n", " 'IP_ADD_MEMBERSHIP',\n", " 'IP_DEFAULT_MULTICAST_LOOP',\n", " 'IP_DEFAULT_MULTICAST_TTL',\n", " 'IP_DROP_MEMBERSHIP',\n", " 'IP_HDRINCL',\n", " 'IP_MAX_MEMBERSHIPS',\n", " 'IP_MULTICAST_IF',\n", " 'IP_MULTICAST_LOOP',\n", " 'IP_MULTICAST_TTL',\n", " 'IP_OPTIONS',\n", " 'IP_RECVDSTADDR',\n", " 'IP_RECVOPTS',\n", " 'IP_RECVRETOPTS',\n", " 'IP_RETOPTS',\n", " 'IP_TOS',\n", " 'IP_TTL',\n", " 'LOCAL_PEERCRED',\n", " 'MSG_CTRUNC',\n", " 'MSG_DONTROUTE',\n", " 'MSG_DONTWAIT',\n", " 'MSG_EOF',\n", " 'MSG_EOR',\n", " 'MSG_OOB',\n", " 'MSG_PEEK',\n", " 'MSG_TRUNC',\n", " 'MSG_WAITALL',\n", " 'NI_DGRAM',\n", " 'NI_MAXHOST',\n", " 'NI_MAXSERV',\n", " 'NI_NAMEREQD',\n", " 'NI_NOFQDN',\n", " 'NI_NUMERICHOST',\n", " 'NI_NUMERICSERV',\n", " 'PF_SYSTEM',\n", " 'SCM_CREDS',\n", " 'SCM_RIGHTS',\n", " 'SHUT_RD',\n", " 'SHUT_RDWR',\n", " 'SHUT_WR',\n", " 'SOCK_DGRAM',\n", " 'SOCK_RAW',\n", " 'SOCK_RDM',\n", " 'SOCK_SEQPACKET',\n", " 'SOCK_STREAM',\n", " 'SOL_IP',\n", " 'SOL_SOCKET',\n", " 'SOL_TCP',\n", " 'SOL_UDP',\n", " 'SOMAXCONN',\n", " 'SO_ACCEPTCONN',\n", " 'SO_BROADCAST',\n", " 'SO_DEBUG',\n", " 'SO_DONTROUTE',\n", " 'SO_ERROR',\n", " 'SO_KEEPALIVE',\n", " 'SO_LINGER',\n", " 'SO_OOBINLINE',\n", " 'SO_RCVBUF',\n", " 'SO_RCVLOWAT',\n", " 'SO_RCVTIMEO',\n", " 'SO_REUSEADDR',\n", " 'SO_REUSEPORT',\n", " 'SO_SNDBUF',\n", " 'SO_SNDLOWAT',\n", " 'SO_SNDTIMEO',\n", " 'SO_TYPE',\n", " 'SO_USELOOPBACK',\n", " 'SYSPROTO_CONTROL',\n", " 'SocketKind',\n", " 'SocketType',\n", " 'TCP_KEEPCNT',\n", " 'TCP_KEEPINTVL',\n", " 'TCP_MAXSEG',\n", " 'TCP_NODELAY',\n", " '_5',\n", " '_i6',\n", " 'create_connection',\n", " 'dup',\n", " 'error',\n", " 'fromfd',\n", " 'g5',\n", " 'gaierror',\n", " 'getaddrinfo',\n", " 'getdefaulttimeout',\n", " 'getfqdn',\n", " 'gethostbyaddr',\n", " 'gethostbyname',\n", " 'gethostbyname_ex',\n", " 'gethostname',\n", " 'getnameinfo',\n", " 'getprotobyname',\n", " 'getservbyname',\n", " 'getservbyport',\n", " 'has_ipv6',\n", " 'herror',\n", " 'htonl',\n", " 'htons',\n", " 'if_indextoname',\n", " 'if_nameindex',\n", " 'if_nametoindex',\n", " 'inet_aton',\n", " 'inet_ntoa',\n", " 'inet_ntop',\n", " 'inet_pton',\n", " 'ntohl',\n", " 'ntohs',\n", " 'setdefaulttimeout',\n", " 'sethostname',\n", " 'socket',\n", " 'socketpair',\n", " 'timeout'}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from socket import *\n", "g6 = set(globals().keys())\n", "g6.difference(g5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Процесс импорта" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'IPython': ,\n", " 'IPython.config': ,\n", " 'IPython.config.application': ,\n", " 'IPython.config.configurable': ,\n", " 'IPython.config.loader': ,\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.deprecated': ,\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.prompts': ,\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.external': ,\n", " 'IPython.external.decorator': ,\n", " 'IPython.external.decorator._decorator': ,\n", " 'IPython.external.path': ,\n", " 'IPython.external.path._path': ,\n", " 'IPython.external.pexpect': ,\n", " 'IPython.external.pexpect._pexpect': ,\n", " 'IPython.external.simplegeneric': ,\n", " 'IPython.external.simplegeneric._simplegeneric': ,\n", " 'IPython.html': ,\n", " 'IPython.html.nbextensions': ,\n", " 'IPython.kernel': ,\n", " 'IPython.kernel.adapter': ,\n", " 'IPython.kernel.blocking': ,\n", " 'IPython.kernel.blocking.channels': ,\n", " 'IPython.kernel.blocking.client': ,\n", " 'IPython.kernel.channels': ,\n", " 'IPython.kernel.channelsabc': ,\n", " 'IPython.kernel.client': ,\n", " 'IPython.kernel.clientabc': ,\n", " 'IPython.kernel.comm': ,\n", " 'IPython.kernel.comm.comm': ,\n", " 'IPython.kernel.comm.manager': ,\n", " 'IPython.kernel.connect': ,\n", " 'IPython.kernel.inprocess': ,\n", " 'IPython.kernel.inprocess.blocking': ,\n", " 'IPython.kernel.inprocess.channels': ,\n", " 'IPython.kernel.inprocess.client': ,\n", " 'IPython.kernel.inprocess.manager': ,\n", " 'IPython.kernel.inprocess.socket': ,\n", " 'IPython.kernel.kernelspec': ,\n", " 'IPython.kernel.launcher': ,\n", " 'IPython.kernel.manager': ,\n", " 'IPython.kernel.managerabc': ,\n", " 'IPython.kernel.multikernelmanager': ,\n", " 'IPython.kernel.zmq': ,\n", " 'IPython.kernel.zmq.datapub': ,\n", " 'IPython.kernel.zmq.displayhook': ,\n", " 'IPython.kernel.zmq.heartbeat': ,\n", " 'IPython.kernel.zmq.iostream': ,\n", " 'IPython.kernel.zmq.ipkernel': ,\n", " 'IPython.kernel.zmq.kernelapp': ,\n", " 'IPython.kernel.zmq.kernelbase': ,\n", " 'IPython.kernel.zmq.parentpoller': ,\n", " 'IPython.kernel.zmq.serialize': ,\n", " 'IPython.kernel.zmq.session': ,\n", " 'IPython.kernel.zmq.zmqshell': ,\n", " 'IPython.lib': ,\n", " 'IPython.lib.backgroundjobs': ,\n", " 'IPython.lib.clipboard': ,\n", " 'IPython.lib.deepreload': ,\n", " 'IPython.lib.display': ,\n", " 'IPython.lib.inputhook': ,\n", " 'IPython.lib.pretty': ,\n", " 'IPython.lib.security': ,\n", " 'IPython.terminal': ,\n", " 'IPython.terminal.embed': ,\n", " 'IPython.terminal.interactiveshell': ,\n", " 'IPython.terminal.ipapp': ,\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._tokenize_py3': ,\n", " 'IPython.utils.capture': ,\n", " 'IPython.utils.codeutil': ,\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.eventful': ,\n", " 'IPython.utils.frame': ,\n", " 'IPython.utils.generics': ,\n", " 'IPython.utils.importstring': ,\n", " 'IPython.utils.io': ,\n", " 'IPython.utils.ipstruct': ,\n", " 'IPython.utils.jsonutil': ,\n", " 'IPython.utils.localinterfaces': ,\n", " 'IPython.utils.log': ,\n", " 'IPython.utils.module_paths': ,\n", " 'IPython.utils.openpy': ,\n", " 'IPython.utils.path': ,\n", " 'IPython.utils.pickleshare': ,\n", " 'IPython.utils.pickleutil': ,\n", " 'IPython.utils.process': ,\n", " 'IPython.utils.py3compat': ,\n", " 'IPython.utils.rlineimpl': ,\n", " 'IPython.utils.signatures': ,\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.traitlets': ,\n", " 'IPython.utils.ulinecache': ,\n", " 'IPython.utils.version': ,\n", " 'IPython.utils.warn': ,\n", " 'IPython.utils.wildcard': ,\n", " 'IPython.utils.zmqrelated': ,\n", " 'PyObjCTools': ,\n", " '__future__': ,\n", " '__main__': ,\n", " '__mp_main__': ,\n", " '_ast': ,\n", " '_bisect': ,\n", " '_bootlocale': ,\n", " '_bz2': ,\n", " '_codecs': ,\n", " '_collections': ,\n", " '_collections_abc': ,\n", " '_compat_pickle': ,\n", " '_ctypes': ,\n", " '_curses': ,\n", " '_datetime': ,\n", " '_frozen_importlib': ,\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", " '_scproxy': ,\n", " '_sitebuiltins': ,\n", " '_socket': ,\n", " '_sqlite3': ,\n", " '_sre': ,\n", " '_ssl': ,\n", " '_stat': ,\n", " '_string': ,\n", " '_strptime': ,\n", " '_struct': ,\n", " '_sysconfigdata': ,\n", " '_thread': ,\n", " '_warnings': ,\n", " '_weakref': ,\n", " '_weakrefset': ,\n", " 'abc': ,\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", " 'dis': ,\n", " 'distutils': ,\n", " 'distutils.version': ,\n", " 'email': ,\n", " 'email._encoded_words': ,\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.iterators': ,\n", " 'email.message': ,\n", " 'email.parser': ,\n", " 'email.quoprimime': ,\n", " 'email.utils': ,\n", " 'encodings': ,\n", " 'encodings.aliases': ,\n", " 'encodings.latin_1': ,\n", " 'encodings.utf_8': ,\n", " 'enum': ,\n", " 'errno': ,\n", " 'fcntl': ,\n", " 'fnmatch': ,\n", " 'functools': ,\n", " 'gc': ,\n", " 'genericpath': ,\n", " 'getopt': ,\n", " 'getpass': ,\n", " 'gettext': ,\n", " 'glob': ,\n", " 'grp': ,\n", " 'hashlib': ,\n", " 'heapq': ,\n", " 'hmac': ,\n", " 'html': ,\n", " 'html.entities': ,\n", " 'http': ,\n", " 'http.client': ,\n", " 'imp': ,\n", " 'importlib': ,\n", " 'importlib._bootstrap': ,\n", " 'importlib.machinery': ,\n", " 'importlib.util': ,\n", " 'inspect': ,\n", " 'io': ,\n", " 'itertools': ,\n", " 'json': ,\n", " 'json.decoder': ,\n", " 'json.encoder': ,\n", " 'json.scanner': ,\n", " 'keyword': ,\n", " 'linecache': ,\n", " 'locale': ,\n", " 'logging': ,\n", " 'logging.handlers': ,\n", " 'lzma': ,\n", " 'marshal': ,\n", " 'math': ,\n", " 'mimetypes': ,\n", " 'mpl_toolkits': ,\n", " 'multiprocessing': ,\n", " 'multiprocessing.connection': ,\n", " 'multiprocessing.context': ,\n", " 'multiprocessing.process': ,\n", " 'multiprocessing.reduction': ,\n", " 'multiprocessing.util': ,\n", " 'numbers': ,\n", " 'opcode': ,\n", " 'operator': ,\n", " 'optparse': ,\n", " 'os': ,\n", " 'os.path': ,\n", " 'pdb': ,\n", " 'pickle': ,\n", " 'pkgutil': ,\n", " 'platform': ,\n", " 'posix': ,\n", " 'posixpath': ,\n", " 'pprint': ,\n", " 'profile': ,\n", " 'pstats': ,\n", " 'pty': ,\n", " 'pwd': ,\n", " 'pydoc': ,\n", " 'queue': ,\n", " 'quopri': ,\n", " 'random': ,\n", " 're': ,\n", " 'readline': ,\n", " 'readline_path': ,\n", " 'reprlib': ,\n", " 'resource': ,\n", " 'runpy': ,\n", " 'select': ,\n", " 'selectors': ,\n", " 'shlex': ,\n", " 'shutil': ,\n", " 'signal': ,\n", " 'site': ,\n", " 'socket': ,\n", " 'sqlite3': ,\n", " 'sqlite3.dbapi2': ,\n", " 'sre_compile': ,\n", " 'sre_constants': ,\n", " 'sre_parse': ,\n", " 'ssl': ,\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", " 'tty': ,\n", " 'types': ,\n", " 'urllib': ,\n", " 'urllib.error': ,\n", " 'urllib.parse': ,\n", " 'urllib.request': ,\n", " 'urllib.response': ,\n", " 'uu': ,\n", " 'uuid': ,\n", " 'warnings': ,\n", " 'weakref': ,\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.devices': ,\n", " 'zmq.devices.basedevice': ,\n", " 'zmq.devices.monitoredqueue': ,\n", " 'zmq.devices.monitoredqueuedevice': ,\n", " 'zmq.devices.proxydevice': ,\n", " 'zmq.error': ,\n", " 'zmq.eventloop': ,\n", " 'zmq.eventloop.ioloop': ,\n", " 'zmq.eventloop.zmqstream': ,\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.tracker': ,\n", " 'zmq.sugar.version': ,\n", " 'zmq.utils': ,\n", " 'zmq.utils.constant_names': ,\n", " 'zmq.utils.interop': ,\n", " 'zmq.utils.jsonapi': ,\n", " 'zmq.utils.sixcerpt': ,\n", " 'zmq.utils.strtypes': }" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import sys\n", "sys.modules" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[_frozen_importlib.BuiltinImporter,\n", " _frozen_importlib.FrozenImporter,\n", " _frozen_importlib.PathFinder]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.meta_path" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['',\n", " '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip',\n", " '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4',\n", " '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin',\n", " '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/readline',\n", " '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload',\n", " '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages',\n", " '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/IPython/extensions']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.path" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['', '/asdf', '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip', '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4', '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin', '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/readline', '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload', '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages']\r\n" ] } ], "source": [ "!PYTHONPATH=/asdf python3.4 -c 'import sys; print(sys.path)'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Создание модулей" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing module.py\n" ] } ], "source": [ "%%writefile module.py\n", "\n", "def f():\n", " pass\n", "\n", "class C:\n", " pass\n", "\n", "x = 1\n", "\n", "print('hello from module.py')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello from module.py\n" ] } ], "source": [ "import module" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['C',\n", " '__builtins__',\n", " '__cached__',\n", " '__doc__',\n", " '__file__',\n", " '__loader__',\n", " '__name__',\n", " '__package__',\n", " '__spec__',\n", " 'f',\n", " 'x']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(module)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting module.py\n" ] } ], "source": [ "%%writefile module.py\n", "\n", "def f():\n", " pass\n", "\n", "class C:\n", " pass\n", "\n", "x = 1\n", "\n", "if __name__ == '__main__':\n", " print('hello from main')\n", "else:\n", " print('hello from module.py')\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello from main\r\n" ] } ], "source": [ "!python3.4 module.py" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello from module.py\n" ] } ], "source": [ "import module" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Пакеты" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "!mkdir package" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing package/__init__.py\n" ] } ], "source": [ "%%writefile package/__init__.py\n", "\n", "print('hello from package')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing package/a.py\n" ] } ], "source": [ "%%writefile package/a.py\n", "\n", "print('hello from a')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing package/b.py\n" ] } ], "source": [ "%%writefile package/b.py\n", "\n", "print('hello from b')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello from package\n", "hello from a\n" ] } ], "source": [ "import package.a" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello from b\n" ] } ], "source": [ "import package.b" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from package import a\n", "a" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from package import *\n", "b" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting package/__init__.py\n" ] } ], "source": [ "%%writefile package/__init__.py\n", "\n", "__all__ = ['b']\n", "print('hello from package')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello from package\n", "hello from b\n" ] } ], "source": [ "from package import *" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "ename": "NameError", "evalue": "name 'a' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'a' is not defined" ] } ], "source": [ "a" ] } ], "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.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }