简体   繁体   English

如何组织 Python 项目?

[英]How to organize a Python Project?

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".我是 Python 新手,正在开始一个迷你项目,但我对如何以“Python 方式”组织文件夹有一些疑问。

I'm using PyDev in my Development Environment, and when I create a new project a folder is created called src我在我的开发环境中使用PyDev ,当我创建一个新项目时,会创建一个名为src的文件夹

+ src

Now, in the PyDev , I can create Pydev Module and PyDev Package现在,在PyDev ,我可以创建Pydev ModulePyDev 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?模块和包的含义是什么?

A Package is basically a folder with __init__.py file under it and usually some Modules, where Module is a *.py file.一个包基本上是一个文件夹,在它下面有__init__.py文件,通常是一些模块,其中模块是一个*.py文件。 It has to do with import mainly.它主要与import If you add __init__.py to Indicators you can use:如果您将__init__.py添加到 Indicators 您可以使用:

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".它不会影响功能,但它更“pythonic”。

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.从文件系统的角度来看,模块是一个以.py结尾的文件,包是一个包含模块和(嵌套)包的文件夹。 Python recognizes a folder as a package if it contains a __init__.py file.如果文件夹包含__init__.py文件,Python 会将文件夹识别为包。

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 .定义包some ,它有一个模块foofoo和一个嵌套的包thing ,它再次有一个模块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 (ie use lower-case names).选择命名包/模块时请遵循PEP8 (即使用小写名称)。

See python-package-template参见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

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 http://pypi.python.org/pypi/modern-package-template

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 .如果是一次性分析,我喜欢使用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.' Python 哲学的一部分是“不要追求完美,因为“足够好”通常就是这样。 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.我喜欢这种结构的详细原因 在我的博客文章中,但基本要点是分层较低级别的projectname目录包含您的实际项目。 Alongside it are all the tools that help manage (git) and package (setup.py, MANIFEST.in) it.除了它之外,还有帮助管理 (git) 和打包 (setup.py, MANIFEST.in) 的所有工具。

A package is a directory with a __init__.py in it.包是一个包含__init__.py的目录。 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.本身并没有“Python 方式”,但您会发现将所有模块放在一个具有与项目相关的名称的包中是个好主意。

Also, to follow the Python style guide, PEP8, the package and module names should be all lowercase.此外,为了遵循 Python 风格指南 PEP8,包和模块名称应全部小写。 So, if we assume the project is called "Botond Statistics" your structure would be something like this:因此,如果我们假设该项目称为“Botond Statistics”,那么您的结构将是这样的:

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

You would then find the Stochastics class by doing然后,您将通过执行以下操作找到 Stochastics 类

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.如果您愿意,可以将此结构放在src/下,但这不是必需的。 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.在这个目录中,我通常也有一个 virtualenv,所以我实际上也有 bin/lib/ 等。 Development is typically done by running开发通常通过运行来完成

./bin/python setup.py tests

As I use the Distrubute test runner to run the tests.因为我使用 Distrubute 测试运行器来运行测试。

That's how I do it.我就是这样做的。 :-) :-)

The cookiecutter project by audreyr includes several Python project templates:cookiecutter的项目audreyr包括一些Python项目模板:

The package uses a single ~/.cookiecutterrc file to create custom project templates in Python, Java, JS, and other languages.该包使用单个~/.cookiecutterrc文件以 Python、Java、JS 和其他语言创建自定义项目模板。

For example, a Python template compatible with PyPI :例如,与PyPI兼容的 Python 模板:

cookiecutter-pypackage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM