How to structure a python project with three applications using a common module

My project contains three Python applications. Appendix 1 is a web application. Applications 2 and 3 contain scripts that load some data.

All three applications should use the Common module, containing the "model" (classes that are stored in the database) and general settings.

I do not know how to structure this project. I could create three directories, one for each application, and copy Common three times to my directories (doesn't seem right).

Another idea that comes to mind; create the main directory and put all the files from Common there , including __ init __. py . Then create three subdirectories (submodules), one for each application.

Another way: install Common with pip, but that means I have to reinstall every time I change something in this module.

In previous projects, I used .NET - the equivalent in this world would be a solution with four projects, one of which was Common .

Any suggestions?

+6
source share
4 answers

I have a similar project that is set up this way

project_root/ App1/ __init__.py FlaskControlPanel/ app.py static/ templates/ models/ __init__.py mymodels.py 

Then I start everything starting with project_root . I have a small script (package or shell depending on my environment) that sets PYTHONPATH=. so that imports work correctly. This is because I usually develop using PyCharm, where the import "just works", but when I deploy the final product, the path does not match what it did in my IDE.

After PYTHONPATH has installed everything from the root of the project, you can perform standard import.

For example, from my FlaskControlPanel app.py , I have this line:

 from models.mymodels import Model1, Model2, Model3 

From App1 __init__.py I have the same import statement:

 from models.mymodels import Model1, Model2, Model3 

I can start the Flask application by executing it from my command line (on Windows) when I am in the project_root directory:

 setlocal SET PYTHONPATH=. python FlaskControlPanel\app.py 

setlocal used to ensure that PYTHONPATH changed only for this session.

+5
source

I like this approach

 projects/ __init__.py project1/ __init__.py project2/ __init__.py lib1/ __init__.py libfile.py lib2/ __init__.py 

So, I need to write cd to the projects folder. To start projects use

 python -m project_name 

This allows me to easily import from any external library, for example

 from lib1.libfile import [imoprt what you want] 

or

 from lib1 import libfile 
+1
source

I would create a structure like this:

 project_root/ app1/ __init__.py script.py common/ __init__.py models.py (all "common" models) 

app1 / script.py

 import os, sys # add parent directory to pythonpath basepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..') if basepath not in sys.path: sys.path.append(basepath) from common.models VeryCommonModel print VeryCommonModel 

If you do not want to set the python path at run time, set the python path before running the script:

 $ export PYTHONPATH=$PYTHONPATH:/path/to/project_root 

And then you can do:

 python app1/script.py 
0
source

Make standard Python modules from your applications. I recommend this structure:

 apps/ common/ setup.py common/ __init__.py models.py app1/ setup.py app1/ __init__.py models.py project/ requirements.txt 

Basic setup for general application:

 #!/usr/bin/env python from setuptools import setup, find_packages setup( name='common', version='1.0.0', packages=find_packages(), zip_safe=False, ) 

Make a similar setup.py for other applications.

Set "-e" to "-e" for your applications in the .txt requirements file:

 -e apps/common -e apps/app1 

Set requirements using pip:

 $ pip install -r requirements.txt 

An editable version means that the source files will be associated with the Python environment. Any change to the source files of your applications will have an immediate effect without reinstalling them.

Now you can import models from your general application (or any other application) anywhere (in other applications, project files, ...).

0
source

Source: https://habr.com/ru/post/974251/


All Articles