python-import

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

Properly Importing Modules in Python

How do I set up module imports so that each module can access the objects of all the others? I have a medium size Python application with modules files in various subdirectories. I have created modules that append these subdirectories to sys.path and imports a group of modules, using import thisModule as tm. Module objects are referre...

python importing relative modules

hi, I have the Python modules a.py and b.py in the same directory. How can I reliably import b.py from a.py, given that a.py may have been imported from another directory or executed directly? This module will be distributed so I can't hardcode a single path. I've been playing around with __file__, sys.path and os.chdir, but it feels m...

Django startup importing causes reverse to happen

This might be an isolated problem, but figured I'd ask in case someone has thoughts on a graceful approach to address it. Here's the setup: -------- views.py -------- from django.http import HttpResponse import shortcuts def mood_dispatcher(request): mood = magic_function_to_guess_my_mood(request) return HttpResponse('Please go to...

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

Prevent Python from caching the imported modules

While developing a largeish project (split in several files and folders) in Python with IPython, I run into the trouble of cached imported modules. The problem is that instructions import module only reads the module once, even if that module has changed! So each time I change something in my package, I have to quit and restart IPython....

Python - How to PYTHONPATH with a complex directory structure?

Consider the following file\directory structure: project\ | django_project\ | | __init__.py | | django_app1\ | | | __init__.py | | | utils\ | | | | __init__.py | | | | bar1.py | | | | ... | | | ... | | django_app2\ | | | __init__.py | | | bar2.py | | | ... | | ... | scripts\ | | __init__.py | |...

Python access parent object instances

I'm currently trying to write a multiple-file Python (2.6.5) game using PyGame. The problem is that one of the files, "pyconsole.py", needs to be able to call methods on instances of other objects imported by the primary file, "main.py". The problem is that I have a list in the main file to hold instances of all of the game objects (pl...

Import fails with a strange error

I get: TemplateSyntaxError at /blog/post/test Caught NameError while rendering: global name 'forms' is not defined for this code: forms.py from dojango.forms import widgets from django.contrib.comments.forms import CommentForm from Website.Comments.models import PageComment class PageCommentForm(CommentForm): title = widg...

How can I get 'urlpatterns = __import__(<string_name>)' to work like a normal import statement?

I'm trying to create an import statement that's pluggable with other projects. This statement is located in urls.py So this works: from forum.urls import urlpatterns # Base Class: <type 'list'> But this doesn't work: from settings import ROOT_URLCONF as project_urls urlpatterns = __import__(project_urls) # Base Class: <type ...

How to properly use relative or absolute imports in Python modules?

Usage of relative imports in Python has one drawback, you will not be able to run the modules as standalones anymore because you will get an exception: ValueError: Attempted relative import in non-package # /test.py: just a sample file importing foo module import foo ... # /foo/foo.py: from . import bar ... if __name__ == "__main__": ...

ImportError: No module named ***** in python

I am very new to python, about one month, and am trying to figure out how the importing works in python. I was told that I can import any 'module' that has Python code in it. So I am trying to import a module just to try it out, but I keep getting an 'ImportError: No module named redue'. This is an example of the python shell: >>> impor...

What's wrong with importing a python module without the full package name?

At http://google-styleguide.googlecode.com/svn/trunk/pyguide.html#Imports, it suggests: Even if the module is in the same package, do not directly import the module without the full package name. This might cause the package to be imported twice (with unintended side effects) when the "main" module that is used to start an applicatio...

Is it possible for my Mercurial hook to call code from another file?

I have a hook function named precommit_bad_branch which imports hook_utils. When invoking precommit_bad_branch via a commit I get the following error message: error: precommit.branch_check hook raised an exception: No module named hook_utils abort: No module named hook_utils! It looks like I'm not allowed to call hook_utils from preco...