开发者

Initialize project layout in python?

Suppose a programmer has the following problem: he wants to start a new python project. He needs a basic layout of boilerplate stuff, like test directory, source directory, setuptools script etc.. How does he create all this stuff and layout with a single command ?

For example, paster (as suggested in one of the answers, provides you this service)

paster create 
Selected and implied templates: PasteScript#basic_package 
A basic setuptools-enabled package 

but paster is part of a tool whose main scope is not the deployment of packages. What if I want to have a template for a library, and a template for an application? How can I modify the template to add my 开发者_开发问答own personal stuff to it ?


You need something that supports templating to pull this off. The most used in the python community is pastescript.

easy_install pastescript # A one-time install
paster create

If you've already decided on the name of the package, than it's just:

paster create mypackage

If you want to customize the template, than the easiest way is to create your own python package that includes the custom template you want. Once you've installed it into your environment, you can then use this custom template as much as you want. (This is the sort of thing used by frameworks like pylons to create a template for a web application).

paster create -t libtemplate mypackage
paster create -t apptemplate mypackage

For more details on how to create templates (which consist of a mix of code and source files) take a look at: http://pythonpaste.org/script/developer.html#templates You'll notice that templates support inheritance, so that you can, e.g. just build upon the included template, or create your own, from-scratch templates.

For a good example of a customized template, you can take a look at the pylons template in source, here: Pylons Template Code

In addition, if you're not already using it, you should take a look at Ian Bicking's virtualenv. It allows you to create temporary 'virtual' environments that allow you to install python packages without using and/or conflicting with whatever system-wide packages you may have installed.

A standard setup with virtualenv and pastescript might look something like this:

mkdir mypackage && cd mypackage
virtualenv --distribute env
source env/bin/activate # 'Turns on / activates' the environment
easy_install pastescript
paster create mypackage


I'm using modern-package-template to layout my Python projects.

modern-package-template is a PasteScript template to create an initial layout for your Python projects using modern tools and practices followed in the Python community. Thus, your projects will have the following characteristics:

  • Use Distribute instead of setuptools as the BDFL himself supports it.
  • Buildout support, though you are not required to make use of it.
  • README.txt and NEWS.txt automatically included in your package metadata as long_description, thus making them appear in the PyPI page for your project.
  • Automatic script (or .exe) creation using Distribute

More info and download from pypi: http://pypi.python.org/pypi/modern-package-template


You can make your own templates. Really useful, for instance for in-house project structure standards.

Best way to start making your own is to start with an existing example and to copy/paste relevant bits from it. Suggestion: ZopeSkel as it is a quite big one with lots of examples. Browse the source code.


I've been using cookiecutter. It's written in python but can be used for any kind of project; not just python. It uses Jinja for templating and features pre and post hooks (written in python or bash) that can easily create/manage one's virtualenvs or anything else you can think of. You can store your own templates in a local directory or pull other peoples directly from the Internet and run them without storing them locally first. It seems much more versatile, simpler to use, and more useful IMHO then paster (disclosure: I've not tried paster). It also is in active development as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜