Nameerror: name 'mostrar_urls' is not defined

Asked

Viewed 665 times

-1

Good afternoon!
I’m trying to develop a project/idea I had, only when I went to put the "menubar" in other "parts of the programme" the program does not "whether" run further, can anyone help me resolve this error?

Error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "ajuda_redes.py", line 21, in ajuda_redes
    ajuda.add_command(label='URLs', command=mostrar_urls)
NameError: name 'mostrar_urls' is not defined

Yes, I did give from import mostrar_urls * in help_networks.py
And yes, the files show_urls.py and help_networks.py are in the same folder.

EDIT(1):
Error:

Traceback (most recent call last):
  File "deep-web-url-aleatoria-develop/src/main.py", line 6, in <module>
    from mostrar_urls import mostrar_urls
  File "deep-web-url-aleatoria-develop/src/mostrar_urls.py", line 5, in <module>
    from ajuda_redes import ajuda_redes
  File "deep-web-url-aleatoria-develop/src/ajuda_redes.py", line 5, in <module>
    from mostrar_urls import mostrar_urls
ImportError: cannot import name 'mostrar_urls' from 'mostrar_urls' (deep-web-url-aleatoria-develop/src/mostrar_urls.py)

EDIT(2):
Guys, I was able to solve the error(s), I switched the "snippets" who were "making a mistake":
from arquivo_exemplo import funcao_exemplo
p/ import funcao_exemplo
exemplo.add_command(label='exemplo', command=funcao_exemplo)
p/ exemplo.add_command(label='exemplo', command=arquivo_exemplo.funcao_exemplo)

I just don’t understand why I can’t put in all three files (main.py, mostrar_urls.py and ajuda_redes.py) otherwise (recommended by PEP8):

from arquivo_exemplo import funcao_exemplo
exemplo.add_command(label='exemplo', command=funcao_exemplo)

Thus "gives" the Import of EDIT(1), unless it is placed only in main.py.

  • If you need to link one branch entire in your question, you urgently need to read about creating a [mcve].

  • @Andersoncarloswoss, okay, thanks for the tip.

1 answer

0

One way is to specify the function you want to use from the file arquivo_exemplo thus: from arquivo_exemplo import função_que_quero_chamar. In this case, when calling the function, you do not need to point out where it comes from. In your case, just do:

from mostrar_urls import mostrar_urls
...
ajuda.add_command(label='URLs', command=mostrar_urls) # continua igual

Note that this way follows the good practices defined by Pep8, as can be seen here, makes clear the names of the imported functions.

  • I would like to thank you for your reply, particularly on the PEP 8, but now it’s "giving" another error =/ , I will edit the "body of the question", since there is no p/ error "current" complete here.

  • On the example of from arquivo import *, I believe you’re mistaken when it comes to the use of arquivo.funcao_somar(1, 2), for example. When using "*", we should only use the function name. That is, in my example the correct call would be funcao_somar(1, 2).

  • 1

    Really @Breno, thanks for pointing out.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.