ModulenotFoundError:无模块

发布于 2025-02-12 01:46:54 字数 396 浏览 0 评论 0原文

我一直在编写的代码上遇到这个问题,我无法弄清楚原因。

我的项目结构:

├──code
|  ├── Bot_Tracker
|  |   ├── __init__.py
|  |   ├── matchhistory.py
|  |   ├── discordUI.py
|  ├── Tests
|  |   ├── __init__.py
|  |   └── test_matchhistory.py

test_matchhistory.py中的导入语句:

from Bot_Tracker.matchhistory import compMatchHistory

我不明白为什么我会继续遇到错误。

I keep having this issue with the code I had written, I cant figure out why.

My project structure:

├──code
|  ├── Bot_Tracker
|  |   ├── __init__.py
|  |   ├── matchhistory.py
|  |   ├── discordUI.py
|  ├── Tests
|  |   ├── __init__.py
|  |   └── test_matchhistory.py

My import statements in test_matchhistory.py:

from Bot_Tracker.matchhistory import compMatchHistory

I don't understand why I keep getting the error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殊姿 2025-02-19 01:46:54

这是一个简单的问题,与Python如何处理包装的存储位置有关。如果软件包不在Python路径中,则Python将无法在导入上访问它。

为了解决这个问题,我需要将软件包目录和父添加到系统路径的软件包目录。

我使用的代码:

from os import path
import sys

if __package__:
    parent_dir = path.dirname(__file__)
    root_dir = path.dirname(parent_dir)
    if parent_dir not in sys.path:
        sys.path.append(parent_dir)
    if root_dir not in sys.path:
        sys.path.append(root_dir)

此代码基本上找到了当时存储文件的目录以及该目录的父,然后检查该目录的路径是否存在于sys.path中。

This was a simple problem related to how Python works out where packages are stored. If the package is not in the python path, then python won't be able to access it on the import.

To solve this I needed to add the package directory and the parent to the package directory to the system path.

The code I used:

from os import path
import sys

if __package__:
    parent_dir = path.dirname(__file__)
    root_dir = path.dirname(parent_dir)
    if parent_dir not in sys.path:
        sys.path.append(parent_dir)
    if root_dir not in sys.path:
        sys.path.append(root_dir)

This code basically finds the directory where the file is stored then and the parent of that directory, after which it checks if the paths to this directories exist in sys.path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文