How is the structure of projects in python?

Asked

Viewed 4,694 times

2

I would like to know if there is a pattern or good practices as to how to structure a Python project. For example, I will make a CRUD using MVC. Something very simple. In JAVA I would create a folder model, one controller and a view. In python I do how? I create modules models, views and controllers? Or create a folder structure? Or packets?

3 answers

4

I don’t know if there’s a pattern to this kind of division, I even consulted PEP, but I couldn’t find anything related. But it follows the pattern I use to build my projects, based on this article(Filesystem Structure of a Python project ):

Projeto/
     NOME/
        __init__.py
        controllers/
            __init__.py
        views/
            __init__.py
        models/
            __init__.py         
     bin/
     docs/
     setup.py
     testes/
         NOME_testes.py
         __init__.py

You can also rely on the structure of some larger projects, follows the structure of Django (Esqueleto Django Project)

[projectname]/                  <- project root
+-- [projectname]/              <- Django root
¦   +-- __init__.py
¦   +-- settings/
¦   ¦   +-- common.py
¦   ¦   +-- dev.py
¦   ¦   +-- djangodefault.py
¦   ¦   +-- __init__.py
¦   ¦   +-- production.py
¦   +-- urls.py
¦   +-- wsgi.py
+-- apps/
¦   +-- __init__.py
+-- configs/
¦   +-- apache2_vhost.sample
¦   +-- README
+-- doc/
¦   +-- Makefile
¦   +-- source/
¦       +-- *snap*
+-- manage.py
+-- README.rst
+-- run/
¦   +-- media/
¦   ¦   +-- README
¦   +-- README
¦   +-- static/
¦       +-- README
+-- static/
¦   +-- README
+-- templates/
    +-- README

3


If Voce will use the "pure" python structure as you wish, including the way you did in your previous language, of course adapting to the particularities of python. Now... if you are going to use some framework, like Django or flask, the ideal would be to follow the recommendations of the FW developers.

Suggestion from one of the members of Python Software Foundation for the structuring of "python Pure":

README.rst
LICENSE
setup.py
requirements.txt
sample/__init__.py
sample/core.py
sample/helpers.py
docs/conf.py
docs/index.rst
tests/test_basic.py
tests/test_advanced.py

See more details on the author’s website.


Highly valued practices in the python community are recommended by Twelve-factor,

The Twelve-factor app:
In the modern era, software is commonly delivered as a service: called web apps, or software-as-service. The twelve-factor application is a methodology for building software-as-a-service that:

  • Use declarative formats to automate initial setup, minimize time and cost for new developers to participate in the project;
  • Has a clear contract with the operating system that supports it, offering maximum portability between environments that run it;
  • Suitable for deployment on modern cloud platforms, avoiding the need for servers and system administration;
  • Minimize divergence between development and production, allowing continuous deployment for maximum agility;
  • And they can scale without significant changes in tools, architectures, or development practices. The twelve-factor methodology can be applied to applications written in any programming language, and using any combination of media services (database, queues, memory cache, etc).

2

Man, I usually use my own structure. In a crud with graphical interface with three tables: A B C I usually do:

*A folder for A with the crud file, the main py file and the graphical interface

*A folder for B with the crud file, the main py file and the graphical interface

*A folder for C with the crud file, the main py file and the graphical interface

*Home page files (main py and GUI) in the main folder.

It usually works, but it varies from the graphical interface, from the SQL language.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.