4

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, my folder structure:

program.py
locations/
    __init__.py
    first.py
    second.py

program.py will be smth with with:

from locations import get_location
location = get_location('first')

and the location is a class defined in first.py smth like this:

from locations import Location # base class for all locations, defined in __init__ (?)
class FirstLocation(Location):
    pass

etc.

Okay, I've tried a lot of import and getattribute statements but now I'm bored and surrender. How to archive such behaviour?


I don't know why, but this code

def get_location(name):
   module = __import__(__name__ + '.' + name)
   #return getattr(module, titlecase(name) + 'Location')
   return module

returns

>>> locations.get_location( 'first')
<module 'locations' from 'locations/__init__.py'>

the locations module! why?!

3
  • 5
    This idea is painful just to read. Are you really sure you want to go this route? Commented Mar 20, 2010 at 5:29
  • I need to be able to do something like in my program.py example. If there is another route, just let me know! :) Sorry for painful reading, my English is not very good. Commented Mar 20, 2010 at 5:31
  • It's not so much the English. It's the idea. PLease explain why you need this. Also what does "smth" mean? Please use backticks ` around Python variables with _ in them. Commented Mar 20, 2010 at 12:30

2 Answers 2

6

You do need to __import__ the module; after that, getting an attr from it is not hard.

import sys

def get_location(name):
    fullpath = 'locations.' + name
    package = __import__(fullpath)
    module = sys.modules[fullpath]
    return getattr(module, name.title() + 'Location')

Edit: __import__ returns the package, so you need one more getattr, see the docs (and read all the section carefully -- "do as I say not as I do";-).

Sign up to request clarification or add additional context in comments.

4 Comments

(where was no titlecase method so I've used someone's snippet) I got AttributeError: 'module' object has no attribute 'FirstLocation'
please look at my question, I've added additional (painful) info
brilliant!!! thanks. (again, str has no titlecase method, or i don't know about it)
It's just .title(), not .titlecase() -- editing to fix, sorry.
1

I think you're looking for:

location = __import__('first')

2 Comments

ImportError: No module named first, when I'm returning the location from get_location with your code
Well, adapt it to whatever the heck you're trying to do, but that's how you import a module using a string as the name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.