When importing the Tkinter module, VS Code gives me a 100+ warning messages about unused imports. Is there a way to turn this off? Or should I be doing something different to avoid this?
Becuase you're using from tkinter import * python is importing everything in the tkinter module. You then get a warning for each class in tkinter that you aren't using. You can either just import the classes you need (from tkinter import Tk), or turn off the warnings in visual studio settings (Tools > Options > Python).
Related
If I write this code:
import math
math.abcdef()
where clearly the method abcdef() does not exist in the imported math module, why doesn't visual studio code show an error?
Is there a way to force vscode to check that the called method exists in the imported module?
Such error messages in vscode are generally provided by linting.
You can enable linting in settings. The error messages obtained by selecting different linter are not exactly the same.
If you don't want use linting, change the python.analysis.typeCheckingMode option in the settings to basic or strict.
At this point, the Python extension prompts an error message.
basic
strict
I am new to Python Programming.
I am using tkinter module to import stuff for graphical user interface.
I import everything from the tkinter module by typing the following statement:
from tkinter import *
Why do I still need to import some certain classes separately from the same module like simpledialog although we have imported everything using the above statement from tkinter?
from tkinter import simpledialog
Without importing simpledialog separately, it throws an error.
Yes, it does throw the error because simpledialog is not directly the module of tkinter.
Basically, the module is every file, which has the file extension .py and consists of proper Python code. There is no special syntax required to make such a file a module. A module can contain arbitrary objects, for example files, classes or attributes. All those objects can be accessed after an import.
If you will do dir(tkinter) without importing the simpledialog explicitly, it shows the results except simpledialog in it.
This is because of the library structure. It doesn't import all the modules of tkinter automatically. Once you use import tkinter.simpledialog it will show you the simpledialog module in it, which means simpledialog was never imported from tkinter earlier.
Also, it is recommended to use import tkinter instead of from tkinter import astrik, except when working in the interactive Python shell. One reason is that the origin of a name can be quite obscure, because it can't be seen from which module it might have been imported.
I don't understand how this happened. myscript.py works, Tkinter is installed. However once I converted it to .exe using pyinstaller, I ran myscript.exe, I got a message in command prompt saying "No module named Tkinter".
How can I fix this?
I've already tried to convert it several times, tried using --noupx, and also tried reinstalling pyinstaller. Thanks
PyInstaller - Hidden Imports
Try using the --hiddenimport flag. Do --hiddenimport=Tkinter, or replace the Tkinter with whatever module you need. The issue seems reoccurring throughout the PyInstaller community. Here is one article.
Name Space
Tkinter has changed its namespaces from Python 2 to Python 3. It's now named tkinter in Python 3 and Tkinter in Python 2. This is the code I like to use so it's cross-version.
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
Then you can reference Tkinter as tk. Or if you do from Tkinter import * you can just use everything without a namespace. See the list here for more of the name changes between version.
I'm new to PythonAnywhere, and when I try to import tkinter using Flask putting from tkinter import *
after the line from flask import Flask, it shows an error saying: "'from tkinter import *' used; unable to detect undefined names".
How do I have to import tkinter, then?
Your code editor is automatically running a linter like flake8 or pyflakes, and it is this linter that tells you that it cannot detect common coding errors because you are using a from <modulename> import * statement. That's because a linter cannot know up front what names that statement will import.
In other words, this is not a Python error. Nor is it specific to PythonAnywhere.
You can avoid the message by importing only what you are actually using in your code:
from tkinter import Frame, StringVar, Label
I've just installed PyCharm Community Edition 3.4.1 and tried to make a simple pygame project in it. I found that code completion runs in a weird way. In this case:
from pygame import event
event.
when I type event. a completion popup with event methods shows immediately. But in the second case:
import pygame
pygame.event.
a popup contains only object methods.
How can I learn the autocomplete tool to look deeper into the library?
Other than creating your own skeletons, you can't. You can make pycharm a little better a code completion if you enable the following:
But other than that, you're out of luck. Python is hard to make code completion for because its a dynamic language, and stubs (skeletons) don't exist for everything.
I tried Daid's answer (removing the try/except in init.py) and it didn't work, but it was very close! Here is how you can fix it specifically for pygame:
Go to your pygame folder and open init.py in a text editor
Navigate to the import section with the try/except clauses (around line 109)
Change the format from import pygame.module to from pygame import module for the modules you want
For example, change
try: import pygame.event
to
try: from pygame import event
Restart PyCharm and it should work :)
It has to do with how pygame is constructed.
The:
python\Lib\site-packages\pygame\__init__.py
File contains the following construction:
try: import pygame.cdrom
except (ImportError,IOError):cdrom=MissingModule("cdrom", geterror(), 1)
Which allows missing imports. However, this confuses pycharm. Removing the try+except will fix the pycharm auto completion.