简体   繁体   English

无法从 django 应用程序父目录中的子目录导入模块

[英]cannot import modules from subdirectories that are in the django app parent directory

Django doesn't seem to be able to import modules from a subdirectory. Django 似乎无法从子目录导入模块。

I've got a file structure like this:我有一个像这样的文件结构:

->project_folder ->项目文件夹

---->app_folder ---->app_folder

------->subdir ------->子目录

when I store a script in app_folder, i can import it, so the command 'from project_folder.app_folder.module import *' works, but I get a module not found error when I do 'from project_folder.app_folder.subdir.module import *'当我将脚本存储在 app_folder 中时,我可以导入它,因此命令 'from project_folder.app_folder.module import *' 有效,但是当我执行 'from project_folder.app_folder.subdir.module import *' 时出现模块未找到错误'

How do I get around this?我该如何解决这个问题? I just want to keep my files nicely organized我只想让我的文件井井有条

have you got an __init__.py in your subdir?你的子目录中有__init__.py吗? Python needs this file to treat a directory as a package. Python 需要此文件将目录视为 package。

put an import in the init file of your app (not subdirecotry).在您的应用程序的初始化文件中放入一个导入(不是子目录)。 I have this structure and the following import in init.py file app/views_dir/group_views.py The following line is the only line of code in my init file我在 init.py 文件 app/views_dir/group_views.py 中有这个结构和以下导入 以下行是我的 init 文件中唯一的代码行

from views_dir.group_views import test从 views_dir.group_views 导入测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM