我在掌握如何构建我的 Python 项目时遇到了一些麻烦。我读过jcalderone: Filesystem structure of a Python project并一直在查看 CouchApp 的源代码, 但我仍然感到很困惑。
我理解如何文件的结构,但我不理解为什么。如果有人可以详细介绍一下,或者可以向我解释一下,我会很高兴。简单介绍如何设置基本的 Python 项目,以及文件如何相互交互。
我认为这绝对是来自 C、C++、Erlang 等其他语言的人或以前从未接触过编程的人可以从中受益的东西。
请您参考如下方法:
name the directory something related to your project. When you do releases, you should include a version number suffix: Twisted-2.5.
不确定为什么不清楚。这似乎很明显。这一切都必须在一个目录中。
为什么东西必须在一个目录中?因为每个人都这么说,所以这就是原因。
create a directory Twisted/bin and put your executables there.
这就是 Linux 的工作方式。可执行文件位于 bin 目录中。将此特定目录放入 PATH 环境变量中变得很容易。
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.
没错。您有/Twisted、/Twisted/bin 和/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.
这正是 Python 包的工作方式。它们是包含 __init__.py 文件的目录。本教程对此非常清楚。
put your unit tests in a sub-package of your package Twisted/twisted/test/.
您还会在哪里进行测试?严重地。没有“为什么?”对此。没有明智的选择。
add Twisted/README and Twisted/setup.py to explain and install your software, respectively
没错。你还会把它们放在哪里?再次。没有“为什么?”它们位于顶级目录中,因为——好吧——这就是目录的用途。它包含文件。
