stefanfoulis / django-phonenumber-field

A django model and form field for normalised phone numbers using python-phonenumbers
MIT License
1.49k stars 315 forks source link

django-phonenumber-field[phonenumberslite] requires phonenumbers on migration #608

Closed davidtingsu closed 4 months ago

davidtingsu commented 4 months ago

I installed django-phonenumber-field with phonenumberslite and when I ran the migration I got No module named phonenumbers. My work around was just to install phonenumbers

Reproduction steps:

poetry add "django-phonenumber-field[phonenumberslite]"
poetry shell
 python manage.py makemigrations

 Traceback (most recent call last):
  File "/Users/davidsu/myproject/manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/davidsu/.pyenv/versions/3.11.7/lib/python3.11/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/Users/davidsu/.pyenv/versions/3.11.7/lib/python3.11/site-packages/django/core/management/__init__.py", line 395, in execute
    django.setup()
  File "/Users/davidsu/.pyenv/versions/3.11.7/lib/python3.11/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/davidsu/.pyenv/versions/3.11.7/lib/python3.11/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/Users/davidsu/.pyenv/versions/3.11.7/lib/python3.11/site-packages/django/apps/config.py", line 301, in import_models
    self.models_module = import_module(models_module_name)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/davidsu/.pyenv/versions/3.11.7/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/davidsu/myproject/patients/models/__init__.py", line 14, in <module>
    from .provider import Provider
  File "/Users/davidsu/myproject/patients/models/provider.py", line 4, in <module>
    from phonenumber_field.modelfields import PhoneNumberField
  File "/Users/davidsu/.pyenv/versions/3.11.7/lib/python3.11/site-packages/phonenumber_field/modelfields.py", line 7, in <module>
    from phonenumber_field import formfields
  File "/Users/davidsu/.pyenv/versions/3.11.7/lib/python3.11/site-packages/phonenumber_field/formfields.py", line 1, in <module>
    import phonenumbers
ModuleNotFoundError: No module named 'phonenumbers'
Sentry is attempting to send 2 pending events
francoisfreitag commented 4 months ago

phonenumberslite provides the phonenumbers package.

/tmp/tmp.8mztCz8Hch % python -m venv venv
/tmp/tmp.8mztCz8Hch % . venv/bin/activate
(venv) /tmp/tmp.8mztCz8Hch % pip install 'django-phonenumber-field[phonenumberslite]'
Collecting django-phonenumber-field[phonenumberslite]
  Downloading django_phonenumber_field-7.3.0-py3-none-any.whl.metadata (3.1 kB)
Collecting Django>=3.2 (from django-phonenumber-field[phonenumberslite])
  Using cached Django-5.0.6-py3-none-any.whl.metadata (4.1 kB)
Collecting phonenumberslite>=7.0.2 (from django-phonenumber-field[phonenumberslite])
  Using cached phonenumberslite-8.13.39-py2.py3-none-any.whl.metadata (11 kB)
Collecting asgiref<4,>=3.7.0 (from Django>=3.2->django-phonenumber-field[phonenumberslite])
  Using cached asgiref-3.8.1-py3-none-any.whl.metadata (9.3 kB)
Collecting sqlparse>=0.3.1 (from Django>=3.2->django-phonenumber-field[phonenumberslite])
  Using cached sqlparse-0.5.0-py3-none-any.whl.metadata (3.9 kB)
Using cached Django-5.0.6-py3-none-any.whl (8.2 MB)
Using cached phonenumberslite-8.13.39-py2.py3-none-any.whl (470 kB)
Downloading django_phonenumber_field-7.3.0-py3-none-any.whl (66 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 66.5/66.5 kB 2.7 MB/s eta 0:00:00
Using cached asgiref-3.8.1-py3-none-any.whl (23 kB)
Using cached sqlparse-0.5.0-py3-none-any.whl (43 kB)
Installing collected packages: phonenumberslite, sqlparse, asgiref, Django, django-phonenumber-field
Successfully installed Django-5.0.6 asgiref-3.8.1 django-phonenumber-field-7.3.0 phonenumberslite-8.13.39 sqlparse-0.5.0

[notice] A new release of pip is available: 24.0 -> 24.1
[notice] To update, run: pip install --upgrade pip
(venv) /tmp/tmp.8mztCz8Hch % python
Python 3.12.4 (main, Jun  7 2024, 06:33:07) [GCC 14.1.1 20240522] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import phonenumbers
>>>

I think your issue is that phonenumberlite was not installed in the environment running the migrations (or the venv not activated).