简体   繁体   English

Python:尝试从同一目录导入文件时出现“ModuleNotFoundError”

[英]Python: 'ModuleNotFoundError' when trying to import file from same directory

Running Python 3.10.运行 Python 3.10。 I have three files in the same directory named Chess, one of which is a __init __.py to make sure it's considered a module.我在同一个名为 Chess 的目录中有三个文件,其中一个是__init __.py以确保它被视为一个模块。

In one file, ChessMain.py I have a line: from Chess import ChessEngine在一个文件 ChessMain.py 我有一行: from Chess import ChessEngine

When I run the file, I get the ModuleNotFoundError: No module named 'Chess' error.当我运行该文件时,我收到ModuleNotFoundError: No module named 'Chess' error.

Sorry if this is a dumb import question, but I can't seem to figure out what I'm doing wrong.抱歉,如果这是一个愚蠢的导入问题,但我似乎无法弄清楚我做错了什么。

This Might help这可能会有所帮助

Usually when I import a module from the same directory, lets say通常当我从同一目录导入模块时,可以说

module_1.py模块_1.py

and

Modile_2.py模块_2.py

And if I want to import it into my main.py file, I simply use如果我想将它导入到我的main.py文件中,我只需使用

import module_name

If they are in the same directory then you should be able to import a class or function from the file based off of its name:如果它们在同一目录中,那么您应该能够根据文件名从文件中导入 class 或 function:

from Chess import <class-name>

If that doesn't work, you can try (as others have said):如果这不起作用,您可以尝试(正如其他人所说):

from .Chess import <class-name>

Or you can try:或者您可以尝试:

from sys import path
path.append(<absolute-path-to-your-directory>)
from Chess import <class-name>

The first example worked fine for me in python 3.7.3.第一个示例在 python 3.7.3 中对我来说效果很好。

暂无
暂无

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

相关问题 尝试导入 python 文件时出现 ModuleNotFoundError,该文件本身会在同一目录中导入文件 - ModuleNotFoundError when trying to import python file which itself imports a file in the same directory Python从同目录导入文件:ModuleNotFoundError: No module named - Python import file from the same directory: ModuleNotFoundError: No module named Python:尝试从同一 package 导入模块时出现“ModuleNotFoundError” - Python: 'ModuleNotFoundError' when trying to import module from the same package python:尝试从目录导入文件时出现“moduleNotFoundError” - python : `moduleNotFoundError` when attempting to import file from a directory 尝试将文件从一个文件夹导入到另一个目录中的另一个文件时出现 ModuleNotFoundError - ModuleNotFoundError when trying to import file from one folder into another file located in another directory 尝试从不同级别的外部目录导入方法时出现 Python ModuleNotFoundError - Python ModuleNotFoundError when trying to import a method from an outer directory of different level 从 python 中的常量文件导入时出现 ModuleNotFoundError - ModuleNotFoundError when import from constants file in python 从另一个目录导入 Python 文件引发 ModuleNotFoundError - Import Python file from another directory raises ModuleNotFoundError Python:尝试从另一个编码 .py 文件导入模块时出现“ModuleNotFoundError” - Python: 'ModuleNotFoundError' when trying to import module from another coding .py file Python:尝试从文件导入模块时出现“ModuleNotFoundError” - Python: 'ModuleNotFoundError' when trying to import module from files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM