ImportError: No module named crispy-forms

2019-03-28 10:17发布

问题:

I'm working on some django apps, pretty noob still. Would like to use crispy-forms, but eclipse and django doesnt recognize it.

Trying to runserver or shell:

$ python manage.py runserver

this happens:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 87, in create
    module = import_module(entry)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named crispy-forms

This is the forms.py I recently added to the site-folder alongside views.py, and it complains about unresolved import of crispy_forms...:

from django.contrib.auth.forms import UserCreationForm
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, ButtonHolder, Submit
from wx.lib.pubsub.core import kwargs

class RegistrationForm(UserCreationForm):
    def __init__(self, *args, **kwargs):
        super(RegistrationForm, self).__init__(*args, **kwargs)

        self.helper = FormHelper()
        self.helper.layout = Layout(
            'username',
            'password1',
            'password2',
            ButtonHolder(
                Submit('register', 
                       'Register', 
                       css_class='btn-prima'
                )
            )
        )

This is my part of my settings.py:

    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'polls',
        'crispy-forms',
    )

...
CRISPY_TEMPLATE_PACK = 'bootstrap'

I'm running virtualenv, my venv-dir looks like this:

venv/bin$ ls -la
total 2916
drwxr-xr-x 3 nr1 nr1    4096 Feb 17 11:24 .
drwxr-xr-x 6 nr1 nr1    4096 Feb 16 19:38 ..
-rw-r--r-- 1 nr1 nr1    2220 Feb 16 19:35 activate
-rw-r--r-- 1 nr1 nr1    1276 Feb 16 19:35 activate.csh
-rw-r--r-- 1 nr1 nr1    2489 Feb 16 19:35 activate.fish
-rw-r--r-- 1 nr1 nr1    1137 Feb 16 19:35 activate_this.py
-rwxr-xr-x 1 nr1 nr1     300 Feb 16 19:44 django-admin
-rwxr-xr-x 1 nr1 nr1     159 Feb 16 19:44 django-admin.py
-rw-r--r-- 1 nr1 nr1     304 Feb 16 19:44 django-admin.pyc
-rwxr-xr-x 1 nr1 nr1     267 Feb 17 11:24 easy_install
-rwxr-xr-x 1 nr1 nr1     267 Feb 17 11:24 easy_install-2.7
drwxr-xr-x 7 nr1 nr1    4096 Feb 16 19:47 .git
-rwxr-xr-x 1 nr1 nr1    2364 Feb 17 00:13 pilconvert.py
-rwxr-xr-x 1 nr1 nr1   15631 Feb 17 00:13 pildriver.py
-rwxr-xr-x 1 nr1 nr1    2609 Feb 17 00:13 pilfile.py
-rwxr-xr-x 1 nr1 nr1    1055 Feb 17 00:13 pilfont.py
-rwxr-xr-x 1 nr1 nr1    2410 Feb 17 00:13 pilprint.py
-rwxr-xr-x 1 nr1 nr1     239 Feb 17 11:24 pip
-rwxr-xr-x 1 nr1 nr1     239 Feb 17 11:24 pip2
-rwxr-xr-x 1 nr1 nr1     239 Feb 17 11:24 pip2.7
-rwxr-xr-x 1 nr1 nr1 2884984 Feb 17 11:24 python
lrwxrwxrwx 1 nr1 nr1       6 Feb 17 11:24 python2 -> python
lrwxrwxrwx 1 nr1 nr1       6 Feb 17 11:24 python2.7 -> python
-rwxr-xr-x 1 nr1 nr1    3886 Feb 17 00:11 sqlformat

I accidentally managed to type in: pip install python today, and it seems it did. Could that have any impact on it? I mean, isnt it virtualenv's task to make sure there is no software-conflicts?

Anyway, I cant get any Django-work going now until i figure this out, any help out there?

Update 1 changes:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
    'crispy_forms',
)

$ python manage.py runserver

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 87, in create
    module = import_module(entry)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named crispy_forms


$ cat requirements.txt 
Django==1.7.4
django-crispy-forms==1.4.0
django-debug-toolbar==1.2.2
django-extras==0.3
django-grappelli==2.6.3
django-haystack==2.3.1
django-reversion==1.8.5
django-tastypie==0.12.1
easy-thumbnails==2.2
Pillow==2.7.0
python-dateutil==2.4.0
python-mimeparse==0.1.4
requests==2.5.1
six==1.9.0
sqlparse==0.1.14

update 2: Installing a new django-project, new virtualenv, all new.. SAME THING HAPPENS

(abc)nr1@kali:~/workspace/websites/abc$ pip install django-crispy-forms Collecting django-crispy-forms Using cached django-crispy-forms-1.4.0.tar.gz Installing collected packages: django-crispy-forms Running setup.py install for django-crispy-forms Successfully installed django-crispy-forms-1.4.0

(abc)nr1@kali:~/workspace/websites/abc$ python manage.py runserver
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/nr1/Envs/abc/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/nr1/Envs/abc/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/home/nr1/Envs/abc/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/nr1/Envs/abc/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/home/nr1/Envs/abc/local/lib/python2.7/site-packages/django/apps/config.py", line 123, in create
    import_module(entry)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named crispy_forms

I now try to install crispy_forms to a completely project, looking in my virtualenv, I see its there:

nr1@kali:~/Envs/abc/local/lib/python2.7/site-packages$ ls -la
total 56
drwxr-xr-x 12 nr1 nr1 4096 Feb 17 21:50 .
drwxr-xr-x  4 nr1 nr1 4096 Feb 17 20:31 ..
drwxr-xr-x  5 nr1 nr1 4096 Feb 17 21:50 crispy_forms
drwxr-xr-x 17 nr1 nr1 4096 Feb 17 20:40 django
drwxr-xr-x  2 nr1 nr1 4096 Feb 17 20:40 Django-1.7.4.dist-info
drwxr-xr-x  2 nr1 nr1 4096 Feb 17 21:50 django_crispy_forms-1.4.0-py2.7.egg-info
-rw-r--r--  1 nr1 nr1  126 Feb 17 20:31 easy_install.py
-rw-r--r--  1 nr1 nr1  315 Feb 17 20:31 easy_install.pyc
drwxr-xr-x  2 nr1 nr1 4096 Feb 17 20:31 _markerlib
drwxr-xr-x 10 nr1 nr1 4096 Feb 17 20:31 pip
drwxr-xr-x  2 nr1 nr1 4096 Feb 17 20:31 pip-6.0.8.dist-info
drwxr-xr-x  4 nr1 nr1 4096 Feb 17 20:31 pkg_resources
drwxr-xr-x  4 nr1 nr1 4096 Feb 17 20:31 setuptools
drwxr-xr-x  2 nr1 nr1 4096 Feb 17 20:31 setuptools-12.0.5.dist-info

So, its clear. Django doesn't even recognize that its installed it. How can that be?? virtualenv says its there, but django cant see it??? wtx...

回答1:

I solve this problem right now, I realize that the crispy-form installed version was the python 2.7 version, but I'm using Django-1.10 with python 3.5, and I think this is your problem too.

Try: pip3 install --user django-crispy-forms



回答2:

You have to make sure that you install crispy-forms into the virtualenv.

  1. Activate VirtualEnv (where env is the directory/name of your virtual environment):

    source env/bin/activate

  2. Install crispy-forms

    pip install django-crispy-forms



回答3:

As per the documentation : http://django-crispy-forms.readthedocs.org/en/latest/install.html#installing-django-crispy-forms, you have to add 'crispy_forms' not 'crispy-forms' to your Installed Apps list.



回答4:

Ok, so I found by chance(almost) another post, this one: Getting stuck at Django error: No module named registration

and thought it might have something to do with pythonpath.

so then I tried easy_install, like suggested:

 $ easy_install -Z django-crispy-forms
    Searching for django-crispy-forms
    Best match: django-crispy-forms 1.4.0
    Adding django-crispy-forms 1.4.0 to easy-install.pth file

    Using /home/nr1/Envs/abc/lib/python2.7/site-packages
    Processing dependencies for django-crispy-forms
    Finished processing dependencies for django-crispy-forms

Now it works! I still think there might be something missing with the pythonpath, because I keep getting this in eclipse:

SignUpView Found at: __module_not_in_the_pythonpath__

, and I would like someone to clarify it, so feel free to contribute here...

but django and crispy_forms now works together. YippikayeyMF!!



回答5:

I use PyCharm for my Django projects. I get exactly the same error as you but in PyCharm until I change the Python interpreter used by PyCharm for the project. I have to, for each project, select the Python interpreter that resides in the relevant virtalenv folder to get the IDE (PyCharm) to recognize the installed modules. I suspect that the Pythonpath lurks somewhere in the background...

Doesn't Eclipse allow you to select the interpreter to use for a project? That's where I'd start looking.

Good luck!



回答6:

I came across the same problem and figured out an alternate way around.

  1. Exit Virtualenv.
  2. Do a fresh pip install of crispy form at the root directory.
  3. Go back to Virtualenv and make migrations followed by migrate.

I think the django installation needs that all third party app be installed inside and as well outside the virtual env.
I would really appreciate if someone could help me with inside details behind the reason.



回答7:

All the above answers are missing one thing. Please cross check the location of your 'External Libraries' on the drive first before you do any of the above advises. Reason: - You be using an IDE that points to a different python directory, that is for guys who have install python more than once.

  • don't think your 'virtualenv' is pointing to the right directory were your 'External Libraries' are stored. So if you go ahead and install in a wrong directory then such error will ok