python random.random() causes "'module' object is not callable" when used in custom template tag

16,813

Solution 1

The answer is ... strange.

When I originally wrote my custom tag, I called it random.py. I quickly realized that this name may not be good and renamed it randomchoice.py and deleted my random.py file. Python kept the compiled random.pyc file around, and it was getting loaded whenever I did import random. I removed my random.pyc file, and the problem went away.

Solution 2

Yes, this kind of error is pretty easy. Basically don't name any of your filenames or anything you create with the same names as any likely python modules you are going to use.

Solution 3

Its been a while since I tinkered around with Django, but if random.random is a "module", then try random.random.random(). Or maybe just try random(). You just don't know what kind of hackery goes on behind the scenes.

Edit

Try this:

sys.path = [r"C:\Program Files\Python26\lib\"] + sys.path
import random
sys.path.pop(0)
Share:
16,813
Krystian Cybulski
Author by

Krystian Cybulski

I am also the Director of Product Engineering at 15Five. We are building the next generation of performance management and employee engagement software. I am an application programmer and web developer with over 20 years of experience. I have started building web pages when the world wide web was still at its infancy and have been involved in the industry ever since. In the past I have worked at internet startups, medium sized firms, and large financial institutions. I was with 15Five from the very beginning and saw it grow to over 190 people. I have experience in many areas, but most specifically: custom web development and specialized web applications building usable websites and online applications, concentrating on ease of use and effectiveness developing highly scalable, low-latency software for demanding industries, such as finance, banking, and live auctions I grew up, studied, and worked all my life in the United States and have returned to Poland. I am currently offering web programming and application development services to firms world-wide. To learn more about me, please have a look at my LinkedIn profile.

Updated on July 28, 2022

Comments

  • Krystian Cybulski
    Krystian Cybulski almost 2 years

    If I start python from the command line and type:

    import random
    print "Random: " + str(random.random())
    

    It prints me a random number (Expected, excellent).

    If I include the above-two lines in my django application's models.py and start my django app with runserver I get the output on the command line showing me a random number (Great!)

    If I take a custom tag which works perfectly fine otherwise, but I include

    import random
    print "Random: " + str(random.random())
    

    as the first 2 lines of the custom tag's .py file, I get an error whenever I try to open up a template which uses that custom tag:

    TypeError at /help/
    'module' object is not callable
    

    Please keep in mind that if I get rid of these two lines, my custom tag behaves as otherwise expected and no error is thrown. Unfortunately, I need some random behavior inside of my template tag.

    The problem is if in a custom tag I do:

    import random
    

    on a custom template tag, it imports

    <module 'django.templatetags.random' from '[snip path]'> 
    

    and not

    <module 'random' from 'C:\\Program Files\\Python26\\lib\\random.pyc'>
    

    as is normally imported from everywhere else

    Django template library has a filter called random, and somehow it is getting priority above the system's random.

    Can anyone recommend how to explicitly import the proper python random?

  • Krystian Cybulski
    Krystian Cybulski about 15 years
    Thanks for the insight. I tried all that, and next I tried to print out which exact module it is importing. This added in important insight into the problem, which I included in the main post.