Why do you show me errors when importing tkinter into VSC?

1

It's very strange because my code is running well but this kind of error keeps coming up:

    
asked by Melissa Alvarez 18.12.2018 в 11:35
source

1 answer

1

You are using a syntax discouraged:

from modulo import *

since that * represents all the symbols defined in that module , which is discouraged for several reasons:

  • Readability If you use different modules and in all of them you make a from modulo import * , when later in the code you find a call to a function, how do you know which module it came from? This is important for someone else who reads your program without knowing all the modules used and wants to search for information about a particular function. Even your future I counts as another person .
  • Avoid collisions. What happens if the module foo defines a symbol called contar , and the module bar also? When you do from foo import * and then from bar import * , which of the two will be used when calling contar() ? The answer depends on the order in which the amounts, which can cause a multitude of problems.
  • The recommended method is simply:

    import modulo
    

    and then, every time you use a symbol defined in that module, you use modulo.simbolo . This eliminates both problems mentioned above.

    If the name of the module is too long and you are too lazy to type it, you can always do the following:

    import modulo as m
    

    Then you can use m.simbolo instead of modulo.simbolo . This avoids the second problem although it incurs a bit in the first one. It is seen very frequently with pandas , which is imported as pd and numpy that is imported as np .

    If you want to avoid the module prefix, and you are only going to use a few symbols of that module, you can put:

    from modulo import simbolo1, simbolo2
    

    and that way your program can use simbolo1 and simbolo2 instead of modulo.simbolo1 and modulo.simbolo2 . This incurs a bit in problem 1 (although you can always look for the place of import to see what module it came from), and if you are not careful also in problem 2, but less by forcing yourself to be more explicit you decrease the probability of collisions of names.

    VSCode uses linting as you type, which is a process that examines your code to search for possible errors and bad practices . According to what linter you use, importing things you do not use afterwards is considered a bad practice ( warning ).

    If your program has a from modulo import simbolo1 , and then does not use simbolo1 , the linter will complain. Naturally the problem is much more serious if you use a from modulo import * , since then you have many more symbols to complain about.

        
    answered by 18.12.2018 / 11:49
    source