python - 即使模块与我正在使用的文件位于同一目录中,也无法导入模块
问题描述
即使模块位于我现在工作的同一目录中,我也无法导入模块(E:\Python Projekti\Python Crash Course)。我试图在这里找到解决方案,我以为我用 .module_name 找到了它,但它仍然不起作用。
我尝试了 .module_name 方法,它为我提供了包含所有可能模块的下拉菜单,但最后,它在终端中给了我一个错误。
from .restaurant_module import Restaurant
restaurant = Restaurant('Madera', 'Soulfood')
restaurant.describe_restaurant()
Traceback (most recent call last):
File "E:/Python Projekti/Python Crash Course/Chapter
9/imported_restaurant.py", line 1, in <module>
from .restaurant_module import Restaurant
ModuleNotFoundError: No module named '__main__.restaurant_module';
'__main__' is not a package
解决方案
我会尝试 Jammy 的建议:import restaurant_module
或者尝试在同一个目录中添加一个空白文件,__init__.py
看看是否可行。
编辑:这个 repo 能帮助你构建它吗?https://github.com/ehmatthes/pcc
推荐阅读
- reactjs - 错误“自定义关键字定义无效:data.errors 应该是布尔值”
- c - 指向函数初始化的双指针数组
- django - VS 2017 Python/Django 模板 - CSS 不起作用,但内联样式起作用
- python - 使用 numpy,如何生成一个数组,其中每个索引处的值是第二个数组中从 0 到同一索引的值的总和?
- java - 距离矩阵与 Apache Poi
- python-3.x - Scipy 最小化:如何将参数传递给目标和约束
- python - 数据框用空白替换下划线不起作用
- c++ - 实例化和特化的定义相互依赖
- c++ - 如何在 C++ 中创建二维数组
- scorm - 我可以通知两个 LMS' 已完成 LTI 工具提供商的课程吗?