Python应用程序的最佳项目结构是什么?

2024-04-19 05:06:19 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

特别是:

  1. 你把来源放在哪里?
  2. 你把应用程序启动脚本放在哪里?
  3. 你把IDE项目放在哪里?
  4. 你把单元/验收测试放在哪里?
  5. 您将非Python数据(如配置文件)放在哪里?
  6. 你在PyD/SO二进制扩展模块中放置C++等非Python源代码吗?

Tags: 项目方法脚本文件夹web应用程序源代码层次结构
3条回答

根据Jean-Paul Calderone的Filesystem structure of a Python project

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

这个blog post by Jean-Paul Calderone通常在Freenode上的python中作为答案给出。

Filesystem structure of a Python project

Do:

  • name the directory something related to your project. For example, if your project is named "Twisted", name the top-level directory for its source files Twisted. When you do releases, you should include a version number suffix: Twisted-2.5.
  • create a directory Twisted/bin and put your executables there, if you have any. Don't give them a .py extension, even if they are Python source files. Don't put any code in them except an import of and call to a main function defined somewhere else in your projects. (Slight wrinkle: since on Windows, the interpreter is selected by the file extension, your Windows users actually do want the .py extension. So, when you package for Windows, you may want to add it. Unfortunately there's no easy distutils trick that I know of to automate this process. Considering that on POSIX the .py extension is a only a wart, whereas on Windows the lack is an actual bug, if your userbase includes Windows users, you may want to opt to just have the .py extension everywhere.)
  • If your project is expressable as a single Python source file, then put it into the directory and name it something related to your project. For example, Twisted/twisted.py. If you need multiple source files, create a package instead (Twisted/twisted/, with an empty Twisted/twisted/__init__.py) and place your source files in it. For example, Twisted/twisted/internet.py.
  • put your unit tests in a sub-package of your package (note - this means that the single Python source file option above was a trick - you always need at least one other file for your unit tests). For example, Twisted/twisted/test/. Of course, make it a package with Twisted/twisted/test/__init__.py. Place tests in files like Twisted/twisted/test/test_internet.py.
  • add Twisted/README and Twisted/setup.py to explain and install your software, respectively, if you're feeling nice.

Don't:

  • put your source in a directory called src or lib. This makes it hard to run without installing.
  • put your tests outside of your Python package. This makes it hard to run the tests against an installed version.
  • create a package that only has a __init__.py and then put all your code into __init__.py. Just make a module instead of a package, it's simpler.
  • try to come up with magical hacks to make Python able to import your module or package without having the user add the directory containing it to their import path (either via PYTHONPATH or some other mechanism). You will not correctly handle all cases and users will get angry at you when your software doesn't work in their environment.

没什么大不了的。任何让你快乐的事都会成功的。没有太多愚蠢的规则,因为Python项目可以很简单。

  • /scripts/bin用于那种命令行接口
  • /tests为您的测试
  • /lib为您的C语言库
  • /doc对于大多数文档
  • /apidoc用于Epydoc生成的API文档。

顶层目录可以包含自述文件、配置文件和其他内容。

困难的选择是是否使用/src树。Python与Java或C没有区别/src/lib/bin

由于一些人认为顶层目录毫无意义,所以顶层目录可以是应用程序的顶层架构。

  • /foo
  • /bar
  • /baz

我建议把这些都放在“我的产品名称”目录下。所以,如果您正在编写一个名为quux的应用程序,那么包含所有这些内容的目录名为/quux

然后,另一个项目的PYTHONPATH可以包含/path/to/quux/foo以重用QUUX.foo模块。

在我的例子中,由于我使用Komodo Edit,我的IDE cuft是一个.KPF文件。实际上,我把它放在顶层的/quux目录中,省略了将它添加到SVN。

相关问题 更多 >