Python: Getting Started with setuptools
drks — Thu, 2009-04-02 06:18
References
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???)
| Attachment | Size |
|---|---|
| helloworld-1.tar.gz | 384 bytes |
RSS Feed
Post new comment