python random.random() 在自定义模板标签中使用时会导致“'module'对象不可调用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/837916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:57:28  来源:igfitidea点击:

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

pythondjangorandomtemplatetags

提问by Krystian Cybulski

If I start python from the command line and type:

如果我从命令行启动 python 并输入:

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!)

如果我在我的 django 应用程序的 models.py 中包含以上两行并使用 runserver 启动我的 django 应用程序,我会在命令行上得到一个显示随机数的输出(太棒了!)

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:

作为自定义标记的 .py 文件的前 2 行,每当我尝试打开使用该自定义标记的模板时,都会收到错误消息:

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.

Django 模板库有一个名为 random 的过滤器,不知何故它的优先级高于系统的随机数。

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

谁能推荐如何显式导入正确的python random?

回答by Krystian Cybulski

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.pyand deleted my random.pyfile. Python kept the compiled random.pycfile around, and it was getting loaded whenever I did import random. I removed my random.pyc file, and the problem went away.

当我最初编写自定义标签时,我称它为random.py。我很快意识到这个名字可能不好,并重命名randomchoice.py并删除了我的random.py文件。Python 保留了编译后的random.pyc文件,每当我这样做时它就会被加载import random。我删除了我的 random.pyc 文件,问题就消失了。

回答by Chris

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.

是的,这种错误很容易。基本上不要使用与您将要使用的任何可能的 Python 模块相同的名称命名您的任何文件名或您创建的任何内容。

回答by Unknown

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.

自从我对 Django 进行修补已经有一段时间了,但是如果 random.random 是一个“模块”,那么尝试 random.random.random()。或者也许只是尝试 random()。你只是不知道幕后发生了什么样的黑客行为。

Edit

编辑

Try this:

试试这个:

sys.path = [r"C:\Program Files\Python26\lib\"] + sys.path
import random
sys.path.pop(0)