首页 > 解决方案 > 即使模块与我正在使用的文件位于同一目录中,也无法导入模块

问题描述

即使模块位于我现在工作的同一目录中,我也无法导入模块(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

标签: python

解决方案


我会尝试 Jammy 的建议:import restaurant_module或者尝试在同一个目录中添加一个空白文件,__init__.py看看是否可行。

编辑:这个 repo 能帮助你构建它吗?https://github.com/ehmatthes/pcc


推荐阅读