cProfile for Python 无法识别函数名称
我在一个名为电子邮件的应用程序中有一个功能,我想对其进行分析。当我尝试执行类似的操作时,它会崩溃
from django.core.management import BaseCommand
import cProfile
class Command(BaseCommand):
def handle(self, *args, **options):
from email.modname import send_email
cProfile.run('send_email(user_id=1, city_id=4)')
当我运行此管理命令时,它会抛出以下错误:
exec cmd in globals, locals
File "<string>", line 1, in <module>
NameError: name 'send_email' is not defined
我在这里缺少什么? cProfile 如何评估字符串(在全局/本地命名空间中查找函数名称)?
I have a function in an app called email which I want to profile. When I try to do something like this, it blows up
from django.core.management import BaseCommand
import cProfile
class Command(BaseCommand):
def handle(self, *args, **options):
from email.modname import send_email
cProfile.run('send_email(user_id=1, city_id=4)')
When I run this management command, it throws the following error:
exec cmd in globals, locals
File "<string>", line 1, in <module>
NameError: name 'send_email' is not defined
What am I missing here? How does cProfile evaluate the string (look up func names in the global/local namespace)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您在方法定义中导入了
send_email
。我建议您使用
runctx
:来自官方文档:
< em>此函数与 run() 类似,但添加了参数来为命令字符串提供全局和本地字典。
The problem is that you imported
send_email
inside your method definition.I suggest you to use
runctx
:From the official documentation:
This function is similar to run(), with added arguments to supply the globals and locals dictionaries for the command string.