简体   繁体   English

ModuleNotFoundError:没有名为“游戏”的模块 | 如何从完全相同的文件夹中的文件中导入内容

[英]ModuleNotFoundError: No module named 'game' | how can I import stuff from a file that is in the exact same folder

I am currently working on a project: a little game, text-based.我目前正在做一个项目:一个基于文本的小游戏。

I made a folder called game , with all my files related to this project.我创建了一个名为game的文件夹,其中包含与该项目相关的所有文件。 I have several, and even the python code is split into a few files to make it easier for me.我有几个,甚至 python 代码也被分成几个文件,以使我更容易。

My directory looks like this:我的目录如下所示:

C:.
    boucle_de_jeu.py
    clss.py
    documentation.md
    fonctions_boucle_jeu.py
    idees.txt
    map_developpement.py
    meta_data.json

--> boucle_de_jeu.py is the main script, it contains the game loop --> clss.py contains the classes of the game's items --> fonctions_boucle_jeu.py contains a few functions used in boucly_de_jeu.py --> map_developpement.py is the file where I create all the instances of the game, with the classes I imported from clss.py --> boucle_de_jeu.py是主脚本,它包含游戏循环 --> clss.py包含游戏项目的类 --> fonctions_boucle_jeu.py包含一些在boucly_de_jeu.py中使用的函数 --> map_developpement.py是我创建游戏所有实例的文件,其中包含我从clss.py导入的类

I tried to run the main file boucle_de_jeu.py , and I got this:我试图运行主文件boucle_de_jeu.py ,我得到了这个:

PS C:\Users\...\python\game> py boucle_de_jeu.py
Traceback (most recent call last):
  File "C:\Users\...\python\game\boucle_de_jeu.py", line 6, in <module>
    import game.map_developpement as map_items
ModuleNotFoundError: No module named 'game'

and these are the first lines of my boucle_de_jeu.py file:这些是我的boucle_de_jeu.py文件的第一行:

# ...
import sys
from os import system

# ...
import game.map_developpement as map_items
import game.fonctions_boucle_jeu as fct_boucle

My question is, what are the ways to make this work?我的问题是,有什么方法可以使这项工作? Maybe I should also reorganize my folder?也许我也应该重新组织我的文件夹?

Thanks in advance for your precious help, have a nice day^^.提前感谢您的宝贵帮助,祝您有美好的一天^^。

To import from game , you should have a file called game.py .要从game导入,您应该有一个名为game.py的文件。 I don't see one in your file list there.我在你的文件列表中没有看到一个。

Instead of this:而不是这个:

import game.map_developpement as map_items

Just do:做就是了:

import map_developpemennt as map_items

I believe that you mean to say all these files are in a folder called game , so the structure really looks like我相信你的意思是说所有这些文件都在一个名为game的文件夹中,所以结构看起来真的像

game/
├── boucle_de_jeu.py
├── clss.py
├── documentation.md
├── fonctions_boucle_jeu.py
├── idees.txt
├── map_developpement.py
└── meta_data.json

Importing files in this manner can be a little tricky and you should be clear about what you hope to achieve.以这种方式导入文件可能有点棘手,您应该清楚自己希望实现的目标。 If this game/ folder is in fact a module that will be used by other projects in the future, then (you need to include an __init__.py file, and) to import from other files in that module you should use the convention.如果这个game/文件夹实际上是一个将来会被其他项目使用的模块,那么(您需要包含一个__init__.py文件,并且)从该模块中的其他文件导入您应该使用约定。

from .map_developpement as map_items

However, if this is the entire project and you are only going to run files in this folder from within the folder, you can use the suggestion made by @jr15:但是,如果这是整个项目,并且您只想从文件夹中运行此文件夹中的文件,则可以使用 @jr15 提出的建议:

from map_developpement as map_items

Note that these are not mutually compatible.请注意,这些不是相互兼容的。 If you wish to use the first option you will need to have all of your testing and whatnot outside of the game/ folder and you will not be able to run files directly from the game/ folder.如果您希望使用第一个选项,您将需要在game/文件夹之外进行所有测试,并且您将无法直接从game/文件夹运行文件。 If you go with the second option you will be unable to use the games/ folder as a separate module.如果您选择第二个选项,您将无法将games/文件夹用作单独的模块。

暂无
暂无

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

相关问题 Python从同目录导入文件:ModuleNotFoundError: No module named - Python import file from the same directory: ModuleNotFoundError: No module named ModuleNotFoundError: 没有名为 &#39;&#39; 的模块。 在同一个文件夹中 - ModuleNotFoundError: No module named '' . in the same folder 导入模块失败:ModuleNotFoundError:没有名为“文件夹”的模块 - Fail to import a Module: ModuleNotFoundError: No module named 'folder' ModuleNotFoundError: No module named - 尝试从另一个文件夹导入文件时 - ModuleNotFoundError: No module named - when attempting to import files from another folder ModuleNotFoundError:没有名为 Foo 的模块 - 如何将模型导入到 Django 应用程序的脚本中? - ModuleNotFoundError: No module named Foo - How can I import a model into a Django app's script? 无法导入 PIL ModuleNotFoundError: No module named &#39;PIL&#39; - can not import PIL ModuleNotFoundError: No module named 'PIL' 来自 main import * ModuleNotFoundError:没有名为“main”的模块, - from main import * ModuleNotFoundError:No module named 'main', 如何使用 import_module 导入模块以解决测试代码中的“ModuleNotFoundError” - How can I import a module using import_module to solve 'ModuleNotFoundError' from test code 导入模块:ModuleNotFoundError:没有模块命名 - Import Module : ModuleNotFoundError: No module named 无法从本地文件夹导入模块? “没有命名的模块” - Can't import module from local folder? "No module named"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM