如何组织一个Python项目?

发布于 2024-10-19 11:47:10 字数 458 浏览 2 评论 0原文

我是 Python 新手,正在启动一个迷你项目,但我对如何以“Python 方式”组织文件夹有一些疑问。

我在开发环境中使用 PyDev,当我创建一个新项目时,会创建一个名为 src 的文件夹。

+ src

现在,在 PyDev 中,我可以创建 Pydev ModulePyDev 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 技术交流群。

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

发布评论

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

评论(8

離人涙 2024-10-26 11:47:10

包基本上是一个文件夹,其下有 __init__.py 文件,通常还有一些模块,其中模块是一个 *.py 文件。
它主要与import有关。如果您将 __init__.py 添加到指标中,您可以使用:

from Indicators.Stochastics import *

from Indicators import Stochastics

顺便说一句,我建议将模块/包名称保持小写。它不影响功能,但更“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:

from Indicators.Stochastics import *

or

from Indicators import Stochastics

By the way, I would recommend to keep module/package names lowercase. It does not affect functionality but it's more "pythonic".

笑,眼淚并存 2024-10-26 11:47:10

从文件系统的角度来看,模块是以 .py 结尾的文件,包是包含模块和(嵌套)包的文件夹。如果文件夹包含 __init__.py 文件,Python 会将其识别为包。

像这样的文件结构

some/
    __init__.py
    foofoo.py
    thing/
        __init__.py
        barbar.py

定义了包 some,它有一个模块 foofoo 和一个嵌套包 thing,它又有一个模块 酒吧。但是,在使用包和模块时,您并没有真正区分这两种类型:

import some

some.dothis() # dothis is defined in 'some/__init__.py'

import some.foofoo # <- module
import some.thing # <- package

请遵循 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

some/
    __init__.py
    foofoo.py
    thing/
        __init__.py
        barbar.py

defines the package some, which has a module foofoo and a nested package thing, which again has a module barbar. However, when using packages and modules, you don't really distinguish these two types:

import some

some.dothis() # dothis is defined in 'some/__init__.py'

import some.foofoo # <- module
import some.thing # <- package

Please follow PEP8 when selecting naming your packages/modules (i.e. use lower-case names).

唐婉 2024-10-26 11:47:10

参见 python-package-template

目录结构

    .
    |-- bin
    |   `-- my_program
    |-- docs
    |   `-- doc.txt
    |-- my_program
    |   |-- data
    |   |   `-- some_data.html
    |   |-- __init__.py
    |   |-- submodule
    |   |   `-- __init__.py
    |   |-- helpers.py
    |-- tests
    |   |-- __init__.py
    |   |-- test_helpers.py
    |-- Makefile
    |-- CHANGES.txt
    |-- LICENSE.txt
    |-- README.md
    |-- requirements-dev.txt
    |-- requirements.txt
    `-- setup.py

cat Makefile

    PYTHON=`which python`
    NAME=`python setup.py --name`


    all: check test source deb

    init:
        pip install -r requirements.txt --use-mirrors

    dist: source deb

    source:
        $(PYTHON) setup.py sdist

    deb:
        $(PYTHON) setup.py --command-packages=stdeb.command bdist_deb

    rpm:
        $(PYTHON) setup.py bdist_rpm --post-install=rpm/postinstall --pre-uninstall=rpm/preuninstall

    test:
        unit2 discover -s tests -t .
        python -mpytest weasyprint

    check:
        find . -name \*.py | grep -v "^test_" | xargs pylint --errors-only --reports=n
        # pep8
        # pyntch
        # pyflakes
        # pychecker
        # pymetrics

    clean:
        $(PYTHON) setup.py clean
        rm -rf build/ MANIFEST dist build my_program.egg-info deb_dist
        find . -name '*.pyc' -delete

See python-package-template

Directory structure

    .
    |-- bin
    |   `-- my_program
    |-- docs
    |   `-- doc.txt
    |-- my_program
    |   |-- data
    |   |   `-- some_data.html
    |   |-- __init__.py
    |   |-- submodule
    |   |   `-- __init__.py
    |   |-- helpers.py
    |-- tests
    |   |-- __init__.py
    |   |-- test_helpers.py
    |-- Makefile
    |-- CHANGES.txt
    |-- LICENSE.txt
    |-- README.md
    |-- requirements-dev.txt
    |-- requirements.txt
    `-- setup.py

cat Makefile

    PYTHON=`which python`
    NAME=`python setup.py --name`


    all: check test source deb

    init:
        pip install -r requirements.txt --use-mirrors

    dist: source deb

    source:
        $(PYTHON) setup.py sdist

    deb:
        $(PYTHON) setup.py --command-packages=stdeb.command bdist_deb

    rpm:
        $(PYTHON) setup.py bdist_rpm --post-install=rpm/postinstall --pre-uninstall=rpm/preuninstall

    test:
        unit2 discover -s tests -t .
        python -mpytest weasyprint

    check:
        find . -name \*.py | grep -v "^test_" | xargs pylint --errors-only --reports=n
        # pep8
        # pyntch
        # pyflakes
        # pychecker
        # pymetrics

    clean:
        $(PYTHON) setup.py clean
        rm -rf build/ MANIFEST dist build my_program.egg-info deb_dist
        find . -name '*.pyc' -delete
清秋悲枫 2024-10-26 11:47:10

您可能想查看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

一世旳自豪 2024-10-26 11:47:10

在决定项目结构之前,最好先问自己该项目的目的是什么。这将是一次性分析吗?您想研究一个玩具概念吗?您打算分发一个完整的项目吗?您在构建项目时想要投入的精力会有所不同。

  • 如果是一次性分析,我喜欢使用 ipython 笔记本。笔记本将记录您的想法,您可以在代码中添加注释以供以后参考。
  • 如果您想研究一个玩具概念,我会找到一种简单、快速的方法,效果最佳。您希望能够快速实施您的概念,以发现它是否可行,从而值得花费更多时间。 Python 哲学的一部分是“不要尝试完美,因为“足够好”往往就是这样。”您随时可以稍后回来并按照最佳软件工程实践的方式构建您的项目。
  • 如果您想构建项目以便以后可以分发它,并使其扩展到许多模块,我建议使用以下结构:

    项目名称
     ├── MANIFEST.in
     ├── setup.py
     ├── 自述文件
     ├── .gitignore
     ├── .git
     ├── 项目名称_env
     └── 项目名称
         ├── __init__.py
         ├── 分包one
         │ ├── __init__.py
         │ ├── 第二个模块.py
         │ ├── 测试
         │ │ └── test_second_module.py
         │ └── 型号
         │ └── 型号1
         ├── 第一个模块.py   
         └── 测试
             └── 测试_第二_模块.py
    

我喜欢这种结构的详细原因 在我的博客文章中,但基本要点是层次结构较低的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 it's a one off analysis, I like to use ipython notebooks. The notebook will capture the flow of your thoughts, and you can add notes in markup to your code for later reference.
  • If it's a toy concept you want to investigate, I find a simple, quick approach to work best. You want to be able to quickly implement your concept to discover if it's even feasible and thus worth spending more time on it. Part of Python's philosophy is 'Don’t try for perfection because “good enough” is often just that.' You can always come back later and structure your project in a way that follows best software engineering practices.
  • 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:

    projectname
     ├── MANIFEST.in
     ├── setup.py
     ├── README
     ├── .gitignore
     ├── .git
     ├── projectname_env
     └── projectname
         ├── __init__.py
         ├── subpackageone
         │   ├── __init__.py
         │   ├── second_module.py
         │   ├── tests
         │   │   └── test_second_module.py
         │   └── models
         │       └── model1
         ├── first_module.py   
         └── tests
             └── test_second_module.py
    

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.

写下不归期 2024-10-26 11:47:10

包是一个包含 __init__.py 的目录。与目录的区别在于您可以导入它。

本身并没有一种“Python 方式”,但您会发现将所有模块放入一个包中并使用与项目相关的名称是一个好主意。

此外,为了遵循 Python 风格指南 PEP8,包和模块名称应全部小写。因此,如果我们假设该项目名为“Botond Statistics”,您的结构将是这样的:

botondstats/
    indicators/
        moving_averages.py
        stochastics.py
    strategies/
        moving_averages_cross.py
    example.py

然后您可以通过执行以下操作找到 Stochastics 类

from botondstats.indicators.stochastics.Stochastics

(有多种方法可以保持结构但使导入更短,但这是另一个问题)。

如果您愿意,可以将此结构放在 src/ 下,但这不是必需的。我从来不这样做。
相反,我有一个主目录:

BotondStatistics/
    docs/
    botonstats/ # the above structure
    setup.py    # Distutils/distribute configuration for packaging.

在这个目录中,我通常还有一个 virtualenv,所以我实际上也有 bin/lib/ 等。开发通常是通过运行来完成的

./bin/python setup.py tests

,因为我使用 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:

botondstats/
    indicators/
        moving_averages.py
        stochastics.py
    strategies/
        moving_averages_cross.py
    example.py

You would then find the Stochastics class by doing

from botondstats.indicators.stochastics.Stochastics

(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:

BotondStatistics/
    docs/
    botonstats/ # the above structure
    setup.py    # Distutils/distribute configuration for packaging.

In this directory I also typically have a virtualenv so I actually also have bin/ lib/ et al. Development is typically done by running

./bin/python setup.py tests

As I use the Distrubute test runner to run the tests.

That's how I do it. :-)

无畏 2024-10-26 11:47:10

audreyrcookiecutter 项目包含多个 Python项目模板:

该包使用单个 ~/.cookiecutterrc 文件即可使用 Python、Java、JS 和其他语言创建自定义项目模板。

例如,与 PyPI 兼容的 Python 模板:

cookiecutter-pypackage

The cookiecutter project by audreyr 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:

cookiecutter-pypackage

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