Python module dependencies - python

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.)

Related

difference between "import foo.bar" and "import foo"?

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>

How to enable autocomplete (IntelliSense) for python package modules?

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.

PyCharm autocomplete does not work with pygame

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.

How can I hook a function in a python module?

So I have a package in my virtual environment installed in the site-packages folder of it.
In that package there is a module with a bunch of functions, I want to change one of these functions without touching the package code, so I want to make a hook of that module and then change/overwrite the function so it behaves in the way I want.
I don't know if this is even posible in python (I'm new to the language)
Example:
I have a package called their_package in my site-packages folder, inside I have a module named whatever.py with the following code:
import sys
import os
def this_is_a_function(parameter):
//whatever logic in here
return result
What I want is to hook the whatever.py module so I can redefine the this_is_a_function to have a diferent logic. Is this posible? I would appreciate a lot a code sample!
Thank you in advance!:)
You can redefine your function with:
import whatever
def this_is_a_function(parameter):
pass
whatever.this_is_a_function = this_is_a_function

Create documentation using pydoc and unknown modules

I'm afraid this will a question for a very particular case. At university we have been told to generate documentation by using pydoc. The problem is that we need to create it for a Maya script, and pydoc yells when it finds import maya.cmds as cmds
So, I tried to comment this line but I keep getting errors:
python C:\Python27\Lib\pydoc.py Script.py
problem in Script - <type 'exceptions.NameError'>: global name 'cmds' is not defined
I also tried Script, without the extension .py but it's silly doing that, we still running around the same issue.
Does anybody know how to generate documentation for Maya scripts, where import maya only works in the maya interpreter?
maya.commands is an stub module until it's run inside a working maya environment; if you just import it and inspect it outside of Maya you'll see that it's basically a placeholder.
If you want to inspect the contents, you can import the maya.standalone module and initialize it before running other commands (in this case it means you won't be able to run pydoc standalone.
You can get the documentation using the write command:
import maya.standalone
maya.standalone.initialize()
import pydoc
import mymodule
pydoc.write(mymodule) # writes the mymodule.html to current directory
Be warned, however, that the documentation for all maya built in functions will be unhelful:
'built-in function ls'
however you can at least document your own stuff without the maya parts crashing.
Pydoc, ironically, does not have a lot of external documentation. However you can see the code here:http://hg.python.org/cpython/file/2.7/Lib/pydoc.py (i'm not sure about the delta between this and the 2.6 version for maya pre-2014 but it works as above in maya 2011)

Categories

Resources