How to organize a Python project?

I am new to Python and I am running a mini project. but I have some doubts about how to organize folders in Python Paths.

I use PyDev in my development environment, and when I create a new project, a folder called "src" is created

+ src 

Now, in PyDev, I can create a "Pydev module" and a "PyDev package"

I need to organize my project as follows:

 + 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?

Regards,

+61
python pydev project-organization
Mar 01 2018-11-11T00:
source share
8 answers

A package is basically a folder with the __init__.py file under it and usually some modules, where Module is *.py . This is mainly due to import . 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 storing module / package names in lower case. This does not affect functionality, but is more "python".

+38
Mar 01 '11 at 13:40
source share

From a file system perspective, a module is a file ending in .py , and the package again is a folder containing the modules and (nested) packages. Python recognizes the folder as a package if it contains the __init__.py file.

File structure like this

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

defines a package some that has a foofoo module and a nested thing that again has a barbar module. However, when using packages and modules, you do not distinguish between 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 choosing package / module naming (i.e. use lowercase names).

+34
Mar 01 2018-11-11T00:
source share

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 
+25
Jan 23 '13 at 4:06 on
source share

You might want to check out the template library of modern packages. This makes it possible to set up a really nice basic layout for a project in which you look at a few questions and try to help you get something that can be easily distributed.

http://pypi.python.org/pypi/modern-package-template

+13
Mar 01 2018-11-11T00:
source share

Before deciding on the structure of a project, it’s good to ask yourself what the purpose of the project is. Will this be one analysis? Toy concept you want to explore? A completely bloated project that you are planning to distribute? The amount of effort you want to make in structuring your project will be different.

  • If this is one analysis, I like to use ipython notebooks . Notepad will capture the flow of your thoughts, and you can add notes to the markup to your code for future reference.
  • If this is the concept of toys that you want to explore, I find a simple and quick approach to the best. You want to be able to quickly implement your concept in order to find out if this is possible, and therefore it is worth spending more time on it. Part of the Python philosophy is "Don't try to be perfect, because" good enough "often happens that way." You can always come back later and structure your project in such a way as to follow the best software development technologies.
  • If you want to structure your project so that it can be distributed later, and therefore it scales for 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 indicated in my blog post , but the main point is that the hierarchically lower level projectname contains your actual project. Along with this, there are all the tools that help manage (git) and the package (setup.py, MANIFEST.in).

+6
Sep 28 '15 at 17:42
source share

A package is a directory with __init__.py in it. The difference from the catalog is that you can import it.

There is no "Python path" as such, but you will find that it is recommended that you place all your modules in one package with the name associated with the project.

In addition, to follow the Python style guide, PEP8, package and module names must be lowercase. So, if we assume that the project is called "Bot Statistics", your structure will be something like this:

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

Then you will find the Stochastic class by doing

 from botondstats.indicators.stochastics.Stochastics 

(There are several ways to maintain the structure, but to make the import shorter, but that is another question).

You can put this structure under src/ if you want, but this is not necessary. I never do that. Instead, I have a main directory:

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

In this directory, I also usually have virtualenv, so I also have bin / lib / et al. Development is usually done by running

 ./bin/python setup.py tests 

How I use Distrubute test runner to run tests.

How I do it. :-)

+4
Mar 01 2018-11-11T00:
source share
+3
Mar 18 '14 at 9:56
source share

The cookiecutter audreyr project includes several Python project templates:

The package uses one ~/.cookiecutterrc file to create custom project templates in Python, Java, JS and other languages.

For example, a Python template compatible with PyPI :

cookiecutter-pypackage

+1
Apr 10 '17 at 14:08
source share



All Articles