開発サーバー起動時のエラー原因と解決方法

はじめに

githubからクローンしたDjango製タスク管理アプリの開発サーバーを以下のコマンドで起動しようとしたらエラーが発生した。

(.venv) hiroki@shibatahiroshitakanoiMac django_todo % python manage.py runserver

エラー内容

djangoがないですよというエラー。

Traceback (most recent call last):
  File "/Users/hiroki/Downloads/Python/django_todo/manage.py", line 11, in main
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/hiroki/Downloads/Python/django_todo/manage.py", line 22, in <module>
    main()
  File "/Users/hiroki/Downloads/Python/django_todo/manage.py", line 13, in main
    raise ImportError(
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

djangoをインストール

pip listでDjangoの存在確認をしたところなかったので、インストールした。無事、Django 5.1.6がインストールできた。

(.venv) hiroki@shibatahiroshitakanoiMac django_todo % pip list | grep Django
(.venv) hiroki@shibatahiroshitakanoiMac django_todo % pip install django    
Collecting django
  Downloading Django-5.1.6-py3-none-any.whl.metadata (4.2 kB)
Collecting asgiref<4,>=3.8.1 (from django)
  Downloading asgiref-3.8.1-py3-none-any.whl.metadata (9.3 kB)
Collecting sqlparse>=0.3.1 (from django)
  Downloading sqlparse-0.5.3-py3-none-any.whl.metadata (3.9 kB)
Downloading Django-5.1.6-py3-none-any.whl (8.3 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.3/8.3 MB 39.9 MB/s eta 0:00:00
Downloading asgiref-3.8.1-py3-none-any.whl (23 kB)
Downloading sqlparse-0.5.3-py3-none-any.whl (44 kB)
Installing collected packages: sqlparse, asgiref, django
Successfully installed asgiref-3.8.1 django-5.1.6 sqlparse-0.5.3

[notice] A new release of pip is available: 24.3.1 -> 25.0.1
[notice] To update, run: pip install --upgrade pip
(.venv) hiroki@shibatahiroshitakanoiMac django_todo % pip list | grep Django
Django            5.1.6

今度はrest_frameworkがないというエラー

開発サーバーを起動しようとしたら、rest_frameworkがないというエラーが出た。

(.venv) hiroki@shibatahiroshitakanoiMac django_todo % python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/Users/hiroki/.pyenv/versions/3.12.2/lib/python3.12/threading.py", line 1073, in _bootstrap_inner
    self.run()
  File "/Users/hiroki/.pyenv/versions/3.12.2/lib/python3.12/threading.py", line 1010, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/hiroki/Downloads/Python/.venv/lib/python3.12/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/Users/hiroki/Downloads/Python/.venv/lib/python3.12/site-packages/django/core/management/commands/runserver.py", line 126, in inner_run
    autoreload.raise_last_exception()
  File "/Users/hiroki/Downloads/Python/.venv/lib/python3.12/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "/Users/hiroki/Downloads/Python/.venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 394, in execute
    autoreload.check_errors(django.setup)()
  File "/Users/hiroki/Downloads/Python/.venv/lib/python3.12/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/Users/hiroki/Downloads/Python/.venv/lib/python3.12/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/hiroki/Downloads/Python/.venv/lib/python3.12/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
                 ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hiroki/Downloads/Python/.venv/lib/python3.12/site-packages/django/apps/config.py", line 193, in create
    import_module(entry)
  File "/Users/hiroki/.pyenv/versions/3.12.2/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'rest_framework'

control + Cで上記のモードを抜けて、djangorestframeworkをインストールした。

(.venv) hiroki@shibatahiroshitakanoiMac django_todo % pip install djangorestframework
Collecting djangorestframework
  Downloading djangorestframework-3.15.2-py3-none-any.whl.metadata (10 kB)
Requirement already satisfied: django>=4.2 in /Users/hiroki/Downloads/Python/.venv/lib/python3.12/site-packages (from djangorestframework) (5.1.6)
Requirement already satisfied: asgiref<4,>=3.8.1 in /Users/hiroki/Downloads/Python/.venv/lib/python3.12/site-packages (from django>=4.2->djangorestframework) (3.8.1)
Requirement already satisfied: sqlparse>=0.3.1 in /Users/hiroki/Downloads/Python/.venv/lib/python3.12/site-packages (from django>=4.2->djangorestframework) (0.5.3)
Downloading djangorestframework-3.15.2-py3-none-any.whl (1.1 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 17.6 MB/s eta 0:00:00
Installing collected packages: djangorestframework
Successfully installed djangorestframework-3.15.2

[notice] A new release of pip is available: 24.3.1 -> 25.0.1
[notice] To update, run: pip install --upgrade pip

開発環境の起動に成功した

(.venv) hiroki@shibatahiroshitakanoiMac django_todo % python manage.py runserver     
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
March 02, 2025 - 13:00:35
Django version 5.1.6, using settings 'django_todo.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

http://127.0.0.1:8000/api/にアクセスすることができた。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

投稿ID : 28859