5dollarwhitebox.org - theboxownsyou

  • blog
  • projects
  • articles
  • tech wiki
  • about
  • login
Home › Python Reference

RSS Feed

Python: Getting Started with setuptools

drks — Thu, 2009-04-02 06:18

References

* PEAK Dev Center


Overview

setuptools is a collection of enhancements to the Python distutils that allow you to more easily build and distribute Python packages, especially ones that have dependencies on other packages.

Now just to be clear, setuptools has a lot of features... however, so far I've really only needed two of them. Setting up console scripts, and enabling plugins to tie into an application. I expect that as I use setuptools more and more, there will be much more that I will want to add here.

You might be reading this and wonder, "Why not just follow the code examples from PEAK?". Well.. that is simple, as with many of my articles, I always try to keep the beginner in mind. And far too often, sifting through the full documentation with 100 different ways to do something gets well confusing. I'm hoping that breaking it down a bit further with some complete examples might help someone move towards adopting setuptools into their daily tool kit. That said, this article is an overview of a the basic usage of setuptools.



Before We Begin - YAHW (Yet Another Hello World)

The following code sets up a basic python command line application that will be used to add setuptools to. Create the files to follow along... or don't. The first file we look at is the __init__.py within the helloworld module. We will deal with this later when we talk about plugins, however for now just create an empty file.

Note: Before starting, you should be working out of a current directory called 'helloworld'. Therefore, from within that directly, there is another 'helloworld' directory also... the second level directory is the helloworld library that gets installed.

 $ pwd
 
/Users/you/helloworld



./helloworld/__init__.py:

# leave file empty



./helloworld/core.py:

#!/usr/bin/env python
 
class HelloWorld(object):
    def __init__(self):
        pass
 
    def say_hi(self, name=None):
        if name:
            print 'Hello World! My name is %s.' % name
        else:
            print 'Hello World!'
 
def main():
    h = HelloWorld()
    h.say_hi('johnny')
 
if __name__ == '__main__':
    main()

Note: core.py is an arbitrary name for the file, that is just a personal preference as a starting point (though is common practice by many).



So, with this simple application I quickly say hi to everyone by running it directly (via __main__):

$ python helloworld/core.py 
Hello World! My name is johnny.



Now, that's all and good... but most likely if this is a command line utility/application, it will be expected to be accessible via a common location like '/usr/bin/helloworld'. Now, technically I could simply install helloworld/core.py to /usr/bin/helloworld but that isn't what we are going for here, is it? (is it???)


AttachmentSize
helloworld-1.tar.gz384 bytes
  • Python - Automatic Script Creation with setuptools
‹ Python: Unique Username, Groupname, Email Validators for TurboGears 2.0 Form Widgets up Python - Automatic Script Creation with setuptools ›
  • Printer-friendly version

Post new comment

The content of this field is kept private and will not be shown publicly.
Input format
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, <c>, <cpp>, <diff>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <perl>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options



Who's online

There are currently 0 users and 1 guest online.
  • blog
  • projects
  • articles
  • tech wiki
  • about
  • login

5dollarwhitebox.org is not responsible in anyway for actions performed based on information found on this site.