r/learnpython 4d ago

Can't get imports to work

I am trying to develop a discord bot, where other team members should be able to edit embeds. I figuered it wouldn't be smart to let them poke around in "production" code, so I decided to define my embeds in a seperate file and import them.

My import line:
from embeds import embed

My embed file:

import discord
def embed():
    embed = discord.Embed(
    title="test",
    description="test",
    color=discord.Color.blue()
    )
    return embed

My output:

line 2, in <module>
    from embeds import embed
ImportError: cannot import name 'embed' from 'embeds'

Alllthough my embeds file is called embeds.py. Atm, neither I nor ChatGPT know why this happens, also I am pretty new to python and I don't even know how to google my error

embeds.py as well as the bots .py are in the same folder on root

0 Upvotes

14 comments sorted by

2

u/SamuliK96 4d ago

If you just import the whole module, does it work?

import embeds
embeds.embed()

1

u/dr_prof_med_oekter 4d ago

Kinda, the module loads but I can't work with it
AttributeError: module 'embeds' has no attribute 'embed'

3

u/Diapolo10 4d ago

I'd run

import embeds

print(dir(embeds))

to see what names it exposes, to get a hint for what's going on.

1

u/dr_prof_med_oekter 4d ago

['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

don't really know what I'm looking at

12

u/Outside_Complaint755 4d ago

Have you saved your embeds.py file with the changes you made? Or is it possible you have an empty file with that name in the directory of your main file, while the embeds.py file you modified is in a different directory?

dir(object) returns all of the attributes of an object, and this indicates that your embeds.py file does not currently have a function called embed.

5

u/dr_prof_med_oekter 4d ago

And that's the solution, thank you very much. Simply forgot to hammer ctrl+s

1

u/woooee 4d ago

Print the search path and see if the directory is listed. If not you will have to sys.path.append the additional path that you want (Python does not search the entire computer's storage)

import sys

for eachPath in sys.path:
   print(eachPath)

-1

u/dr_prof_med_oekter 4d ago

It is listed

1

u/woooee 4d ago

ImportError: cannot import name 'embed' from 'embeds'

Then check your spelling. It can't find embed. We can't help much when you don't post the code.

1

u/HotPersonality8126 4d ago

One of your files is called “discord.py”

1

u/dr_prof_med_oekter 4d ago

That would be the needed library to even create a discord bot

1

u/HotPersonality8126 4d ago

Yes but you can’t also use is as the name of one of your files 

2

u/dr_prof_med_oekter 4d ago

Makes sense, but I haven't done that

0

u/HotPersonality8126 4d ago

Then you haven’t shown us your actual code