简体   繁体   中英

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. I have several, and even the python code is split into a few files to make it easier for me.

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

I tried to run the main file boucle_de_jeu.py , and I got this:

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:

# ...
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 . 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/
├── 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.

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:

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. If you go with the second option you will be unable to use the games/ folder as a separate module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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