#๐Ÿ”’ Import error

104 messages ยท Page 1 of 1 (latest)

stone moon
dark folioBOT
#

@stone moon

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.

ebon raven
#

hello!

stone moon
#

Hii

wraith iron
#

Any reason you can't run discord on your PC and just copy and paste the code?

stone moon
#

Dont know my login sadly

ebon raven
#

thats not constructive

stone moon
ebon raven
#

okay so the import keysThingy works

#

weird

stone moon
#

But keyMaps isnt being recognised

#

The functions code works when its in the main code file

#

So it is purely an importing issue

lament mortar
ebon raven
#

hmm

stone moon
ebon raven
#

try doing from keyThingy import keyMaps

#

its very weird, the only thing i can think of is that its not recognising it as a function

stone moon
ebon raven
#

wait whats the full error msg?

#

and can you show the file structure

stone moon
#

Its recognising it as a function, just isnt importing it

lament mortar
#

it's because you have a circular import (I believe)

stone moon
stone moon
#

Omg that was it

ebon raven
#

ohhhhhhhhhhhh

#

welp good job

#

you solved it

stone moon
#

Wait it works without needing the Info class??

#

Or is it just using the Info class because its called in the main code file

ebon raven
#

new modules are for enclosed things that only return stuff and stuff

lament mortar
stone moon
lament mortar
#

running it will give you a NameError

ebon raven
stone moon
lament mortar
#

if you get circular imports, then you need factor out the dependency to another module, so you can import it separately, or put it all in one

stone moon
#

Enter key works ๐Ÿ˜

#

Tysm

lament mortar
stone moon
lament mortar
#

it makes it unclear what names are available as well

stone moon
#

Mhm

lament mortar
#

either:

import mod

to namespace it, or:

from mod import a, b, c
stone moon
#

But yeah this has been running me ragged for the last hour ๐Ÿ˜ญ tysm

#

@lament mortar while youre here can i ask you a question?

lament mortar
#

ok

stone moon
#

I was thinking of just doing an if statement for every individual key when typing

#

Is there a more efficient way to do that with pygame?

lament mortar
#

I don't know what you're looking to do

stone moon
#

Without using the inbuilt text input, im trying to make a text editor from scratch

lament mortar
stone moon
stone moon
lament mortar
#

that doesn't really answer my question

stone moon
#

Itd be something like:

if key == pygame.K_a:
Put an a on the screen
If key == pygame.K_b:
Put a b on the screen

stone moon
lament mortar
stone moon
#

Yeah, but for every individual key

#

Which would be very convoluted

lament mortar
#

you could define 52ish functions for each letter

#

then map those functions using a dict to the letters

winged anvil
lament mortar
#

there should be some attribute or method which tells you the name of the key from the event, as a string

winged anvil
#

it resolves like 99% of import issues for me

winged anvil
stone moon
#

Depending of if the key is pressed or not

lament mortar
stone moon
#

Yeah that wouldntve fixed the issue

winged anvil
#

if the situation gets desperate I end up modifying pythonpath

lament mortar
#

I feel like you don't understand what a circular import is

winged anvil
stone moon
winged anvil
lament mortar
winged anvil
#

I swear it just makes all import related errors vanish

lament mortar
#

circular imports are possible, but they make for confusing code

winged anvil
#

it's obvious

stone moon
lament mortar
#

you have an argument called Info

stone moon
#

Yeah that was one of my debugging attempts lol

stone moon
lament mortar
#

it can't do that

stone moon
#

Mhm yeah i get that now

lament mortar
#

here's a test to see how a circular imports runs.

# a.py
print("a1")
import b
print("a2")
x = 1
print("a3", x)
# b.py
print("b1")
import a
print("b2", a.x)
a.x = 2
print("b3", a.x)
#

If you have both files, and run a.py, you get this

a1
b1
a1
a2
a3 1
b2 1
b3 2
a2
a3 1
stone moon
#

Wait importing just runs the code from the file?

lament mortar
#

and here's labels for which import they are running at

a1   # original run of `a.py`

b1   # `import b` from a

a1   # `import a` from b
     # skip over `import b`
a2   # continue with `import a` from b
a3 1 # finish `import a` from b

b2 1 # continue `import b` from a
b3 2 # finish `import b` from a

a2   # continue original run of `a.py`
a3 1 # finish original run of `a.py`
stone moon
#

Omg like a year ago i used to convert my code files to text files and use exec() on them ๐Ÿ˜ญ๐Ÿ˜ญ

lament mortar
#

and then you get a module object which has the variables as attributes

dark folioBOT
#
Python help channel closed for inactivity

This help channel has been closed. 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.