Home > Software design >  django-crispy-forms ModuleNotFoundError but can import in console
django-crispy-forms ModuleNotFoundError but can import in console

Time:04-18

i've been trying to get crispy forms working for 3 hours now it's driving me nuts, please help! (or suggest another/better way to style forms in django?!)

i have added crispy_forms to installed apps as:

INSTALLED_APPS = [
   ...
   'crispy_forms',
]

CRISPY_TEMPLATE_PACK = 'bootstrap4'

i'm on windows 10, using pycharm-pro with a venv. the interpreter works fine for everything else i have installed. in terminal i can run python then import crispy_forms with no errors, but pycharm throws a ModuleNotFoundError if i try to makemigrations, or run the server.

i tried toggling 'add contents and source root to system path" but honestly i don't really understand, and in any case it didn't help...

thanks!!

terminal outputs:

PS E:\Dev\Spotify\spo_djangos> pip show django-crispy-forms
Name: django-crispy-forms
Version: 1.14.0
Summary: Best way to have Django DRY forms
Home-page: https://github.com/django-crispy-forms/django-crispy-forms
Author: Miguel Araujo
Author-email: [email protected]
License: MIT
Location: c:\users\ryzen\appdata\local\programs\python\python310\lib\site-packages

PS E:\Dev\Spotify\spo_djangos> python
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import crispy_forms
>>>

python traceback:

E:\Dev\Spotify\spo_djangos\.venv\python.exe E:/Dev/Spotify/spo_djangos/manage.py runserver 8000
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "E:\Dev\Spotify\spo_djangos\.venv\lib\threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "E:\Dev\Spotify\spo_djangos\.venv\lib\threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
  File "E:\Dev\Spotify\spo_djangos\.venv\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "E:\Dev\Spotify\spo_djangos\.venv\lib\site-packages\django\core\management\commands\runserver.py", line 110, in inner_run
    autoreload.raise_last_exception()
  File "E:\Dev\Spotify\spo_djangos\.venv\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "E:\Dev\Spotify\spo_djangos\.venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    autoreload.check_errors(django.setup)()
  File "E:\Dev\Spotify\spo_djangos\.venv\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "E:\Dev\Spotify\spo_djangos\.venv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "E:\Dev\Spotify\spo_djangos\.venv\lib\site-packages\django\apps\registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "E:\Dev\Spotify\spo_djangos\.venv\lib\site-packages\django\apps\config.py", line 224, in create
    import_module(entry)
  File "E:\Dev\Spotify\spo_djangos\.venv\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'crispy_forms'

my interpreter config: interpreter config


update

i followed Error after installing crispy form

and got the fopllowing error:

Error: Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2022.1\plugins\python\helpers\packaging_tool.py", line 73, in run_pip
    runpy.run_module(module_name, run_name='__main__', alter_sys=True)
  File "E:\Dev\Spotify\spo_djangos\.venv\lib\runpy.py", line 209, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "E:\Dev\Spotify\spo_djangos\.venv\lib\runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "E:\Dev\Spotify\spo_djangos\.venv\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "E:\Dev\Spotify\spo_djangos2\venv\lib\site-packages\pip\__main__.py", line 29, in <module>
    from pip._internal.cli.main import main as _main
  File "E:\Dev\Spotify\spo_djangos2\venv\lib\site-packages\pip\_internal\cli\main.py", line 9, in <module>
    from pip._internal.cli.autocompletion import autocomplete
  File "E:\Dev\Spotify\spo_djangos2\venv\lib\site-packages\pip\_internal\cli\autocompletion.py", line 10, in <module>
    from pip._internal.cli.main_parser import create_main_parser
  File "E:\Dev\Spotify\spo_djangos2\venv\lib\site-packages\pip\_internal\cli\main_parser.py", line 8, in <module>
    from pip._internal.cli import cmdoptions
  File "E:\Dev\Spotify\spo_djangos2\venv\lib\site-packages\pip\_internal\cli\cmdoptions.py", line 23, in <module>
    from pip._internal.cli.parser import ConfigOptionParser
  File "E:\Dev\Spotify\spo_djangos2\venv\lib\site-packages\pip\_internal\cli\parser.py", line 12, in <module>
    from pip._internal.configuration import Configuration, ConfigurationError
  File "E:\Dev\Spotify\spo_djangos2\venv\lib\site-packages\pip\_internal\configuration.py", line 20, in <module>
    from pip._internal.exceptions import (
  File "E:\Dev\Spotify\spo_djangos2\venv\lib\site-packages\pip\_internal\exceptions.py", line 7, in <module>
    from pip._vendor.pkg_resources import Distribution
  File "E:\Dev\Spotify\spo_djangos2\venv\lib\site-packages\pip\_vendor\pkg_resources\__init__.py", line 80, in <module>
    from pip._vendor import platformdirs
  File "E:\Dev\Spotify\spo_djangos2\venv\lib\site-packages\pip\_vendor\platformdirs\__init__.py", line 31, in <module>
    PlatformDirs = _set_platform_dir_class()  #: Currently active platform
  File "E:\Dev\Spotify\spo_djangos2\venv\lib\site-packages\pip\_vendor\platformdirs\__init__.py", line 27, in _set_platform_dir_class
    result: Type[PlatformDirsABC] = getattr(importlib.import_module(module), name)
  File "E:\Dev\Spotify\spo_djangos\.venv\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "E:\Dev\Spotify\spo_djangos2\venv\lib\site-packages\pip\_vendor\platformdirs\windows.py", line 1, in <module>
    import ctypes
  File "E:\Dev\Spotify\spo_djangos\.venv\lib\ctypes\__init__.py", line 8, in <module>
    from _ctypes import Union, Structure, Array
ImportError: DLL load failed while importing _ctypes: The specified module could not be found.

pycharm error details says to run E:\Dev\Spotify\spo_djangos2\venv\Scripts\activate.bat PS E:\Dev\Spotify\spo_djangos> pip install django-crispy-forms in terminal sop i did. no errors, but still getting modulenotfound error

PS E:\Dev\Spotify\spo_djangos> E:\Dev\Spotify\spo_djangos2\venv\Scripts\activate.bat
PS E:\Dev\Spotify\spo_djangos> pip install django-crispy-forms
Collecting django-crispy-forms
  Using cached django_crispy_forms-1.14.0-py3-none-any.whl (133 kB)
Installing collected packages: django-crispy-forms
Successfully installed django-crispy-forms-1.14.0
PS E:\Dev\Spotify\spo_djangos> pip show django-crispy-forms
Name: django-crispy-forms
Version: 1.14.0
Summary: Best way to have Django DRY forms
Home-page: https://github.com/django-crispy-forms/django-crispy-forms
Author: Miguel Araujo
Author-email: [email protected]
License: MIT
Location: c:\users\ryzen\appdata\local\programs\python\python310\lib\site-packages
Requires:
Required-by:
PS E:\Dev\Spotify\spo_djangos> 

CodePudding user response:

windows execution policy was interfering, activate.bat was failing silently so i thought the venv was activated but i was just in the venv directory.

adjusted execution policies and got crispy forms installed.

hopefully this can help someone else some day! (here for instructions, don't set them too liberal!! https://docs.microsoft.com/en-gb/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.)

CodePudding user response:

All you have to do is to install django-crispy-forms by:

pip install django-crispy-forms

or

pip install --upgrade django-crispy-forms

in settings.py installed apps add crispy_forms In settings.py add:

 CRISPY_TEMPLATE_PACK = 'bootstrap4'
  • Related