Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#30215 closed Bug (fixed)

`AttributeError: __spec__` on `./manage.py runserver`

Reported by: Flaviu Tamas Owned by: Florian Moussous
Component: Core (Management commands) Version: 2.2
Severity: Release blocker Keywords:
Cc: Florian Moussous Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

I get a AttributeError: __spec__ when I try using the live server. Here's the full stack trace:

$ pipenv run ./manage.py runserver
Watchman unavailable: pywatchman not installed.
Watching for file changes with StatReloader
Performing system checks…

Traceback (most recent call last):
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/py/_vendored_packages/apipkg.py", line 141, in __makeattr
    modpath, attrname = self.__map__[name]
KeyError: '__spec__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/dev/inventory/manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 95, in handle
    self.run(**options)
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 102, in run
    autoreload.run_with_reloader(self.inner_run, **options)
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/django/utils/autoreload.py", line 579, in run_with_reloader
    start_django(reloader, main_func, *args, **kwargs)
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/django/utils/autoreload.py", line 564, in start_django
    reloader.run(django_main_thread)
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/django/utils/autoreload.py", line 275, in run
    self.run_loop()
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/django/utils/autoreload.py", line 281, in run_loop
    next(ticker)
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/django/utils/autoreload.py", line 321, in tick
    state.update(self.loop_files(state, previous_timestamp))
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/django/utils/autoreload.py", line 328, in loop_files
    for path, mtime in self.snapshot_files():
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/django/utils/autoreload.py", line 348, in snapshot_files
    for file in self.watched_files():
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/django/utils/autoreload.py", line 241, in watched_files
    yield from iter_all_python_module_files()
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/django/utils/autoreload.py", line 103, in iter_all_python_module_files
    return iter_modules_and_files(modules, frozenset(_error_files))
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/django/utils/autoreload.py", line 114, in iter_modules_and_files
    if not isinstance(module, ModuleType) or module.__spec__ is None:
  File "/home/user/etc/.virtualenvs/inventory-HrJj1m_g/lib/python3.7/site-packages/py/_vendored_packages/apipkg.py", line 146, in __makeattr
    raise AttributeError(name)
AttributeError: __spec__

The fix is really simple: in django/utils/autoreload.py, line 114, put or not hasattr(module, '__spec__') before or module.__spec__ is None:

Change History (13)

comment:1 by Flaviu Tamas, 6 years ago

(fyi I'm on 2.2b1)

comment:2 by Simon Charette, 6 years ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

As you've reported getattr(module, '__spec__', None) is None should do.

Are you interested in submitting a Github PR to address the issue?

comment:3 by Florian Moussous, 6 years ago

Hello
Just for my information, I'm new to contributing (never submitted a patch yet), and I'm trying to reproduce the bug, just to learn how it works, but I can't.
I checked out the tag 2.2b1, installed requirements in a virtualenv, started a new project, and fired ./manage runserver
What other steps can I take to reproduce the bug?
(sorry if this not the right place to discuss it)

comment:4 by Florian Moussous, 6 years ago

Cc: Florian Moussous added

comment:5 by Flaviu Tamas, 6 years ago

I should have done a better job presenting a reproducible case.

  1. pip install django==2.2b py
  2. django-admin startproject mysite
  3. Add import py to the top of mysite/mysite/urls.py
  4. ./manage.py runserver and the error will show up.

comment:6 by Ahisahar Pretel, 6 years ago

Owner: changed from nobody to Ahisahar Pretel
Status: newassigned

Wops sorry, I did not see the comment, do you still want the issue or can I do it? @Florian Moussous

comment:7 by Florian Moussous, 6 years ago

Actually I was wondering if someone already posted a patch somewhere because the checkbox "Has patch" is checked... So I did not send the PR yet because I don't want to cause a mess
But if the problem still needs fixing, I'd be glad to do so: I've already written the patch and the test to go with it.

comment:8 by Ahisahar Pretel, 6 years ago

Owner: Ahisahar Pretel removed
Status: assignednew

comment:9 by Ahisahar Pretel, 6 years ago

Feel free then :)

comment:10 by Florian Moussous, 6 years ago

Owner: set to Florian Moussous
Status: newassigned

Thanks

comment:12 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In 4dac17ac:

[2.2.x] Fixed #30215 -- Fixed autoreloader crash for modules without spec.

Regression in c8720e7696ca41f3262d5369365cc1bd72a216ca.
Backport of 99cfb28e995388db109e2f2ed15307f09945dda8 from master.

comment:13 by Tim Graham <timograham@…>, 6 years ago

In 99cfb28:

Fixed #30215 -- Fixed autoreloader crash for modules without spec.

Regression in c8720e7696ca41f3262d5369365cc1bd72a216ca.

Note: See TracTickets for help on using tickets.
Back to Top