Ced
Ced

Reputation: 1301

python project with different subfolder

Hello I have a python application which is structured in this way.

--project
    --a_subfolder
        --a_file.py
    --another_subfolder
        --another_file

Is there a clean solution to handle dependencies between those files? And is it really good practice to do stuff like that:

import a_file
x = a_file.AClass()

Upvotes: 1

Views: 91

Answers (2)

Tony
Tony

Reputation: 10357

Edited after comment from Ced

It sounds like you are working on a large Django project, if that's the case then you are correct to do what you suggest. Put your models in one folder and views in another.

In fact, in a large Django project I have I use this method of organising files; like this example:

+-project
|
+--models
   +--__init.py__
   +--cars.py   [car, van & truck classes]
   +--boats.py  [boat & ship classes]

Import the classes you want to expose from each sub folder in the __init.py__ file of the models folder.

Models init.py

from cars import car, van, truck
from boats import boat, ship

In your views you can then reference the boat and car models with a shorter notation

from project.models import car, truck, ship

So the meandering answer to your question is yes, you can organise files in separate folders and then reference between them.

Upvotes: 1

mmenschig
mmenschig

Reputation: 1128

you can do something like

from a_subfolder import a_file as afile
from another_subfolder import another_file as another

x = afile.AClass() is perfectly valid.

Upvotes: 1

Related Questions