Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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".

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?

like image 724
André Avatar asked Mar 01 '11 13:03

André


3 Answers

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".

like image 172
Botond Béres Avatar answered Nov 07 '22 15:11

Botond Béres


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).

like image 41
Oben Sonne Avatar answered Nov 07 '22 15:11

Oben Sonne


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
like image 28
Vitaly Fadeev Avatar answered Nov 07 '22 17:11

Vitaly Fadeev