import antigravity
DESCRIPTION
No matter how much we love writing software, there are times it makes us want to set fire to our keyboards. We'll take a look at virtualenv, pip, and other module-related technologies that make life a more enjoyable experience.TRANSCRIPT
Making life more enjoyable with Python
import antigravity
My name is Jeremy Carbaugh
sucks at technology
Python
modules
$ cat markdown.py...def markdown(text, ...):...
>>> from markdown import markdown>>> markdown(‘# Python!’)u’<h1>Python!</h1>’
congress/__init__.pycongress/house.pycongress/senate.py
>>> import congress>>> congress.senate<module ‘senate’ from ...
>>> from congress import senate>>> senate<module ‘senate’ from ...
$ cat congress/house.pydef impeach(president): ...
>>> from congress.house import impeach>>> if user.party != president.party:>>> impeach(president)
$ cat congress/__init__.pyclass Bill(object): def __init__(self, chamber, number, title): self.chamber = chamber self.number = number self.title = title
>>> from congress import Bill>>> hr45 = Bill(‘House’, 1811, ‘GREEN Act’)
GREEN Act
To authorize the President to review and approve oil and gas exploration, development, and production projects under existing Federal oil and gas leases, both onshore and offshore, and to limit administrative and judicial proceedings with respect to such projects, upon finding that such a project complies with all applicable Federal laws, and for other purposes.
sys.path
current directory$PYTHONPATH
default paths
>>> import sys>>> for path in sys.path:... print path
/usr/lib/python2.5/usr/lib/python2.5/plat-linux2/usr/lib/python2.5/lib-dynload/usr/local/lib/python2.5/site-packages/usr/lib/python2.5/site-packages/var/lib/python-support/python2.5
>>> sys.path.append(‘/path/to/lib’)>>> ‘/path/to/lib’ in sys.pathTrue
distutils
$ cat setup.pyfrom distutils.core import setupfrom serpente import __version__ setup( name='python-serpente', version=__version__, py_modules=['serpente'],)
$ python setup.py installrunning installrunning build...
$ python>>> import serpente>>> serpente.encode(2009)u’MMIX’
$ python setup.py sdistrunning sdist...creating python-serpente-0.1...creating dist...
$ ls distpython-serpente-0.1.tar.gz
PyPI
$ cat setup.pyfrom distutils.core import setupfrom serpente import __version__ setup( name='python-serpente', version=__version__, description='Roman numeral encoder and decoder', license='BSD License', author='Jeremy Carbaugh', author_email='[email protected]', url='http://github.com/jcarbaugh/python-serpente/', py_modules=['serpente'], classifiers=[ 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'License :: OSI Approved :: BSD License', 'Natural Language :: English', 'Operating System :: OS Independent', 'Programming Language :: Python', ],)
$ python setup.py registerrunning registerUsing PyPI login from ~/.pypircServer response (200): OK
setuptoolsdistutils
$ easy_install markdownSearching for markdown...Finished processing dependencies for markdown
pipsetuptoolsdistutils
$ pip install markdown...Successfully installed markdown
$ pip freeze reqs.txt$ cat reqs.txtmarkdown==1.7
$ pip install -r reqs.txt...Successfully installed
$ cat reqs.txtmarkdown==1.7-e git://.../python-serpente.git#egg=serpente
$ pip install -r reqs.txtRequirement already satisfied: markdown==1.7...Checking out serpente from git+git://......Successfully installed
So what about version conflicts?
virtualenvpip
setuptoolsdistutils
$ python virtualenv.py sandbox
$ ls sandbox/bin build include lib src
$ ls sandbox/bin/python*sandbox/bin/pythonsandbox/bin/python2.5
$ pip install -E sandbox/ markdown...Successfully installed markdown
$ cd sandbox/lib/python2.5/site-packages
$ lsmarkdown-1.7-py2.5.egg-infomarkdown.pypython_serpente-0.1-py2.5.egg-infoserpente.py
That’s too much typing.
virtualenvwrappervirtualenv
pipsetuptoolsdistutils
$ mkvirtualenv sandbox(sandbox)$
(sandbox)$ deactivate$
$ workon sandbox(sandbox)$
$ pip install -E sandbox/ markdown
$ workon sandbox(sandbox)$ pip install markdown
(sandbox)$ add2virtualenv \> /usr/local/src/django-trunk
(sandbox)$ cdsitepackages
(sandbox)$ cdvirtualenv
(sandbox)$ cat bin/postactivatesvn up /usr/local/src/django-trunk
(sandbox)$ deactivate
$ workon sandbox...Updated to revision 10844.
let’s wrap this up
$ mkvirtualenv labs
(labs)$ svn co http://.../trunk/ sunlightlabs
(labs)$ cd sunlightlabs
(labs)$ pip install -r reqs.txt
(labs)$ mate .
(labs)$ ./manage.py runserver
github.com/sunlightlabs
github.com/jcarbaugh
@jcarbaugh
The End
Creative Commons Attribution-Noncommercial-Share Alike 3.0
photo credits
sailorganymede
psmith
fotopakismo
su-lin
nostri-imago
zkorb