How to access a python module variable using a string [ django ]

I have a django application, with a module called "app"
Now this module has a file called "urls.py" which has a variable called "HOME_URL"

What I'm trying to do ?

app_label = "app" url = __import__(app_label).urls.HOME_URL print url

That obviously doesn't work, but I hope you got what I'm trying to do, If not please comment I will edit the question to contain more info.

-------------Problems Reply------------

You can use import_module to load a module relative to the root of a django project.

from django.utils.importlib import import_module
app_label = "app"
url = import_module("%s.urls" % app_label).HOME_URL

This should work within your django project, or in ./manage.py shell.

Category:python Views:0 Time:2011-08-19

Related post

  • How to access a Python global variable from C? 2008-11-12

    I have some global variables in a Python script. Some functions in that script call into C - is it possible to set one of those variables while in C and if so, how? I appreciate that this isn't a very nice design in the first place, but I need to mak

  • how do python module variables work? 2010-08-24

    I used to think that once a module was loaded, no re-importing would be done if other files imported that same module, or if it were imported in different ways. For example, I have mdir/__init__.py, which is empty, and mdir/mymod.py, which is: thenum

  • Unable to find the Python module execute_manager in django.core.management 2009-04-13

    Problem: to find the Python module execute_manager in the class django.core.management. The question is related to another question of mine. I am trying to start my server by python manage.py runserver The command is dependent on manage.py. The file

  • How to import python module with same filename that is already imported? 2011-02-08

    I have a problem with python module import. I installed django (this can be any other module). I use this module: from django.template import Context # other imports # use Context I have a folder in my application named "utilities". In this folder I

  • Help with accessing module variable inside Python function? 2011-08-10

    I have the following function in a Python module called dictbuilder.py: def my_dictbuilder(reader_o, writer): fieldnames = ('name', 'number') reader = csv.DictReader(reader_o, fieldnames = fieldnames, delimiter="\t") my_dict = {} id = 0 for row in re

  • Python: How to make a cross-module variable? 2008-09-26

    The __debug__ variable is handy in part because it affects every module. If I want to create another variable that works the same way, how would I do it? The variable (let's be original and call it 'foo') doesn't have to be truly global, in the sense

  • Python and Eclipse: How to use interactive console after running a module, with the module variables and functions 2010-04-14

    Let's consider 3 situations: 1) I write a pyhon module in Eclipse (pydev) and run it Ctrl-F11. The module runs and I don't have any control or access (AFAIK) to the module variables and functions. 2) I have defined a python interpreter as an external

  • Python module get top scope variables 2011-02-08

    Hey, in my python modules (written in python) I want to be able to access the variables from the top level scope, that is, the file that is calling the modules. How would I go about doing this? Thanks. --------------Solutions------------- In general,

  • Is there a Python module to access Advantage Database Server? 2009-07-27

    As the title suggest, I was wondering if there is a Python module that can access an Advantage Database Server (Sybase) files such as ADT and DBF. I have searched the web and couldn't find what I'm looking for this is why I wanted to ask it here. ---

  • Accessing a class's variable in Python 2010-08-08

    class Example(object): def the_example(self): itsProblem = "problem" theExample = Example() print(theExample.itsProblem) How do I access a class's variable? I've tried adding this definition: def return_itsProblem(self): return itsProblem Yet, that f

  • Python - Instance Variable Access 2011-05-01

    I am currently making a game. I have 2 classes and I want one to access the others instance variables. I'm not sure how to do this or if it is possible. Both Classes at some point inherits to the class gameEngine gameEngine <- Game gameEngine <

  • Which Python module to use to access the Proxy setting of Windows 7? 2011-08-04

    I am new to Python and would like to write a script to change Windows proxy settings based on the network I am connected to. Is there any existing python module I can use? Appreciate your help. Thanks, Sethu --------------Solutions------------- I wou

  • Python module not working (not able to import) even after including it in PATH variable 2012-02-04

    I have one testing module that I want to use for android testing. I have the files but there is no installation file for it, so I added the module to PATH variable, even then it doesn't work I try to import it. Any way to make it work. Do I have to p

  • Python module for multiple variable global optimization 2010-11-19

    I have been looking for a python module that implements the common techniques of global optimization (finding the global minimum of a function in N dimensions) without success. If you heard about a simulated annealing or genetic algorithm implementat

  • How to avoid computation every time a python module is reloaded 2008-10-12

    I have a python module that makes use of a huge dictionary global variable, currently I put the computation code in the top section, every first time import or reload of the module takes more then one minute which is totally unacceptable. How can I s

  • Lazy module variables--can it be done? 2009-09-22

    I'm trying to find a way to lazily load a module-level variable. Specifically, I've written a tiny Python library to talk to iTunes, and I want to have a DOWNLOAD_FOLDER_PATH module variable. Unfortunately, iTunes won't tell you where its download fo

  • Python: static variable decorator 2009-10-23

    I'd like to create a decorator like below, but I can't seem to think of an implementation that works. I'm starting to think it's not possible, but thought I would ask you guys first. I realize there's various other ways to create static variables in

  • Python Module Initialization Order? 2010-06-21

    I am a Python newbie coming from a C++ background. While I know it's not Pythonic to try to find a matching concept using my old C++ knowledge, I think this question is still a general question to ask: Under C++, there is a well known problem called

  • Python global variable insanity 2010-06-25

    You have three files: main.py, second.py, and common.py common.py #!/usr/bin/python GLOBAL_ONE = "Frank" main.py #!/usr/bin/python from common import * from second import secondTest if __name__ == "__main__": global GLOBAL_ONE print GLOBAL_ONE #Print

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.110 (s). 11 q(s)