Python 导入错误:没有名为脆皮形式的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28560968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:26:31  来源:igfitidea点击:

ImportError: No module named crispy-forms

pythondjangodjango-crispy-forms

提问by beowwwulf

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

我正在开发一些 django 应用程序,仍然很菜。想使用脆皮形式,但 eclipse 和 django 无法识别它。

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...:

这是我最近与views.py一起添加到站点文件夹中的forms.py,它抱怨未解决的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:

这是我的 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:

我正在运行 virtualenv,我的 venv-dir 如下所示:

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?

我今天不小心输入了:pip install python,似乎确实如此。这对它有什么影响吗?我的意思是,确保没有软件冲突不是 virtualenv 的任务吗?

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

无论如何,在我弄清楚之前,我现在无法进行任何 Django 工作,有什么帮助吗?

Update 1changes:

更新 1更改:

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

更新 2:安装一个新的 django 项目,新的 virtualenv,全新的......同样的事情发生

(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$ pip install django-crispy-forms 收集 django-crispy-forms 使用缓存的 django-crispy-forms-1.4.0.tar.gz 安装收集的包:django-crispy -forms 为 django-crispy-forms 运行 setup.py install 成功安装 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:

我现在尝试将crispy_forms安装到一个完整的项目中,查看我的virtualenv,我看到它在那里:

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...

所以,很清楚。Django 甚至不承认它安装了它。怎么可能??virtualenv 说它在那里,但 django 看不到它???重量...

采纳答案by beowwwulf

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

好的,所以我偶然(几乎)找到了另一篇文章,这篇文章:陷入 Django 错误:没有名为注册的模块

and thought it might have something to do with pythonpath.

并认为它可能与pythonpath有关。

so then I tried easy_install, like suggested:

所以我尝试了easy_install,就像建议的那样:

 $ 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:

现在它起作用了!我仍然认为 pythonpath 可能缺少一些东西,因为我一直在 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!!

但是 django 和crispy_forms 现在可以一起工作了。YippikayeyMF!!

回答by kristian

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

您必须确保将crispy-forms 安装到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

  1. 激活 VirtualEnv(其中 env 是虚拟环境的目录/名称):

    源环境/bin/激活

  2. 安装脆皮形式

    pip 安装 django-crispy-forms

回答by Abhishek Agarwala

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.

根据文档:http: //django-crispy-forms.readthedocs.org/en/latest/install.html#installing-django-crispy-forms ,您必须将“crispy_forms”而不是“crispy-forms”添加到您的已安装的应用程序列表。

回答by David Kaplan

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...

我将 PyCharm 用于我的 Django 项目。我得到与您完全相同的错误,但在 PyCharm 中,直到我更改 PyCharm 用于项目的 Python 解释器。对于每个项目,我必须选择位于相关 virtalenv 文件夹中的 Python 解释器,以使 IDE (PyCharm) 识别已安装的模块。我怀疑 Pythonpath 潜伏在后台的某个地方...

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

Eclipse 不允许您选择用于项目的解释器吗?那就是我开始寻找的地方。

Good luck!

祝你好运!

回答by Chiradip

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.
  1. 退出 Virtualenv。
  2. 在根目录中执行新的脆皮形式的 pip 安装。
  3. 返回 Virtualenv 并进行迁移,然后进行迁移。

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.

我认为 django 安装需要将所有第三方应用程序安装在虚拟环境内外。
如果有人可以帮助我了解原因背后的内部细节,我将不胜感激。

回答by Johnny Camby

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.

以上所有答案都缺少一件事。在执行上述任何建议之前,请先交叉检查驱动器上“外部库”的位置。原因: - 您使用的 IDE 指向不同的 python 目录,这适用于多次安装 python 的人。

  • 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
  • 不要认为您的“virtualenv”指向正确的目录,因为您的“外部库”已被存储。所以如果你继续安装在错误的目录中,那么这样的错误就可以了

回答by Toguko

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.

我现在解决了这个问题,我意识到安装的版本是 python 2.7 版本,但我使用的是 Django-1.10 和 python 3.5,我认为这也是你的问题。

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

尝试: pip3 install --user django-crispy-forms