[英]ModuleNotFoundError when importing local .py file
I've tried looking everywhere about this problem but I cannot find the solution.我试过到处寻找这个问题,但我找不到解决方案。 I am also new to Python so forgive me, but I am trying to import my local readMessages.py file into my TextReactions.py file.
我也是 Python 的新手,所以请原谅我,但我正在尝试将我的本地 readMessages.py 文件导入到我的 TextReactions.py 文件中。 I've tried import readMessages, from readMessages import take_screenshot, but I still get the error ModuleNotFoundError: No module named 'readMessages'.
我试过 import readMessages, from readMessages import take_screenshot,但我仍然收到错误 ModuleNotFoundError: No module named 'readMessages'。 Thank you for any help in advanced!
感谢您在高级方面的任何帮助!
from readMessages import take_screenshot
from dotenv import load_dotenv
# Loads the .env file that resides on the same level as the script.
load_dotenv()
# Grab the API token from the .env file.
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
import requests
import json
import discord
import os, os.path
import pyautogui
class readMessages:
os.system('cls')
list = os.listdir('C://Users//gamer//Documents//Pythong//Screenshots')
count = len(list)
def retrieve_messages(channelid):
print("MESSAGES BELOW:")
If you wish to import local modules, then you should add an __init__.py
file in the directory of the python file you want to import.如果要导入本地模块,则应在要导入的 python 文件的目录中添加
__init__.py
文件。 The __init__.py
can be empty. __init__.py
可以为空。
Python has to types of imports, global and relative. Python 具有导入类型,全局和相对。
With the statement:随着声明:
from readMessages import take_screenshot
You are trying a global import of a module present in your installed libraries.您正在尝试对已安装库中存在的模块进行全局导入。
You should try a relative import:您应该尝试相对导入:
from .readMessages import take_screenshot
If this does not work, your working directory is probably misconfigured, you should update it on your ide.如果这不起作用,您的工作目录可能配置错误,您应该在 ide 上更新它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.