Initialize project layout in python?

后端 未结 4 536
情书的邮戳
情书的邮戳 2020-12-28 10:38

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,

4条回答
  •  既然无缘
    2020-12-28 11:24

    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
    

提交回复
热议问题