python-module

How do I document a module in Python?

That's it. If you want to document a function or a class, you put a string just after the definition. For instance: def foo(): """This function does nothing.""" pass But what about a module? How can I document what a file.py does? ...

External classes in Python

I'm just beginning Python, and I'd like to use an external RSS class. Where do I put that class and how do I import it? I'd like to eventually be able to share python programs. ...

How to get filename of the __main__ module in Python?

Suppose I have two modules: a.py: import b print __name__, __file__ b.py: print __name__, __file__ I run the "a.py" file. This prints: b C:\path\to\code\b.py __main__ C:\path\to\code\a.py Question: how do I obtain the path to the __main__ module (a.py in this case) from within the "b.py" library? ...

Is there a good python module that does HTML encoding/escaping in C?

There is cgi.escape but that appears to be implemented in pure python. It seems like most frameworks like Django also just run some regular expressions. This is something we do a lot, so it would be good to have it be as fast as possible. Maybe C implementations wouldn't be much faster than a series of regexes for this? ...

Is there a fini routine for a python module written in C?

I have a python module written in C, and I would like to add a function that is called when the module is unloaded. I obviously have an initfoo function to initialize the module -- is there a way to tell python to call a finifoo function when it's uninitializing the module? Is atexit my only option? ...

Test for Python module dependencies being installed

How could one test whether a set of modules is installed, given the names of the modules. E.g. modules = set(["sys", "os", "jinja"]) for module in modules: # if test(module exists): # do something While it's possible to write out the tests as: try: import sys except ImportError: print "No sys!" This is a bit cumbersome f...

What do I need to read Microsoft Access databases using Python?

How can I access Microsoft Access databases in Python? With SQL? I'd prefere a solution that works with Linux, but I could also settle for Windows. I only require read access. ...

Efficiently importing modules in Django views

Hi all, I was wondering - how do people handle importing large numbers of commonly used modules within django views? And whats the best method to do this efficiently? For instance, I've got some views like, admin_views.py search_views.py . . and from what I've seen, every one of them needs to use HttpResponse or other such commonly ...

Check for a module in Python without using exceptions

I can check for a module in Python doing something like: try: import some_module except ImportError: print "No some_module!" But I don't want to use try/except. Is there a way to accomplish this? (it should work on Python 2.5.x.) Note: The reason for no using try/except is arbitrary, it is just because I want to know if there is ...

Error importing a python module in Django

In my Django project, the following line throws an ImportError: "No module named elementtree". from elementtree import ElementTree However, the module is installed (ie, I can run an interactive python shell, and type that exact line without any ImportError), and the directory containing the module is on the PYTHONPATH. But when I a...

Accessing py2exe program over network in Windows 98 throws ImportErrors

I'm running a py2exe-compiled python program from one server machine on a number of client machines (mapped to a network drive on every machine, say W:). For Windows XP and later machines, have so far had zero problems with Python picking up W:\python23.dll (yes, I'm using Python 2.3.5 for W98 compatibility and all that). It will then ...

Python: How do I disallow imports of a class from a module?

I tried: __all__ = ['SpamPublicClass'] But, of course that's just for: from spammodule import * Is there a way to block importing of a class. I'm worried about confusion on the API level of my code that somebody will write: from spammodule import SimilarSpamClass and it'll cause debugging mayhem. ...

python more trouble importing modules

I asked a similar question yesterday, but have acquired a really odd problem since then. With this directory structure: app/ models/ __init__.py user.py other.py pages/ __init__.py pages.py The models/__init__.py file has this line: __all__ = ['user', 'other'] and the pages/__init__....

How can I figure out in my module if the main program uses a specific variable?

I know this does not sound Pythonic, but bear with me for a second. I am writing a module that depends on some external closed-source module. That module needs to get instantiated to be used (using module.create()). My module attempts to figure out if my user already loaded that module (easy to do), but then needs to figure out if the ...

Installing Python modules on windows

I'm trying to install Swish-E,but unfortunately a windows installer is not present so I'm trying to install it via the console (under Windows 7) but i keep on getting the same error, and i get this error on every module i try to install C:\Users\Mg\Downloads\pysqlite-2.5.6\pysqlite-2.5.6>python setup.py install running install running ...

python metaprogramming

I'm trying to archive a task which turns out to be a bit complicated since I'm not very good at Python metaprogramming. I want to have a module locations with function get_location(name), which returns a class defined in a folder locations/ in the file with the name passed to function. Name of a class is something like NameLocation. So...

Pylibmc: ImportError: dynamic module does not define init function (init_pylibmc)

>>> import pylibmc Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pylibmc.py", line 55, in <module> import _pylibmc ImportError: dynamic module does not define init function (init_pylibmc) Trying to import pylibmc, which...

Checking for module availability programmatically in Python?

given a list of module names (e.g. mymods = ['numpy', 'scipy', ...]) how can I check if the modules are available? I tried the following but it's incorrect: for module_name in mymods: try: import module_name except ImportError: print "Module %s not found." %(module_name) thanks. ...

Easiest way to automatically download required modules in Python?

I would like to release a python module I wrote which depends on several packages. What's the easiest way to make it so these packages are programmatically downloaded just in case they are not available on the system that's being run? Most of these modules should be available by easy_install or pip or something like that. I simply want ...

Why does Python Array Module Process Strings and Lists Differently?

I'm having trouble understanding the result of the following statements: >>> from array import array >>> array('L',[0xff,0xff,0xff,0xff]) array('L', [255L, 255L, 255L, 255L]) >>> from array import array >>> array('L','\xff\xff\xff\xff') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: string length ...