Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python same module in different file

I have a MVC project and I would to import my module like that:

 import projet

 view = projet.view()

 controller = projet.controller()

 model = project.model()

but, I'd like that controller(), model() and view() are in different files. How to do a module (project) but with different file in without import the other files after?

like image 984
Epitouille Avatar asked Mar 21 '23 19:03

Epitouille


1 Answers

Create directory named project, create file __init__.pyin this dir, put there code:

from view import *
from controller import *
from model import *

To the same dir put your view.py, controller.py, model.py

When you do

import project

all other imports would be done automatically (from __init__.py). This is called packages (directory project will become package name, packages are detected by existence of __init__.py).

Further reading: http://www.network-theory.co.uk/docs/pytut/Packages.html

like image 172
nickzam Avatar answered Apr 02 '23 03:04

nickzam