简体   繁体   English

python在不同文件中的相同模块

[英]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.pycontroller.pymodel.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.

相关问题 为什么python json模块在同一文件上产生不同的编码 - Why python json module produces different encoding on same file Python:从模块导入时生成不同的随机选择(不同的 python 文件) - 保持相同的 output - Python: generate a different random choice when importing from module (different python file) - Keep getting same output Python:具有相同名称的模块和文件 - Python: Module and file with the same name Python:导入具有相同名称的不同模块 - Python : importing different module with same name 从Python中的不同包中加载相同的模块 - Load the same module from different packages in Python 在包中导入Python模块会从同一个包中返回不同的模块 - Importing a Python module in a package returns a different module from the same package 从与python模块相同的目录中读取文件 - Reading file from the same directory as the python module Python 日志记录模块始终使用相同的文件 - Python logging module always uses the same file 在python中调用模块来处理同一个文件。 它适用于一种情况,但不适用于另一种情况,我无法弄清楚有什么不同 - Calling a module in python to process the same file. It works in one scenario, but not in the other, and I can't figure out what's different 使用python gdal模块打开相同投影但大小不同的图像 - Using the python gdal module to open images in the same projection but different sizes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM