This question is not about Pygame, I'm usin Pygame as an example.
While experimenting with Pygame I've noticed that autocomplete is not working for some modules. For example, if I start typing pygame.mixer autocomplete shows MissingModule. While searching for a solution I've found a lot of similar questions for various text editors and modules that have parts written in C. I am using Visual Studio Code, python path is set correctly and my code runs fine.
One strange workaround is modifying Pygame's __init__.py
What's the right way to enable autocomplete?
I've found a solution, but I would appreciate some explanation from someone more experienced with Python:
import package.module as module
With Pygame mixer it's:
import pygame.mixer as mixer
But I still don't understand why autocomplete for import.package.module doesn't work, but import.package.module as module does.
Probably pygame.mixer doesn't work with import pygame because there is no attribute mixer inside pygame package. If attribute is not there autcomplete won't list it.
When you import package, Python doesn't recursively import subpackages and modules unless it's explicitly assigned inside the "__init__.py" file inside a package.
This is why import pygame.mixer as mixer works because you import pygame package and mixer module(?) which is available through local name mixer. However with such import you don't have pygame available in local scope.
Similar situation is when you just import pygame.mixer. pygame is available but mixer has to be referenced by pygame.mixer.
In both cases pygame package and pygame.mixer module(?) are executed.
You could also use from pygame import mixer instead import pygame.mixer as mixer or from pygame import mixer as module if you want to rename.
Try:
from pygame import *
Now you can see nested autocompletion options.
Related
I have been recently attempting to try out some minor game development with Python 3.9 and have been running into some problems. For some reason, Python does not recognize that I have tkinter installed.
From what I have seen online, a big fix is to make sure the path variables are set up correctly. I have already added it to my env variables but that has not seemed to fix anything.
I also know I have it installed, as I see it here in my Python site-packages folder.
At this point, I am stumped. I do have the import as tkinter as well and not Tkinter.
The only thing I can think of now is totally uninstalling Python but I would prefer not to do that if there are any other options.
Here is the code:
import math
import random
import pygame
import tkinter as tk
from tkinter import messagebox
class cube(object):
rows = 20
w = 500
I am using Windows 10 and VSCode as well.
Anything helps and thanks!
Seems I somehow screwed up my Python installation. Completely removing it from my system and reinstalling seemed to fix all of my issues.
I just found that when I do
import pygame.joystick
I not only have access to joystick, but also to display, i.e. I can for example do
pygame.display.init()
just as if I had simply imported pygame.
What is the difference?
What's happening is that importing pygame.joystick triggers additional imports; either the pygame package itself, or pygame.joystick, or any of the pygame.* modules these two modules import, happen to import pygame.display somewhere.
So the fact that you can now reference pygame.display is an accident of implementation details. You may not be able to in future versions (if the project no longer needs to import pygame.display to load pygame.joystick, for example).
It is better to stick to an explicit import in your own project.
On import, module's inner code on the top level is executed, and the module is added to sys.modules and made available for use. Based on what was inside the source file, anything can and may happen.
In your case, either of pygame/__init__.py or pygame/joystick.py contains:
import pygame.display
Hence the availability of the module you weren't even trying to import.
In the source code of joystick, they're importing pygame.display or pygame and the sort.
In the C version of the documentation:
In the file joystick.c they've included joystick.h
#include <joystick.h>
I'm running python 3.7.1 on my MacBook and it has been running perfectly until I tried importing pygame. I'm taking an intro to python class and we always just do from module import *. When I tried to do that same format with pygame I just got an error ModuleNotFoundError: No module named 'pygame'
What should I do about using pygame or should I just forget using pygame altogether
I'm new to python and the reason I'm trying to use pygame is that I'm making a game where the user controls a .gif or a spaceship and tries to avoid asteroids. All of my code is solid accept for the collision detection. Everyone seems to point me towards using pygame but i can't get that to work. Should I continue to use pygame and if so, how do i fix my problem? Should I drop pygame altogether and just try collision detection a different way; (if so, what other way?)
pygame is not bundled in standard python.
to install pygame in Mac, see
Mac installation from pygame.org
Also, from module import * is not a good practice if you're using more modules,
which will causes class name conflicts.
Easiest way to do it - forget 3.7.1 go on the Python website (linked) and download 3.6.7 ther when your setting up click add to PATH then you need to install pygame ( save it locally near where python is saved) then go on to terminal by going to finder and clicking applications and search terminal and type pip install pygame if there is anything you are confused about just ask (:
Path button
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.
I'm trying to make my own module for easy sprite rendering & creation for personal uses. The only problem is, it needs pygame. If I were to put import pygame at the top of my module, could I then in another program, setup pygame instead of setting it up in the module? In general, does importing modules in one program, then importing that program into your main module, does the main program inherit the same dependencies, or do you need to explicitly re-import them?
Module to be used:
import pygame
def makeSprite():
# todo write code INCLUDING PYGAME DEPENDENCIES
pass
def updateSprite():
# todo write code INCLUDING PYGAME DEPENDENCIES
pass
Program using module:
import myModule # myModule is the name of the module above
pygame.init()
makeSprite(arg1, arg2)
updateSprite(arg1, arg2)
pygame.functionCallFromPygame()
Can the main program also use the module? Thank you.
That shouldn't be a problem. As long as nothing tries to actually use pygame functionality before pygame.init() is called, it'll work fine.
(In other words, as long as whatever program using your library calls pygame.init() before calling your library's functions, you'll be fine.)