Python Ecosystem

TotT: 2014

Python Libraries

  • Load code with the import statement
  • Single file modules
  • Multi-module packages
  • C-code extensions

Example: Modules

In startup.py:

import worker

if __name__ == '__main__':
  # this block executes if we run startup.py directly but
  # not if this module is imported by another
  worker.run()

In worker.py:

import time

def run():
  while 1:
    print "I'm running!"
    time.sleep(1)
    

Example: Packages

Packages are collections of modules.

myproject
  |- main.py
  |- worker.py
  |- widgets
     |- __init__.py
     |- button.py
     |- listbox.py
     |- combobox.py
     

Example: Packages

In main.py:

import widgets
import worker

class MyApp(object):
  def __init__(self):
    self.combo = widgets.Combobox(['NC', 'SC', 'GA', 'VA'], 'Search')
    
  def run(self):
    worker.run()
    
if __name__ == '__main__':
  app = MyApp()
  app.run()
  

Example: Packages

In widgets/__init__.py:

from .button import Button
from .listbox import Listbox
from .combobox import Combobox

In widgets/combobox.py:

from .button import Button
from .listbox import Listbox

class Combobox(object):
  def __init__(self, items, button_label='Submit'):
    self.listbox = Listbox(items)
    self.button = Button(button_label)
    

Example: Absolute Imports

# import math module, assign to variable 'math' in this
# module's global namespace
import math

# import math module, assign to variable 'm' in this
# module's global namespace
import math as m

# import 'sin' and 'cos' from module math
# assign to 'sin' and 'cos' variables
from math import sin, cos

# import everything from math and assign to vars in this
# module's global namespace; typically considered bad form
from math import *

Example: Relative Imports

These are only possible within a package.

# import 'parse' from a peer module named 'minidom' within
# the same package as this module
from .minidom import parse

# import everything from 'etree', a peer of this module's package;
# using * for relative imports within your own package structure 
# is more acceptable than absolute * imports
from ..etree import *

Absolute Import Search

  • sys.path list governs module/package lookup
  • Interpreter seeds sys.path with common locations
  • You can modify sys.path at runtime

sys.path Defaults

  1. Directory containing the input script
  2. PYTHONPATH environment variable paths
  3. Installation-dependent directories

Python Package Index (PyPI)

pip

"A tool for installing Python packages."
- pip Homepage

pip Basics

Commands to master:

$ pip search
$ pip install
$ pip uninstall

Demo: Using pip

search, install, uninstall

Demo: Using pip

requirements.txt

  • States project dependencies
  • Can be used by pip

Example: requirements.txt

ipython==1.1.0
tornado==3.1.1
Jinja2==2.7.1
pyzmq==14.0.1
numpy==1.8.0
pandas>=0.12.0

To process this file we run:

$ pip install -r requirements.txt

site-packages

  • Python adds a system-wide site-packages folder to sys.path
  • pip and other tools install here by default
  • site-packages are global, for better or worse

virtualenv

"virtualenv is a tool to create isolated Python environments."
- virtualenv Introduction

virtualenv Basics

Commands to master:

$ virtualenv [--no-site-packages] <name>
$ <name>/bin/activate
(name)$ deactivate

Demo: Using virtualenv

virtualenv, activate, pip, deactivate

Demo: Installing Python Libraries

Other Pythons

  • CPython is the original
  • Jython is Python for the JVM
  • PyPy is an interpreter with a JIT
  • IronPython is Python for .NET / Mono
  • Cython is static compiler for Python

Review

  • Modules
  • Packages
  • sys.path
  • site-packages
  • pip
  • virtualenv
  • Other implementations