假设您想要用Python开发一个重要的终端用户桌面(而不是web)应用程序。构建项目文件夹层次结构的最佳方法是什么?

理想的特性是易于维护、ide友好性、适合源代码控制分支/合并,以及易于生成安装包。

特别是:

你把源头放在哪里? 应用程序启动脚本放在哪里? 你把IDE项目的cruft放在哪里? 你把单元/验收测试放在哪里? 你把非python数据放在哪里,比如配置文件? 你把非python源代码放在哪里,比如pyd/二进制扩展模块的c++ ?


当前回答

根据我的经验,这只是一个迭代的问题。把你的数据和代码放在你认为它们应该去的地方。不管怎样,你都有可能是错的。但一旦你对事情会如何发展有了更好的了解,你就能更好地做出这些猜测。

至于扩展源,我们在trunk下有一个Code目录,其中包含python目录和其他各种语言目录。就我个人而言,我更倾向于下次尝试将任何扩展代码放到它自己的存储库中。

说了这么多,我回到我最初的观点:不要把它看得太大。把它放在对你有用的地方。如果你发现某些东西不起作用,它可以(也应该)被改变。

其他回答

根据我的经验,这只是一个迭代的问题。把你的数据和代码放在你认为它们应该去的地方。不管怎样,你都有可能是错的。但一旦你对事情会如何发展有了更好的了解,你就能更好地做出这些猜测。

至于扩展源,我们在trunk下有一个Code目录,其中包含python目录和其他各种语言目录。就我个人而言,我更倾向于下次尝试将任何扩展代码放到它自己的存储库中。

说了这么多,我回到我最初的观点:不要把它看得太大。把它放在对你有用的地方。如果你发现某些东西不起作用,它可以(也应该)被改变。

“Python打包权威”有一个样本项目:

https://github.com/pypa/sampleproject

它是一个示例项目,作为Python打包用户指南的打包和分发项目教程的辅助而存在。

非Python数据最好使用setuptools中的package_data支持绑定到Python模块中。我强烈推荐的一件事是使用名称空间包来创建多个项目可以使用的共享名称空间——很像将包放在com.yourcompany.yourproject中的Java约定(并且能够拥有一个共享的com.yourcompany.utils名称空间)。

再分支和合并,如果你使用一个足够好的源代码控制系统,它甚至可以通过重命名来处理合并;芭莎在这方面尤其在行。

与这里的其他一些答案相反,我赞成将src目录作为顶级目录(附带doc和test目录)。文档目录树的特定约定将根据您使用的内容而有所不同;例如,Sphinx有自己的快速入门工具支持的约定。

请,请利用setuptools和pkg_resources;这使得其他项目更容易依赖于您代码的特定版本(如果使用package_data,则可以使用不同的非代码文件同时安装多个版本)。

以正确的方式查看Python项目的开源。

让我摘录这篇优秀文章的项目布局部分:

When setting up a project, the layout (or directory structure) is important to get right. A sensible layout means that potential contributors don't have to spend forever hunting for a piece of code; file locations are intuitive. Since we're dealing with an existing project, it means you'll probably need to move some stuff around. Let's start at the top. Most projects have a number of top-level files (like setup.py, README.md, requirements.txt, etc). There are then three directories that every project should have: A docs directory containing project documentation A directory named with the project's name which stores the actual Python package A test directory in one of two places Under the package directory containing test code and resources As a stand-alone top level directory To get a better sense of how your files should be organized, here's a simplified snapshot of the layout for one of my projects, sandman: $ pwd ~/code/sandman $ tree . |- LICENSE |- README.md |- TODO.md |- docs | |-- conf.py | |-- generated | |-- index.rst | |-- installation.rst | |-- modules.rst | |-- quickstart.rst | |-- sandman.rst |- requirements.txt |- sandman | |-- __init__.py | |-- exception.py | |-- model.py | |-- sandman.py | |-- test | |-- models.py | |-- test_sandman.py |- setup.py As you can see, there are some top level files, a docs directory (generated is an empty directory where sphinx will put the generated documentation), a sandman directory, and a test directory under sandman.

根据Jean-Paul Calderone的Python项目文件系统结构:

Project/
|-- bin/
|   |-- project
|
|-- project/
|   |-- test/
|   |   |-- __init__.py
|   |   |-- test_main.py
|   |   
|   |-- __init__.py
|   |-- main.py
|
|-- setup.py
|-- README