[英]python same module in different file
I have a MVC project and I would to import my module like that: 我有一个MVC项目,我会像那样导入我的模块:
import projet
view = projet.view()
controller = projet.controller()
model = project.model()
but, I'd like that controller(), model() and view() are in different files. 但是,我希望控制器(),model()和view()在不同的文件中。 How to do a module (project) but with different file in without import the other files after?
如何做一个模块(项目),但有不同的文件,而不导入其他文件后?
Create directory named project
, create file __init__.py
in this dir, put there code: 在这个目录中创建名为
project
目录,创建文件__init__.py
,放在那里代码:
from view import *
from controller import *
from model import *
To the same dir put your view.py
, controller.py
, model.py
对于同一个目录放置你的
view.py
, controller.py
, model.py
When you do 当你这样做
import project
all other imports would be done automatically (from __init__.py
). 所有其他导入将自动完成(来自
__init__.py
)。 This is called packages (directory project
will become package name, packages are detected by existence of __init__.py
). 这称为包(目录
project
将成为包名,通过__init__.py
存在检测包)。
Further reading: http://www.network-theory.co.uk/docs/pytut/Packages.html 进一步阅读: http : //www.network-theory.co.uk/docs/pytut/Packages.html
You could make a project.py
file that looks like this: 你可以制作一个看起来像这样的
project.py
文件:
from viewModule import view
from controllerModule import controller
from modelModule import model
Then your above code should work 那么你的上面的代码应该工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.