【问题标题】:What is the format for importing python variables and functions from another file in another directory从另一个目录中的另一个文件导入python变量和函数的格式是什么
【发布时间】:2021-07-15 06:22:25
【问题描述】:

目前在 discord.py 中开发一个机器人,我有这些导入,它们从不同的 py 文件中获取不同的函数和/或变量:

from core.db.read import return_all_document_id
from core.extras.terminal import database_Critical
from core.db.write import New_Member
from core.main import *
import discord
from discord.ext import tasks, commands

有人告诉我,例如,使用 core.db.read 是正确的方法,但我得到了这个回溯:

Traceback (most recent call last):
  File "c:\Users\mj\Documents\Pycharm\wake\core\extensions\events.py", line 1, in <module>
    from core.db.read import return_all_document_id
ModuleNotFoundError: No module named 'core'"

有没有更好的方法来做到这一点?如果不是,那可能是我的问题?

My folder Structure

【问题讨论】:

  • 这取决于你如何运行它。如果您自己运行events.py,那么显然它不知道如何找到core 模块。
  • 我想我在这里回答了:stackoverflow.com/a/68375393/13944524

标签: python import


【解决方案1】:

尝试使用这个,要从另一个文件夹导入模块,我们可以尝试将一个名为 'init.py' 的空文件(在 init 的两侧使用双下划线)放入该文件夹并使用带点符号的相对路径 因为它向 Python 发出信号,表明该文件夹应被视为包

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-12
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 2013-06-19
    相关资源
    最近更新 更多