Python 中的项目结构和文件实现

发布于 2024-10-18 05:18:49 字数 418 浏览 1 评论 0 原文

我在掌握如何构建我的 Python 项目时遇到了一些困难。我已阅读jcalderone:Python项目的文件系统结构并一直在查看CouchApp,但我仍然感到很困惑。

我了解文件的结构如何,但我不明白为什么。如果有人能给我详细介绍或者向我解释,我会很高兴。简单介绍如何设置基本的 Python 项目,以及文件如何相互交互。

我认为这绝对是来自 C、C++、Erlang 等其他语言的人或以前从未编程过的人可以从中受益。

I am having a bit of troubles getting a grasp on how to structure my Python projects. I have read jcalderone: Filesystem structure of a Python project and been looking at the source code of CouchApp, but I'm still feeling very puzzled.

I understand how the files should be structured, but I don't understand why. I would love if somebody could hook me up with a detailed walk-through of this, or could explain it to me. Simply how to set up a basic python project, and how the files would interact with each other.

I think this is definitely something people coming from other languages like C, C++, Erlang ... or people who have never been programming before, could benefit from.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿看小鸭子会跑 2024-10-25 05:18:49

将目录命名为与您的项目相关的名称。当您发布版本时,应该包含版本号后缀:Twisted-2.5。

不知道为什么这还不清楚。这似乎是显而易见的。所有内容都必须位于一个目录中。

为什么东西必须在一个目录中?因为大家都这么说,所以就是这样。

创建一个目录 Twisted/bin 并将可执行文件放在那里。

这就是 Linux 的工作方式。可执行文件位于 bin 目录中。它可以轻松地将这个特定目录放入您的 PATH 环境变量中。

如果您的项目可表示为单个 Python 源文件,请将其放入目录中并命名为与您的项目相关的名称。例如,Twisted/twisted.py。

正确的。您有 /Twisted、/Twisted/bin 和 /Twisted/twisted.py,其中包含实际运行的代码。你还会把它放在哪里?

这没有“为什么”。你还能把它放在哪里?

如果您需要多个源文件,请创建一个包(Twisted/twisted/,带有空的 Twisted/twisted/init.py)并将源文件放入其中。例如,Twisted/twisted/internet.py。

这就是 Python 包的工作方式。它们是带有 __init__.py 文件的目录。本教程对此非常清楚。

将单元测试放入 Twisted/twisted/test/ 包的子包中。

您还会在哪里进行测试?严重地。没有“为什么?”对此。没有明智的选择。

添加 Twisted/README 和 Twisted/setup.py 分别解释和安装您的软件

吧。你还会把它们放在哪里?再次。没有“为什么?”它们位于顶级目录中,因为——好吧——这就是目录的用途。它包含文件。

name the directory something related to your project. When you do releases, you should include a version number suffix: Twisted-2.5.

Not sure why this is unclear. It seems obvious. It all has to be in one directory.

Why do things have to be in one directory? Because everyone says so, that's why.

create a directory Twisted/bin and put your executables there.

This is the way Linux works. Executables are in a bin directory. It makes it easy to put this specific directory in your PATH environment variable.

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.

Right. You have /Twisted, /Twisted/bin and /Twisted/twisted.py with your actual, running code in it. Where else would you put it?

There's no "why" to this. Where else could you possibly put it?

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.

This is just the way Python packages work. They're directories with __init__.py files. The tutorial is pretty clear on this.

put your unit tests in a sub-package of your package Twisted/twisted/test/.

Where else would you put your tests? Seriously. There's no "why?" to this. There's no sensible alternative.

add Twisted/README and Twisted/setup.py to explain and install your software, respectively

Right. Where else would you put them? Again. There's no "why?" They go in the top directory because -- well -- that's what a directory is for. It contains files.

遮了一弯 2024-10-25 05:18:49

我不是Python专家,但如果您认为

  • 可能有计算机/程序涉及惠特项目,
  • 那么从第一个链接阅读这一行是有道理的。可能还有其他人涉及惠特项目

如果您有一致的名称和文件结构,人类和计算机都可能理解你的复杂程序好多了。

这涉及以下主题:测试、构建、部署、可重用性、搜索、结构、一致性……

标准带来连接。

I am not expert in python but reading this lines from first link makes sense if you think that

  • There might be computers/programs involved whit project
  • There might be other people involved whit project

If you have consistent names and file structures both humans and computers might understand your complex program much better.

This involves topics like: testing, building, deploying, re-usability, searching, structure, consistency...

Standard makes connectivity.

黯淡〆 2024-10-25 05:18:49

让我们尝试回答每条规则:

1) 您应该有一个具有好名称的根目录。如果您制作了软件包的 tarball,则将文件放在根目录上并不被认为是好的行为。当我打开一些东西并且当前文件夹中堆满了垃圾时,我感到真的很生气。

2)您应该将可执行文件与模块分开。他们是不同的野兽。如果您打算使用 distutils,它会让您的生活更轻松。

3) 如果您只有一个模块,则上述原因不适用。所以你可以简化你的树。

4)单元测试应该与其包紧密结合。但它们不是包,因此它是子包的完美案例。

Let's try to answer to each rule:

1) You should have a root dir with a good name. If you make a tarball of your package, it's not considered good behavior to have files on the root. I feel really angry when I unpack something and the current folder ends up cluttered with junk.

2) You should separate your executables from modules. They are different beasts. And if you plan to use distutils, it will make your life easier.

3) If you have a single module, the reasons above don't appy. So you can simplify your tree.

4) Unit tests should be closely tighted to it's package. But they are not the package, so it's the perfect case for a subpackage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文