AttributeError: module 'profile' has no attribute 'run'
Asked Answered
V

4

6

So I have an extended User model (extended AbstractUser) called Profile. This was in a seperate app called "profiles". I was plugging in the standard login and realised it was looking for a "profile" app name as standard, so I renamed it (directories, code, DB schema) which I thought should work. app_name was actually already set to "profile" in apps.py, so didn't need to rename url references.

No dice. I get the error in the title.

So I removed all migrations (I'm at the stage where I can still do this OK :) ) and deleted the sqlite DB that I'm currently working with, thinking I'd just rerun the migrations and recreate everything.

Same error.

The stack trace is:

λ  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'

What have I missed as part of this rename?

Any other info I've missed posting that would help? (I've searched for any instance of "profiles", but everything is correctly renamed now, and the app was working OK prior to this, so it's purely this rename that's messed it up).

Edit: Can it be that "profile" conflicts with some internal python routine? That cProfile.py file under the python installation is defined as

#! /usr/bin/env python3

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

Now that can't be anything to do with my project? Do I just need to call my app/extended user model something other than "profile"?

Virulent answered 2/8, 2019 at 9:41 Comment(2)
try to delete files from pycache and then check.Faxun
Didn't help. Same error.Virulent
V
26

OK. So yes, it's a naming clash. I've renamed my "Profile" model and "profile" app as "UserProfile" and "user_profile" and updated everything accordingly. It all seems to work now ...

Just putting an answer here in case anybody comes along in the future with the same issue!

Virulent answered 2/8, 2019 at 11:26 Comment(1)
I've encountered the same issue and just wanted to clarify that renaming only the app (not the Profile model) it's enough to fix the problem.Captivity
P
15

In my case, it was profile.py file.

Delete the file and try again.

Pacifist answered 29/12, 2020 at 11:20 Comment(1)
Thanks enormously for this answer. Where in the documentation does it say that creating a file called profile.py plays all sorts of havoc?!?Pich
P
1

In my case was the conflict between app - 'profile' and 'django-debug-toolbar':

I rename my app from 'profile' to 'account' and it solved the problem

Peppery answered 14/2, 2023 at 10:33 Comment(0)
G
0

For those who have app named "profile" in Django and also want to use ipython as interface in Django shell, I got a little tricky solution. Recently, I encountered the same problem, and I just dived into the library, and changed a single line of code.

In my case, I don't need to commit ipython as a required library in requirement.txt. So I just amend the code locally.

Go back to the error, it said:

File "/some_path/.pyenv/versions/3.7.17/lib/python3.7/cProfile.py", line 22, in <module>
run.__doc__ = _pyprofile.run.__doc__
AttributeError: module 'profile' has no attribute 'run'

Seems it relatively imported the wrong profile.py (in my case I have an app called profile)

What I did is just go to that cProfile.py, change the following line:
From:
import profile as _pyprofile
To:
from .profile import Profile as _pyprofile

Problem solved.
python version: 3.7.17
ipython version: 7.34.0

Gleam answered 21/10, 2023 at 1:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.