【发布时间】:2017-11-05 06:27:09
【问题描述】:
我是 Django 新手(所以这可能是一个 Python3 问题)。另外,我已经阅读了其他类似的问题,例如Error importing Python module。
我有一个目录结构如下:
--
my_class-- --
__init__.py-- --
custom_class_1.py-- --
custom_class_2.py
在文件 custom_class_2.py 的位置有以下几行:
from custom_class_1 import Custom_Class_1
class Custom_Class_2:
self.thing = Custom_Class_1()
即一个类是建立在其他类之上的。
使用先前链接问题中的solution
这会导致 Django 服务器吐出这个错误:
from custom_class_1 import Custom_Class_1
ImportError: No module named 'Custom_Class_1'
这让我觉得我在定义一个类时没有遵循 pythonic 约定,建立在另一个类上(但都在同一个目录中)。
我做错了什么以及如何解决?
【问题讨论】:
-
试试
from .custom_class_1 import Custom_Class_1是否有效(custom_class_1前面有一个点表示相对导入)。 -
那个 Django 错误消息没有意义——它说
Custom_Class_1是一个模块。你确定那是实际的错误信息吗? -
@MSeifert 似乎有效!!
-
@JohnGordon 是的,我确定。这就是终端在运行服务器时所写的内容,包括
my_class
标签: python django python-3.x