如何组织一个Python项目?
我是 Python 新手,正在启动一个迷你项目,但我对如何以“Python 方式”组织文件夹有一些疑问。
我在开发环境中使用 PyDev
,当我创建一个新项目时,会创建一个名为 src
的文件夹。
+ src
现在,在 PyDev
中,我可以创建 Pydev Module
和 PyDev Package
我需要按以下方式组织我的项目:
+ Indicators
- Moving_averages.py
- Stochastics.py
+ Strategies
- Moving_averages_cross.py
- example.py
如何根据模块和包来组织它?模块和包的含义是什么?
I'm new to Python and I'm starting a mini Project, but I have some doubts on how to organize the folders in the "Python Way".
I'm using PyDev
in my Development Environment, and when I create a new project a folder is created called src
+ src
Now, in the PyDev
, I can create Pydev Module
and PyDev Package
I need to organize my Project in the following way:
+ Indicators
- Moving_averages.py
- Stochastics.py
+ Strategies
- Moving_averages_cross.py
- example.py
How can I organize this in terms of Modules and Packages? What is the meaning of Modules and Packages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
包基本上是一个文件夹,其下有
__init__.py
文件,通常还有一些模块,其中模块是一个*.py
文件。它主要与
import
有关。如果您将__init__.py
添加到指标中,您可以使用:或
顺便说一句,我建议将模块/包名称保持小写。它不影响功能,但更“Pythonic”。
A Package is basically a folder with
__init__.py
file under it and usually some Modules, where Module is a*.py
file.It has to do with
import
mainly. If you add__init__.py
to Indicators you can use:or
By the way, I would recommend to keep module/package names lowercase. It does not affect functionality but it's more "pythonic".
从文件系统的角度来看,模块是以
.py
结尾的文件,包是包含模块和(嵌套)包的文件夹。如果文件夹包含__init__.py
文件,Python 会将其识别为包。像这样的文件结构
定义了包
some
,它有一个模块foofoo
和一个嵌套包thing
,它又有一个模块酒吧。但是,在使用包和模块时,您并没有真正区分这两种类型:
请遵循 PEP8 当选择命名你的包/模块时(即使用小写名称)。
From a file system perspective, a module is a file ending with
.py
and a package is a folder containing modules and (nested) packages again. Python recognizes a folder as a package if it contains a__init__.py
file.A file structure like that
defines the package
some
, which has a modulefoofoo
and a nested packagething
, which again has a modulebarbar
. However, when using packages and modules, you don't really distinguish these two types:Please follow PEP8 when selecting naming your packages/modules (i.e. use lower-case names).
参见 python-package-template
目录结构
cat Makefile
See python-package-template
Directory structure
cat Makefile
您可能想查看modern-package-template 库。它提供了一种为项目设置非常好的基本布局的方法,它会引导您解决一些问题,并尝试帮助您获得能够相当轻松地分发的内容。
http://pypi.python.org/pypi/modern-package-template
You might want to check out the modern-package-template libary. It provides a way to setup a really nice basic layout for a project that walks you through a few questions and tries to help you get something that's able to be distributed fairly easily.
http://pypi.python.org/pypi/modern-package-template
在决定项目结构之前,最好先问自己该项目的目的是什么。这将是一次性分析吗?您想研究一个玩具概念吗?您打算分发一个完整的项目吗?您在构建项目时想要投入的精力会有所不同。
如果您想构建项目以便以后可以分发它,并使其扩展到许多模块,我建议使用以下结构:
我喜欢这种结构的详细原因 在我的博客文章中,但基本要点是层次结构较低的
projectname
目录包含您的实际项目。除此之外还有所有帮助管理(git)和打包(setup.py、MANIFEST.in)的工具。Before deciding on a project structure, it's good to ask yourself what the purpose of the project is going to be. Is this going to be one off analysis? A toy concept you want to investigate? A full blown project you intend to distribute? The amount of effort you want to put into structuring your project will be different.
If you want to structure your project so you can later distribute it, and so that it scales to many modules I recommend the following structure:
The detailed reasons why I like this structure are in my blog post, but the basic gist is that the hierarchically lower level
projectname
directory contains your actual project. Alongside it are all the tools that help manage (git) and package (setup.py, MANIFEST.in) it.包是一个包含
__init__.py
的目录。与目录的区别在于您可以导入它。本身并没有一种“Python 方式”,但您会发现将所有模块放入一个包中并使用与项目相关的名称是一个好主意。
此外,为了遵循 Python 风格指南 PEP8,包和模块名称应全部小写。因此,如果我们假设该项目名为“Botond Statistics”,您的结构将是这样的:
然后您可以通过执行以下操作找到 Stochastics 类
(有多种方法可以保持结构但使导入更短,但这是另一个问题)。
如果您愿意,可以将此结构放在
src/
下,但这不是必需的。我从来不这样做。相反,我有一个主目录:
在这个目录中,我通常还有一个 virtualenv,所以我实际上也有 bin/lib/ 等。开发通常是通过运行来完成的
,因为我使用 Distrubute 测试运行程序来运行测试。
我就是这么做的。 :-)
A package is a directory with a
__init__.py
in it. The difference from a directory is that you can import it.There isn't a "Python way" per se, but you'll find that it's a good idea to put all your modules in one package with a name related to the project.
Also, to follow the Python style guide, PEP8, the package and module names should be all lowercase. So, if we assume the project is called "Botond Statistics" your structure would be something like this:
You would then find the Stochastics class by doing
(There are various ways to keep the structure but make imports shorter, but that's another question).
You can put this structure under
src/
if you want to, but it's not necessary. I never do.Instead I have a main directory:
In this directory I also typically have a virtualenv so I actually also have bin/ lib/ et al. Development is typically done by running
As I use the Distrubute test runner to run the tests.
That's how I do it. :-)
尝试
python_boilerplate_template
:https://pypi.python.org/pypi/python_boilerplate_template
Try
python_boilerplate_template
:https://pypi.python.org/pypi/python_boilerplate_template
audreyr
的cookiecutter
项目包含多个 Python项目模板:该包使用单个
~/.cookiecutterrc
文件即可使用 Python、Java、JS 和其他语言创建自定义项目模板。例如,与
PyPI
兼容的 Python 模板:The
cookiecutter
project byaudreyr
includes several Python project templates:The package uses a single
~/.cookiecutterrc
file to create custom project templates in Python, Java, JS, and other languages.For example, a Python template compatible with
PyPI
: