python程序应用_Python应用程序布局:参考

python程序应用

Python, though opinionated on syntax and style, is surprisingly flexible when it comes to structuring your applications.

尽管Python在语法和样式方面颇受好评,但在构造应用程序时却异常灵活。

On the one hand, this flexibility is great: it allows different use cases to use structures that are necessary for those use cases. On the other hand, though, it can be very confusing to the new developer.

一方面,这种灵活性非常好:它允许不同的用例使用这些用例所必需的结构。 但是,另一方面,这会使新开发人员感到非常困惑。

The Internet isn’t a lot of help either—there are as many opinions as there are Python blogs. In this article, I want to give you a dependable Python application layout reference guide that you can refer to for the vast majority of your use cases.

互联网也无济于事-意见与Python博客一样多。 在本文中,我想为您提供可靠的Python应用程序布局参考指南,您可以在绝大多数用例中参考该指南

You’ll see examples of common Python application structures, including command-line applications (CLI apps), one-off scripts, installable packages, and web application layouts with popular frameworks like Flask and Django.

您将看到常见Python应用程序结构的示例,包括命令行应用程序(CLI应用程序),一次性脚本,可安装软件包以及带有流行框架(例如FlaskDjango)的 Web应用程序布局。

Note: This reference guide assumes a working knowledge of Python modules and packages. Check out our introduction to Python modules and packages for a refresher if you’re feeling a little rusty.

注意:本参考指南假定您对Python模块和软件包有一定的了解。 如果您感到有些生锈,请查看我们对Python模块和软件包的介绍 ,以复习一下。

命令行应用程序布局 (Command-Line Application Layouts)

A lot of us work primarily with Python applications that are run via command-line interfaces (CLIs). This is where you often start with a blank canvas, and the flexibility of Python application layouts can be a real headache.

我们很多人主要使用通过命令行界面(CLI)运行的Python应用程序。 这是您通常从空白画布开始的地方,Python应用程序布局的灵活性可能真是令人头疼。

Starting with an empty project folder can be intimidating and lead to no shortage of coder’s block. In this section, I want to share some proven layouts that I personally use as a starting point for all of my Python CLI applications.

从一个空的项目文件夹开始可能会令人生畏,并且会导致缺少编码器块。 在本部分中,我想分享一些经过验证的布局,这些布局我个人用作所有Python CLI应用程序的起点。

We’ll start with a very basic layout for a very basic use case: a simple script that runs on its own. You’ll then see how to build up the layout as the use cases advance.

我们将从一个非常基本的用例的非常基本的布局开始:一个独立运行的简单脚本。 然后,您将了解如何随着用例的发展而构建布局。

一次性脚本 (One-Off Script)

You just make a .py script, and it’s gravy, right? No need to install—just run the script in its directory!

您只需创建一个.py脚本,它就很肉汁了,对吗? 无需安装-只需在其目录中运行脚本即可!

Well, that’s fine if you’re just making a script for your own use, or one that doesn’t have any external dependencies, but what if you have to distribute it? Especially to a less tech-savvy user?

好吧,如果您只是编写一个供自己使用的脚本,或者没有任何外部依赖性的脚本,那很好,但是如果您必须分发该脚本呢? 尤其是对于不那么精通技术的用户?

The following layout will work for all of these cases and can easily be modified to reflect whatever installation or other tools you use in your workflow. This layout will cover you whether you’re creating a pure Python script (that is, one with no dependencies) or using a tool like pip or Pipenv.

以下布局将适用于所有这些情况,并且可以轻松修改以反映您在工作流程中使用的任何安装或其他工具。 无论您是要创建纯Python脚本(即无依赖性的脚本)还是使用pipPipenv之类的工具,这种布局都将覆盖您。

While you read this reference guide, keep in mind that the exact location of the files in the layout matters less than the reason they are placed where they are. All of these files should be in a project directory named after your project. For this example, we will use (what else?) helloworld as the project name and root directory.

阅读本参考指南时,请记住,文件在布局中的确切位置比其放置位置的原因要少。 所有这些文件都应位于以项目命名的项目目录中。 对于此示例,我们将使用(还有什么?) helloworld作为项目名称和根目录。

Here’s the Python project structure I typically use for a CLI app:

这是我通常用于CLI应用程序的Python项目结构:

helloworld/
│
├── .gitignore
├── helloworld.py
├── LICENSE
├── README.md
├── requirements.txt
├── setup.py
└── tests.py
helloworld/
│
├── .gitignore
├── helloworld.py
├── LICENSE
├── README.md
├── requirements.txt
├── setup.py
└── tests.py

This is pretty straightforward: everything is in the same directory. The files shown here are not necessarily exhaustive, but I recommend keeping the number of files to a minimum if you plan on using a basic layout like this. Some of these files will be new to you, so let’s take a quick look at what each of them does.

这非常简单:所有内容都在同一目录中。 此处显示的文件不一定是详尽无遗的,但是如果您打算使用这种基本布局,则建议将文件数保持在最少。 其中一些文件对您来说是新文件,因此让我们快速看一下每个文件的功能。

  • .gitignore: This is a file that tells Git which kinds of files to ignore, like IDE clutter or local configuration files. Our Git tutorial has all the details, and you can find sample .gitignore files for Python projects here.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值