How do I import Tkinter? [duplicate] - python

This question already has answers here:
ImportError when importing Tkinter in Python [duplicate]
(3 answers)
Closed 6 years ago.
When I try to import tkinter, this is the message I get:
Traceback (most recent call last):
File "Num_inc_dec.py", line 1, in <module>
from Tkinter import *
ImportError: No module named Tkinter
I've searched for my error and have found nothing. I've tried just typing import Tkinter. I'm using python 2.7, but just in case I have also tried using a lower case t.
Additional:
I'm using cygwin to launch my .py files as I'm new to programming, and that was in the tutorial to setting up an environment to work in that I used. I suspect this is part if not the error as I can find nothing online fixing my problem.
Edit: I fixed it. When installing cygwin I had to enable something to get it to work. I searched tkinter and enabled it from the thing that came up.

"The Python Windows Installers include Tcl/Tk as well as Tkinter. These are essentially a one-click install of everything needed." http://tkinter.unpythonic.net/wiki/How_to_install_Tkinter
I would check that you are actually running the correct version of Python.
Perhaps try explicitly stating you would like to run 2.7 with
$python2.7
>>> import Tkinter
As
import Tkinter
will return an ImportError in python 3.5. But will execute correctly in 2.7.
Then if the problem has not been solved, just reinstall python 2 with the windows installer. Tk and Tkinter are packaged with the python installer.

Related

Python module, pyttsx3 not recognised in VS code or pycharm

I am trying to make a bot that responds to certain question asked like ‘what is the time’ . I am trying to use pyttsx3 module to make it work but this error is coming:
Traceback (most recent call last):
File "/Users/HarAd MAC/Desktop/Projects/Python_Work/bot.py", line 1, in <module>
import pyttsx
ImportError: No module named pyttsx3
I have download the module and checked it in VS code Terminal. It is also showing Requirement already satisfied. Can someone help me in this problem. Same error was shown when I was trying to run it in pycharm. These type of problems have also occurred when using modules that have to be download manually.
I am using MacBook Air big Sur and python 3.9.
Thanks.
I don't know for sure but I believe when your import the library you have to do import pyttsx3 while what you were doing is import pyttsx. Here's some more info on usage and how to import

ImportError: No module named stdio, any way to start fresh with python?

I'm getting the error
Traceback (most recent call last):
File "ghs.py", line 1, in <module>
import stdio
ImportError: No module named stdio
When I try to run my script. I can run my script on other machines just fine. I have installed python using homebrew. And I've tried everything I can think of to get it to recognize my modules! I've uninstalled and reinstalled using brew. I've tried changing the path (though I don't fully understand this). I get no issues using brew doctor.
I've also tried using a python virtual environment but to no avail.
Any ideas on how to fix this issue or else 'start fresh' from a fresh version of python?
When you import a module, Python looks for it at the directory your code is, and the directory in which the built-in libraries are (C:\Users\pc\AppData\Local\Programs\Python\Python35-32\Lib in my case, I'm using Windows 10 and Python 3.5). If it can't find it, it raises ImportError.
I couldn't find a module named stdio in my computer. I also know some C++ and as far as I know, stdio is the library for inputs and outputs(prints). In python, there is no need to import such a library.
You can use try,except statement to test if your code works without importing the module like this.
try:
import stdio
except:
#rest of your code goes here
You will need to indent your whole code however this can be done easily with a text editor in which you can edit more than one line at a time.

`import pygame` shows no errors even when pygame is not installed

I've been installing, uninstalling, and reinstalling pygame because there's a program that I've been trying to run (that uses pygame). However whenever I've installed pygame, the program doesn't work.
Traceback (most recent call last):
File "C:\Users\Ryan\Desktop\en\snake.py", line 28, in <module>
class Segment(pygame.sprite.Sprite):
AttributeError: 'module' object has no attribute 'sprite'
(Pygame is not installed when I receive this error supposedly)
What's really confusing me though is that when I type import pygame in normally in IDLE, I get no errors even when I just uninstalled it.
Windows 8 64 bit
Python 3.4.3
Pygame-1.9.2a0.win32-py3.4.msi (pygame file name)
Python is installed on C Drive (C:\Python34)
Whenever Pygame was installed, it was on the D drive
Have Python directory added to Path
Here's a screenshot of a video tutorial I was following, and it shows there are two installation paths. And here's mine, which only shows one path. I'm not sure if this is a big deal, but I just want to be sure.
Open your python interpreter and import pygame
Then you can print pygame to see exactly what is being loaded, should be something like: <module 'pygame' from '/Library/Python/2.7/site-packages/pygame/__init__.py'>
(repeat the above step until it fails importing pygame),
Close your prompt and install pygame. Try executing it again. Should the problem persist, you can edit some file in the game you are trying to run and add two lines: import sys and print sys.path and check the list of paths to make sure that there is not some other pygame installation somewhere bundled with the game that is being used instead of your installed pygame.
Modification of Josep Valls's answer:
You will need to change sys.path. This can be accomplished with the following code:
import sys
sys.path.append('''directory where pygame is''')
import pygame
The sys.path variable tell Python where to look for modules. By adding a path by append, you are telling Python to look for imported modules there. After that pointer is made, you can import pygame.

Error importing Zelle's graphics.py package

I am working though Zelle's Python book (Python 2.7.5, Canopy Express, Windows 7). For Chapter 5 you need to install his graphics package, graphics.py. I downloaded the current version, put it in the C:\Python27\Lib\site-packages folder and made sure the file has the '.py' extension, but when I type >>import graphics I get the following error in both Canopy and IDLE:
ImportError
Traceback (most recent call last)
<ipython-input-1-e6fd1288a7fc> in <module> ()
----> 1 import graphics
ImportError: No module named graphics
To the best of my knowledge I've followed the instructions and hints on the website and here in StackOverflow, but I'm flummoxed. Any suggestions? I like the Zelle book but I'm not sure how much use it will be from this point on without the graphics package - from just skimming it seems like the majority of the examples and exercises use it. Thanks.
It took a while, but I got it working. Enthought Canopy uses its own (virtual) environments, so you need to put Zelle's graphics package there, not in the system library. I had success by placing graphics in Enthought's top level directory, which you can determine in the interpreter:
>> import site
>> site.getpackages()[0]
I copied the graphics package there, without the .py extension, fired up Canopy and I was off and running. Thanks to everyone who has posted, in various places, tips on importing - eventually it made sense! By the way,
>> site.getpackages()
will give you all site package directories, but I only needed the first.

Think Python - Can't get tkinter to work

I am all new to everything about coding and programming, and I'm right now reading the book Think Python. Now I have to make tkinter work, so I can import the module TurtleWorld. First I try to import tkinter just to see that it work, and it does. Then when I'm trying to import TurtleWorld, suddenly there is no module called tkinter? I have no idea what to do, feels like I have been doing everything I could find! I'm using python 3.4.1 and I haven't downloading tcl or swampy cause they already came with python.
>>> import tkinter
>>> import swampy.TurtleWorld
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import swampy.TurtleWorld
File "C:\Python34\lib\site-packages\swampy\TurtleWorld.py", line 8, in <module>
from Tkinter import TOP, BOTTOM, LEFT, RIGHT, END, LAST, NONE, SUNKEN
ImportError: No module named 'Tkinter'
You are using Python 3.4.1 and according to the Swampy: Installation Instructions you need to have Python 2 installed to use the Tkinter module. Python 2 is the version of Python used in Think Python, and you will not be able to use Swampy with Python 3. The current version of Python 2 is in the Python 2.7 series.
To see if you have the Tkinter module, at the Python prompt, type:
import Tkinter
Note that the word Tkinter is case sensitive and the tkinter module (not the Tkinter module) should be used with Python 3.

Categories