#!/usr/bin/env python # coding: utf-8 # # Варианты иморта модулей # In[1]: g1 = set(globals().keys()) import contextlib g2 = set(globals().keys()) g2.difference(g1) # In[2]: contextlib.contextmanager # In[3]: import numpy as np g3 = set(globals().keys()) g3.difference(g2) # In[4]: from contextlib import closing g4 = set(globals().keys()) g4.difference(g3) # In[5]: from contextlib import * g5 = set(globals().keys()) g5.difference(g4) # In[6]: from socket import * g6 = set(globals().keys()) g6.difference(g5) # # Процесс импорта # In[1]: import sys sys.modules # In[2]: sys.meta_path # In[3]: sys.path # In[4]: get_ipython().system("PYTHONPATH=/asdf python3.4 -c 'import sys; print(sys.path)'") # # Создание модулей # In[1]: get_ipython().run_cell_magic('writefile', 'module.py', "\ndef f():\n pass\n\nclass C:\n pass\n\nx = 1\n\nprint('hello from module.py')\n") # In[2]: import module # In[3]: dir(module) # In[4]: get_ipython().run_cell_magic('writefile', 'module.py', "\ndef f():\n pass\n\nclass C:\n pass\n\nx = 1\n\nif __name__ == '__main__':\n print('hello from main')\nelse:\n print('hello from module.py')\n") # In[1]: get_ipython().system('python3.4 module.py') # In[2]: import module # # Пакеты # In[1]: get_ipython().system('mkdir package') # In[2]: get_ipython().run_cell_magic('writefile', 'package/__init__.py', "\nprint('hello from package')\n") # In[3]: get_ipython().run_cell_magic('writefile', 'package/a.py', "\nprint('hello from a')\n") # In[4]: get_ipython().run_cell_magic('writefile', 'package/b.py', "\nprint('hello from b')\n") # In[5]: import package.a # In[6]: import package.b # In[7]: from package import a a # In[8]: from package import * b # In[1]: get_ipython().run_cell_magic('writefile', 'package/__init__.py', "\n__all__ = ['b']\nprint('hello from package')\n") # In[2]: from package import * # In[3]: b # In[4]: a