#πŸ”’ Python code cant import function from different python file in the same folder

30 messages Β· Page 1 of 1 (latest)

rigid remnant
#
from typing import Final
import os
from dotenv import load_dotenv
from discord import Intents, Client, Message
from responses import get_response

load_dotenv()
TOKEN: Final[str] = os.getenv('DISCORD_TOKEN')

intents : Intents = Intents.default()
intents.message_content = True #NOQA
client: Client = Client(intents=intents)

async def send_message(message: Message, user_message: str) -> None:
    if not user_message:
        print("Message was empty because intents were probably not enabled")
        return
    if is_private := user_message[0] == "?":
        user_message = user_message[1:]
    try:
        response: str = get_response(user_message)
        await message.author.send(response) if is_private else await message.channel.send(response)
    except Exception as e:
        print(e)

# handling startup
@client.event
async def on_ready() -> None:
    print(f"{client.user} is now running!")

@client.event
async def on_message(message: Message) -> None:
    if message.author == client.user:
        return
    
    username: str = str(message.author)
    user_message: str = message.content
    channel: str = str(message.channel)

    print(f"[{channel}] {username}: {user_message}")
    await send_message(message, user_message)

#MAIN ENTRY POINT
def main() -> None:
    client.run(token=TOKEN)



if __name__ == "__main__":
    main()
shy hullBOT
#

@rigid remnant

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

rigid remnant
#

Traceback (most recent call last):
File "c:\Users\Public.DESKTOP-G9TTK92\Documents\discord\main.py", line 5, in <module>
from responses import get_response
ImportError: cannot import name 'get_response' from 'responses' (c:\Users\Public.DESKTOP-G9TTK92\Documents\discord\responses.py)

#

Ive tried to find a solution and may suck at finding it, but I couldnt figure out why it cant import it

#

VSCode auto suggests both the file and function so it does ecognize that it exists

green robin
#

What is the content of reponse.py?

#

And does response import main?

rigid remnant
green robin
#

That is literally the only thing in the file?

rigid remnant
#

Yeah

#

Am I missing something?

green robin
#

I would make sure that you're looking at the code that you think you are, and that all the code is saved.

rigid remnant
#

Its a small code because I havent added much functionality to it, but it has everything I have written down

#

And isn't missing anything Ive had written down

green robin
#

I'd say the code you're think you're running isn't saved, or is a different file from what's actually being run.

#

The only other possible cause I can see is apparently mismatched OS newline characters can cause this somehow, but VSCode should catch that.

#

The three main causes of that error from what I can see:

  1. A circular import
  2. The name being imported is wrong
  3. Using the wrong newline character.

If that's the entire content of responses, 1 is out, so that leaves 2 and 3, which could both be caused by running the wrong file or not saving the code.

rigid remnant
#

OMG Im so sorry saving somehow fixed it, I thought python didnt need the files to be manually saved so I didnt do that

green robin
rigid remnant
#

Ohh okay

#

Well huge thanks for your help!

green robin
#

VSCode shows a blue dot in the tab if the code is unsaved.

#

Btw

#

It's also just a good habit to obsessively hit ctrl+s while writing. I hit ctrl+s so often I accidentally do it on webpages sometimes after typing.

#

And you're welcome.

rigid remnant
#

Yeah i will remember to do that, I do that when programming in php during school because it doesnt save automatically on my school pc, will make sure to make that my habit now

#

Thanks again!

shy hullBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

πŸ”’ Python code cant import function from different python file in the same folder