Package and distribute your Python code

947 views

Published on

Slide deck for my talk on packaging at PyCon India 2013

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
947
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
17
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Package and distribute your Python code

  1. 1. Package and distribute your Python code PyCon India2013,Bangalore Sanket Saurav Co-founder, CampusHash
  2. 2. Your awesome Python module is ready! What now?
  3. 3. Let's go to the Cheese Shop! No, really! PyPI == Cheese Shop ;)
  4. 4. Agenda (duh!) At the end of the talk, you can expect that your package: *Well, not really. **Guaranteed, or get your money back. @sanketsaurav Can be easily installable with pip or easy_install Can be specified as a dependency for another package Will have proper tests included* Will have proper documentation* Will make the world a better place!** · · · · · 4/20
  5. 5. Laying out your package @sanketsaurav MoonGrabber |- bin/ |- CHANGES.txt |- docs/ |- LICENSE.txt |- MANIFEST.in |- README.rst |- setup.py |- moongrabber/ |- __init__.py |- moongun.py |- battery.py |- test/ |- __init__.py |- test_moongun.py |- test_fuel.py LAYOUT 5/20
  6. 6. Breaking it up @sanketsaurav bin contains any scripts which use MoonGrabber that could be useful for the user. CHANGES.txt lists the changes to your module in successive versions. · · v<version>, <date>-- Initialrelease. CHANGES.TXT docs contains the documentation for your package should you choose to write some. And you should always do so. In LICENSE.txt, just paste your favorite license. MANIFEST.in specifies the extra files that you might want to include in your package. · · · include*.txt include*.json recursive-includedocs *.txt MANIFEST.IN 6/20
  7. 7. Breaking it up @sanketsaurav The README.txt file should be written in reStructuredText, so that PyPI can use it to generate the package's PyPI page. · =========== MoonGrabber =========== MoonGrabber is asuper-awesomemodulewhichlets youstealtheMoon. Butpromiseto putitback.Youcanuseitlikethis:: #!/usr/bin/envpython frommoongrabber importbattery, moongun if battery.is_full(): moon=moongun.grab_moon(planet="Earth") README.RST 7/20
  8. 8. Writing your setup.py @sanketsaurav fromdistutils.coreimportsetup setup( name='MoonGrabber', version='0.4.2', author='Dr. Nefario', author_email='nef@grulabs.com', packages=['moongrabber', 'moongrabber.test'], scripts=['bin/clean-gun.py','bin/recharge-battery.py'], url='http://pypi.python.org/pypi/MoonGrabber/', license='LICENSE.txt', description='StealtheMoonfromyour favoriteplanet.', long_description=open('README.rst').read(), install_requires=[ "Django >=1.1.1" ], ) SETUP.PY 8/20
  9. 9. Creating a source distribution @sanketsaurav The sdist command creates a source distribution of your package.· $pythonsetup.py sdist SHELL It creates a MANIFEST file, a dist directory in your project folder.· This directory contains your archived distribution file. In this case, it is MoonGrabber-0.4.2.tar.gz. - 9/20
  10. 10. The Cheese Shop! Register yourself with PyPI @sanketsaurav $pythonsetup.py register SHELL If you're already registered as a user on PyPI, this will register your package. If not, follow the instructions and the console and regsiter yourself. Then run the command again to register the package. · · $pythonsetup.py sdistupload SHELL This will re-build the source distribution and upload it to PyPI.· Whenever you want to update the package, you can use this command.- 10/20
  11. 11. And...you're done! Yay!
  12. 12. But there's more... Let's dig deeper
  13. 13. Add some classifiers @sanketsaurav Classifiers are used to additional metadata to your package. The classifiers argument in the setup() function takes a list of classifier strings. The list of strings can be found at pypi.python.org/pypi?%3Aaction=list_classifiers. · · · 13/20
  14. 14. More with modules @sanketsaurav packages_dir ={ '' : 'foo', 'bar' : 'boo' } SETUP() Use it when you source folder layout is different from your package layout.· py_modules =['foo', 'bar.zoo'] SETUP() Use it to include individual modules.· 14/20
  15. 15. Extension modules @sanketsaurav ext_package='pkg' SETUP() Use it to list extension packages.· ext_modules=[ Extension('foo', ['foo.c'] ) ] SETUP() Use it to add extension modules, if you may.· library_dirs=['/usr/X11R6/lib'], libraries=['X11', 'Xt'] SETUP() Use it to include external libraries.· 15/20
  16. 16. Adding more data @sanketsaurav package_data={'mypkg': ['data/*.dat']} SETUP() You can include data which is critical to your package.· data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']), ('config', ['cfg/data.cfg']), ('/etc/init.d', ['init-script'])] ) SETUP() You can add external data files too.· 16/20
  17. 17. Creating built distributions @sanketsaurav $pythonsetup.py bdist SHELL This would create an archived built distribution. This is a dumb build.· $pythonsetup.py bdist_rpm $pythonsetup.py bdist_wininst $pythonsetup.py bdist_msi SHELL You can build these binaries on the respective platforms.· 17/20
  18. 18. Do a setup.cfg Because doing setup() is too mainstream ;) @sanketsaurav [bdist_rpm] release=1 packager =Greg Ward doc_files =CHANGES.txt README.txt USAGE.txt doc/ examples/ [build_ext] inplace=1 SETUP.CFG Use this when you cannot have everything predefined. Users can edit this file to customize their installation. · 18/20
  19. 19. I love questions! LOL JK. PyPI == Cheese Shop ;)
  20. 20. Thank You! You have been a great audience! twitter @sanketsaurav www blog.sanketsaurav.com github sanketsaurav

×