I already installed the latest version of Python 3 and I installed Pygame using the terminal I also included import Pygame in my file however when I run the file it refuses to open and displays the error message "no module named pygame"
What's the problem?
What's the solution?
Try python3 first.py in your terminal.
Or in your file first.py add this line at the top:
(I used python3.x but you should add the correct version of your python 3)
#!/usr/local/bin/python3.x
To see your version:
$ python3 -V
Python 3.5.2
To see your python 3 path:
$ which python3
Related
After typing SnakeGame.py I am getting the error:
Traceback (most recent call last):
File "SnakeGame.py", line 1, in
import pygame
ImportError: No module named pygame).
Pls give me a solution on how to solve this, I have installed pygame also.
thanks in advance.
That error means that the python interpreter isn't able to find the pygame module.
Are you using a virtual environment? If you are using an IDE like PyCharm, try to install the pygame module inside the IDE console.
pip install pygame
install pygame in CMD write pip install pygame
if you use visual studio code go to market and type pygame then install it ...
enter image description here
visual studio code
I had this same problem a few days ago. My solution was to use a virtual environment. To do this, open a terminal in your folder and use the following commands: 1.) pip install pipenv 2.) pipenv shell 3.) pipenv install pygame
After this, you need to select the correct python interpreter. In VS Code, hit ctrl+shift+p Then click the correct interpreter (should have the name of the folder). Then try to run the file. If that doesn't work, try to run this in the terminal: pipenv run {name of the python file, example: app.py}
Hope that works for you
I am an absolute newbie. I'm trying to make Python GUI for my school project so I decided to use Tkinter. When I try to import Tkinter it throws this message:
>>> import tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 36, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
I tried to find a solution online but I couldn't figure it out (mostly didn't understand it).
I read about some problem with directory in setup.py but I don't understand how to fix it. I have tkinter folder in my python3.7 folder.
I don't really understand these steps that I found:
If it fails with "No module named _tkinter", your Python configuration needs to be modified to include this module (which is an extension module implemented in C). Do not edit Modules/Setup (it is out of date). You may have to install Tcl and Tk (when using RPM, install the -devel RPMs as well) and/or edit the setup.py script to point to the right locations where Tcl/Tk is installed. If you install Tcl/Tk in the default locations, simply rerunning "make" should build the _tkinter extension.
I'm using Mac OS and use Visual Studio Code.
To check your python version, run this on terminal:
$ python --version
Check what python your machine is using. Run:
$ which python
If it's using python from Homebrew, it's probably using Python 2. If you installed python 3 manually, just install tKinter manually.
$ brew install python-tk
To run python 2 manually, run on terminal:
$ python <FILENAME>
If python 3, run:
$ python3 <FILENAME>
https://stackoverflow.com/a/9883299/4678635 will help you.
In summary, you have to reinstall python for your computer bit system.
And below code strategy is also helpful to you.
try:
from Tkinter import * # for Python2
except ImportError:
from tkinter import * # for Python3
I have uninstalled python completed from my windows 10 and even deleted all its path from environment variables but it is runnning python -V and showing Python 2.7.14 which was never there.
I want to remove this 2.7.14 but it is not present anywhere on my system.
When i run python in command line it gives ImportError: No module named site which is OK(since i have uninstalled python).
But after running python --version it shows Python 2.7.14 which was never there in the first place. the only python i had was python 3.7.6 which i have installed.
python on cmd gives
ImportError: No module named site
while
python -V gives
Python 2.7.14
Open a command prompt and type where python.exe:
C:\Users\partner>where notepad.exe
C:\Windows\System32\notepad.exe
C:\Windows\notepad.exe
If it is in your path this should find it for you.
When I write a simple code in PyCharm:
import setuptools
a = 10 ## This line is of no use
I don't get any error while executing it. But if I try to import setuptools from the terminal I get an error that there is no module named setuptools.
What I write at the terminal is:
python3 -c "import setuptools"
I have setuptools installed for python 2.7 and I have tried the same for 2.7 version also and it runs at both the places i.e. terminal and PyCharm.
Looks like the environment setup by pycharm doesn't match the one you have in your terminal.
Try to display sys.path in your script executing in pycharm and adjust your PYTHONPATH env var to be inline with it.
I have an existing Python 2.4 and it is working properly with tkinter as I tested it using
python
import _tkinter
import Tkinter
Tkinter._test()
Now, I have installed python 2.5.2 but when I try the same tests (with the newer version), it returns (but the same tests are working for the previous version)
ImportError: No module named _tkinter
I know that tcl8.5 and tk8.5 are installed on my machine as the following commands return there locations
whereis tcl
tcl: /usr/lib/tcl8.4 /usr/local/lib/tcl8.5 /usr/local/lib/tcl8.4 /usr/share/tcl8.4
whereis tk
tk: /usr/lib/tk8.4 /usr/local/lib/tk8.5 /usr/share/tk8.4
Any ideas how do I make my newer python version work with tkinter?
The files you found are for linking directly to tcl/tk. Python depends on another library as well: _tkinter.so. It should be in /usr/lib/python2.5/lib-dynload/_tkinter.so.
How did you install python2.5? If you are using Debian or Ubuntu you need to install the python-tk package to get Tkinter support.
If the _tkinter.so file is there, your environment could be causing problems.
If
python -E -c "import
Tkinter;Tkinter._test()"
suceeds, but
python -c "import
Tkinter;Tkinter._test()"
fails, then the problem is with how your environment is set up. Check the value of PYTHONPATH is set correctly.