I am using Linux Mint 17 (Ubuntu 14.04) and already got pygame 1.9.2 working in Eclipse on my Windows PC. Now I also installed it on this machine, but it doesn't work. Thus there is no apt-get for python3-pygame I downloaded the source from https://bitbucket.org/pygame/pygame and built and installed it without getting errors.
When running the following on the command line I receive no error:
import pygame
pygame.init()
If I try the same in an Eclipse PyDev project I get the error: "Undefined variable from import: init".
First I thought that my Interpreter was not set up correctly, but the path where pygame is installed is added to the libraries (/usr/local/lib/python3.4/dist-packages). I realized that for Python2.7 pygame is installed in /usr/lib/pytho2.7/dist-packages instead, but this directory doesn't exist for python3.4 on my machine.
Since it seems to work on the command-line it must have something to do with the Eclipse or Pydev settings, right?
UPDATE:
Ok, now things are getting really confusing. I found out that pygame indeed is also working from within eclipse if I run the project, but only giving me these error messages for eg. pygame.init(), pygame.QUIT, pygame.K_ESCAPE, pygame.KEYDOWN. I find it very strange, because pygame.time.Clock() or pygame.display.set_caption() don't give error messages. So I only get undefined variable from import errors (also see here How do I fix PyDev "Undefined variable from import" errors? ).
Use sudo pip3 install pygame, pip3 should work assuming you have python3 and setuptools installed.
You can install pip3 using sudo apt-get install python3-pip and then use sudo pip3 install package_name to get whatever you want.
The only thing which worked for me (I spent two days now finding out what the problem is) is to add "pygame" to the forced builtins for the Interpreter (Window -> Preferences -> PyDev -> Interpreter -> Python Interpreter -> Forced Builtins Tab -> New -> pygame.
This is only a workaround, but at least I am rid of those errors for now and auto-completion still works. I didn't find any other solution that worked for me so far.
For me the problem was resolved by importing *.
instead of:
import pygame
try this:
from pygame import *
now instead of calling init() by saying pygame.init() just use init()
also you should not have to explicitly call using pygame. for the most part.
NOTE: after playing around with this in Eclipse I did end up having to use both:
import pygame
from pygame import *
for whatever reason one of my functions only works if I have pygame.even.get()
but the test of my code does not need to use pygame.
(I am sure there is a perfectly good reason but I am still very new to python)
Related
I currently have Python 3.10.5 and pygame 2.1.2.
VScode 1.70.2
Versions
For some reason there is the following warning: Image for warning
Import "pygame" could not be resolved PylancereportMissingImports
Even though I have the pygame library installed using "pip install pygame".
And when I run the file by pressing the "Run Python File" in the right top corner, it runs perfectly without an error. It even uses the terminal to show whatever I am printing.
My issue here is the warning and that intellisense for pygame isn't working.
What can I do to fix that.
I already tried to make a clean install on everything, python, pygame, even VScode. Nothing.
Try to reload vscode (by command pallete or just restart the app), reinstall pygame and make sure python path is selected. Also it is worth to trying selecting interpreter. Open command pallete and type: Python: Select Interpreter, then select your python version.
yes as the #NoBlockhit stated probably you have 2 different python versions installed and vs code choses the one without the pygame library (https://code.visualstudio.com/docs/python/environments) if you scrolldown it shows you how to chose an in interpreter...
I'm new to programming and python and, wanted to try pygame but when I put import pygame, I get:
Unused import statement 'import pygame'
I'm using python 3.10.6, what pygame should I install?
I am not sure where that error message is coming from. Probably your IDE, because Python itself won't complain about unused imports.
It sounds like you have typed import pygame which makes the pygame module available for you to call (once you install it), but haven't typed any code that uses pygame.
For example, I would expect your code to include pygame.init() somewhere, because all pygame clients need to do that to prepare it.
Once your code actually uses pygame, and doesn't just import it, the error should go away.
If you want to use any Python package in the code you need to install it.
To install any package, use the following command:
pip install <package name> # for eg: pygame
After that, you can use it anywhere in the program.
Also, please be sure that you are in a proper virtual environment or in a global environment.
To create a new virtual environment, follow the below solution.
Create Virtual Environment The system cannot find the path specified
the problem:
as part of my studies, i have tried to download PyGame for my own independent project. I've currently downloaded 1.9.6, as well as currently owning IDLE version 3.8. i have imported it many times:
folder.
on one occasion , i placed it in its own separate folder with the coding file in another internal folder
when importing it into a new python script, the result was that there was "no module named Pygame", even though it was in a file containing the recent edition (this method was used successfully to download Tkinter).
I also tried making a python script outside the contained folder like this
this,too had the same result.
Any idea what exactly to do?
any more important information
The computer type is a MAC 10.13.6 and the IDE is IDLE. As previously mentioned, the downloaded version is version 1.9.6 and i have not a clue what how many bits my computer manages. Apologies that this is probably one of MILLIONS of the exact same question answered.I've tried at least 2 of these solutions.
First you need to make sure that you have pip installed. Type pip in your cmd (command prompt) and if you get no errors you are fine. If you have errors follow this link to get it. If you are stuck on that link - basically all you need to do is uninstall python and then when you are installing it again tick the box that says:
Add Python 3.5 to PATH
Now just finish installing python.
Next you need to install pygame with pip install pygame in your cmd - run it as administrator just to be safe. If you receive errors try updating pip with python -m pip install --upgrade pip. Now create and save a new python file. Import pygame in that file by using the code import pygame. The output should be:
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Now you are free to code in pygame! I hope this helped.
Basically, i'm trying to use the turtle module at home as I have been using it in school and it's quite fun to use. However, when I try to import it and run the code, it says that there is an error in line 1 of the code and says that no module named pygame was found. I didn't write pygame, I specifically wrote
from turtle import *
which at school works perfectly fine. Both my computer and the school computers use Python 3.6 but the turtle module will just not work at home. Any ideas why this might be happening? If it helps, I've tried to install pygame in the past but it wouldn't work so I stopped trying to install it, and just now I tried deleting and uninstalling pygame but it still shows up with the error. Thanks for taking your time to read my question.
The error that comes up is as follows:
Traceback (most recent call last):
File "C:/Users/homeuser/AppData/Local/Programs/Python/Python36-32/jsjs.py", line 1, in <module>
from turtle import *
File "C:/Users/homeuser/AppData/Local/Programs/Python/Python36-32\turtle.py", line 1, in <module>
import pygame
ModuleNotFoundError: No module named 'pygame'
>>>
Also, pip install pygame comes up with an error saying invalid syntax
First, you need to download the version of pygame suitable for your version of python. Here's a link to their download page: https://pypi.python.org/pypi/Pygame/1.9.3
Next, open the command prompt. This technique works provided that you have pip installed and you have the required environmental variables.
Now, type in the following into the command prompt: 'python -m pip install "Path/To/Downloaded/File.py"'
Here's a real-life example: 'python -m pip install "C:/Users/danie/Downloads/pygame-1.9.3-cp36-cp36m-win_amd64.whl"'
If you have any errors, it would most likely because:
- (A) You don't have pip installed properly.
- (B) You gave an invalid directory branch.
- (C) You spelled something in the command wrong.
- (D) You forgot to download the Pygame module.
I hope this helps.
Pygame should be a dependency of turtle. Try to download pygame with pip.
More information about the installation:
https://www.pygame.org/wiki/GettingStarted
You've been working inside a Python standard library directory, placing your own files there and clobbering Python's insides. The error you're seeing is because you've edited or replaced the turtle.py from the standard library. At this point, there's no telling how much damage has occurred, and manually fixing things isn't going to be practical.
Uninstall and reinstall Python to restore things to normal, and stop putting your own work in that directory.
Seeing the error code you've posted, it seems that some files placed in the python directory have been messed with or there must have been some changes. Nevertheless, You can try doing an uninstall and then doing a clean install of python.
Then proceed to download pygame by:
pip install pygame
and then turtle module. You may have to look up for the Tkinter module too.
My current version of Python is the newest 3.5 and the only available PyGame was for 3.2 (both PyGame and Python are 32-bit). I've scoured stackoverflow for a resolution and can't find any way to make this work. I've installed the PyGame easy installer and placed it in the directory where my Python install is, and in Visual Studio (I've also tried this in PyCharm as well as the standard Python IDE in command prompt), upon typing
import pygame or import sys, pygame
I'm presented with the error Import Error was
unhandled by user code - DLL load failed: The specified module could not be found.
My final solution is to uninstall Python 3.5 and install the version which matches PyGame.
This shouldn't have anything to do with Python version. You are missing a dynamic library (the DLL). This means that either the DLL is not on your system or Python can't find it. You should probably try the msi/exe installer for PyGame mentioned here, as it should install any dependencies. This would fix the issue if it is caused by the DLL being absent from your system.
It could also be caused by the libraries not being on PYTHONPATH. Search the error you received and you should see quite a few answers on fixing this.
If you're just getting started, you may want to look at a different library. There are likely quite a few game libraries for Python that can be installed with a simple pip install. You could then return to PyGame when you are more comfortable if you wanted.
get it from here: https://www.dropbox.com/s/hnmcaq1rf6zn7m3/pygame-1.9.2a0-cp34-none-win32.whl?dl=0
download it and install it by putting it in C:\python3.5\Scripts
then run pip3 install pygame-1.9.2a0-cp34-none-win32.whl
(this is for 3.4, but 3.5 works too)
it should work, and your error has nothing to do with the package, it's just that you do not have a DLL file, install it again from the website above