属性错误:模块'profile'没有'run'属性。

5

我有一个扩展了AbstractUser的扩展用户模型,名为Profile。这个模型在一个名为"profiles"的独立应用程序中。我想使用标准的登录功能,但发现它默认要使用名为"profile"的应用程序,于是我重命名了应用程序、代码和数据库架构。虽然apps.py文件中的app_name已经设置为"profile",但无需重命名url引用。

但是还是不行,出现了标题中的错误。

于是我删除了所有迁移记录(目前我还可以这样做:)),并删除了我当前正在使用的sqlite数据库,以便重新运行迁移并重新创建所有内容。

但是仍然出现相同的错误。

堆栈跟踪如下:

λ  python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\core\management\base.py", line 361, in execute
    self.check()
  File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\core\management\base.py", line 390, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\core\management\base.py", line 377, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\debug_toolbar\apps.py", line 18, in check_middleware
    from debug_toolbar.middleware import DebugToolbarMiddleware
  File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\debug_toolbar\middleware.py", line 12, in <module>
    from debug_toolbar.toolbar import DebugToolbar
  File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\debug_toolbar\toolbar.py", line 141, in <module>
    urlpatterns = DebugToolbar.get_urls()
  File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\debug_toolbar\toolbar.py", line 134, in get_urls
    for panel_class in cls.get_panel_classes():
  File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\debug_toolbar\toolbar.py", line 116, in get_panel_classes
    import_string(panel_path) for panel_path in dt_settings.get_panels()
  File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\debug_toolbar\toolbar.py", line 116, in <listcomp>
    import_string(panel_path) for panel_path in dt_settings.get_panels()
  File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Users\mjnic\.virtualenvs\pyp-E_0Se9Bl\lib\site-packages\debug_toolbar\panels\profiling.py", line 1, in <module>
    import cProfile
  File "D:\Python\Python37-32\Lib\cProfile.py", line 22, in <module>
    run.__doc__ = _pyprofile.run.__doc__
AttributeError: module 'profile' has no attribute 'run'

在这个更名中,我错过了什么?

还有其他我没有发布的信息可以帮助吗?(我已经搜索了任何“配置文件”的实例,但现在一切都已正确更名,应用程序之前运行良好,因此纯粹是这个更名弄乱了它)。

编辑:可能“配置文件”与某些内部Python例程冲突吗? Python安装下的cProfile.py文件被定义为

#! /usr/bin/env python3

"""Python interface for the 'lsprof' profiler.
   Compatible with the 'profile' module.
"""

那可能与我的项目无关?我只需要将我的应用程序/扩展用户模型命名为除“profile”之外的其他名称吗?


尝试从__pycache__中删除文件,然后检查。 - Nalin Dobhal
没有帮助。同样的错误。 - michjnich
4个回答

22

好的,是的,这是一个命名冲突。我已经将我的“Profile”模型和“profile”应用程序重命名为“UserProfile”和“user_profile”,并相应地更新了所有内容。现在似乎一切都正常了...

只是在这里放一个答案,以防将来有人遇到同样的问题!


1
我遇到了相同的问题,只想澄清一下,重命名_仅应用程序_(而不是档案模型)就足以解决问题。 - Terseus

13
在我的情况下,是profile.py文件出了问题。 删除该文件后再尝试。

1
非常感谢您的回答。文档中在哪里说创建一个名为 profile.py 的文件会造成各种混乱?!? - Matthew Walker

1
在我的情况下,是应用程序“profile”和“django-debug-toolbar”之间的冲突:
我将我的应用程序从“profile”重命名为“account”,问题得到了解决。

0
对于那些在Django中有一个名为"profile"的应用程序,并且还想在Django shell中使用ipython作为接口的人来说,我找到了一个小技巧的解决方案。最近,我遇到了同样的问题,我只是深入研究了库,并改变了一行代码。
在我的情况下,我不需要将ipython作为必需的库提交到requirement.txt中。所以我只是在本地修改了代码。
回到错误,它说:
文件 "/some_path/.pyenv/versions/3.7.17/lib/python3.7/cProfile.py",第22行, run.__doc__ = _pyprofile.run.__doc__ AttributeError: 模块'profile'没有属性'run'
似乎它相对导入了错误的profile.py(在我的情况下,我有一个名为"profile"的应用程序)
我所做的就是进入那个cProfile.py,改变以下行: 从: import profile as _pyprofile 到: from .profile import Profile as _pyprofile 问题解决。 Python 版本:3.7.17 IPython 版本:7.34.0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接