cx_freeze 无法捆绑 django.utils.six.moves

Posted

技术标签:

【中文标题】cx_freeze 无法捆绑 django.utils.six.moves【英文标题】:cx_freeze fails to bundle django.utils.six.moves 【发布时间】:2014-07-26 16:49:16 【问题描述】:

我正在尝试将 django 与 cx_freeze 捆绑在一起。使用我的 setup.py,我可以将它捆绑在一起,但是当我调用生成的可执行文件时,会出现以下导入错误。我尝试了各种方法来解决这个问题,但无法解决。

使用的版本: Django==1.6.4 cx-冻结==4.3.3

./build/exe.linux-x86_64-2.7/script
Traceback (most recent call last):
  File "lib/python2.7/site-packages/cx_Freeze/initscripts/Console.py", line 27, in <module>
    exec(code, m.__dict__)
  File "script.py", line 7, in <module>
    from models import Project
  File "/remote/vgrnd77/pritam/tryout/package/models.py", line 6, in <module>
    from django.db.models import CharField, Model, \
  File "lib/python2.7/site-packages/django/db/models/__init__.py", line 5, in <module>
    from django.db.models.query import Q
  File "lib/python2.7/site-packages/django/db/models/query.py", line 14, in <module>
    from django.db.models.fields import AutoField
  File "lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 15, in <module>
    from django import forms
  File "lib/python2.7/site-packages/django/forms/__init__.py", line 8, in <module>
    from django.forms.fields import *
  File "lib/python2.7/site-packages/django/forms/fields.py", line 17, in <module>
    from django.forms.util import ErrorList, from_current_timezone, to_current_timezone
  File "lib/python2.7/site-packages/django/forms/util.py", line 4, in <module>
    from django.utils.html import format_html, format_html_join
  File "lib/python2.7/site-packages/django/utils/html.py", line 12, in <module>
    from django.utils.text import normalize_newlines
  File "lib/python2.7/site-packages/django/utils/text.py", line 11, in <module>
    from django.utils.six.moves import html_entities
ImportError: cannot import name html_entities

这是我的示例脚本:

#!/usr/bin/env python

import os
import django
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from models import Project

if __name__ == '__main__':
    print "My first exe"
    p = Project.objects.all()
    for project in p:
        print project.name

setup.py 如下所示:

main_python_file = "script.py"

import sys

from cx_Freeze import setup, Executable

# html_entities is missing
base = 'Console'
buildOptions = dict(
    create_shared_zip = False,
    append_script_to_exe = False,
    packages = [],
    includes = ['django'],
    excludes = ['tkinter']
    )

setup(
        name = "my_exe",
        version = "0.1",
        description = "My first exe",
        options = dict(build_exe = buildOptions),
        executables = [Executable(main_python_file, base = base)])

项目表如下所示:

class Project(Model):
    """
    Project table
    """
    name = CharField(max_length=255)

有人遇到并解决了这个问题吗?

冻结输出显示它无法找出 django.utils.six.moves。它在缺少的模块中显示“六个”:

缺少模块:

? django.utils.six.moves imported from django.db.backends, django.db.models.base, django.db.models.sql.where, django.dispatch.dispatcher, django.forms.formsets, django.http.cookie, django.http.response, django.utils.crypto, django.utils.functional, django.utils.html_parser, django.utils.ipv6, django.utils.regex_helper, django.utils.text
? django.utils.six.moves.urllib.parse imported from django.core.files.storage, django.core.validators, django.forms.fields, django.forms.widgets, django.http.request, django.http.response, django.utils.encoding, django.utils.html, django.utils.http

【问题讨论】:

你能显示冻结的输出吗? django.utils.six 冻结好吗? 嗨,Thomas,我查看了 freeze 输出,发现 cx_freeze 无法冻结 django.utils.six 。它显示在缺少的模块部分。你知道我需要做什么来冻结 django.utils.six。 如果您将django 放入packages 而不是includes,cx_Freeze 将复制其所有子模块,这有望解决此问题。我有filed a bug。 感谢 Thomas 提交错误。将 django 放入包中并不能解决问题。我在运行脚本时看到相同的导入错误。 如果在包中包含htmlentitydefs 会怎样? 【参考方案1】:

如果您查看 django.utils.six 文件,您会发现以下代码:

...
MovedModule("http_cookiejar", "cookielib", "http.cookiejar"),
MovedModule("http_cookies", "Cookie", "http.cookies"),
MovedModule("html_entities", "htmlentitydefs", "html.entities"),
MovedModule("html_parser", "HTMLParser", "html.parser"),
...

django 动态地做的是凭空构建 django.utils.six.moves 模块。 MovedModule 的参数是 .所以python33(我使用的那个)中的解决方案是在我的可执行模块中导入html.entities。我试图将它添加到可执行文件的包含中,但这似乎不起作用。我假设您可以将 import htmlenditydefs 用于 python2 版本。

【讨论】:

以上是关于cx_freeze 无法捆绑 django.utils.six.moves的主要内容,如果未能解决你的问题,请参考以下文章

cx_freeze include_msvcr 不捆绑 windows VC2015 运行时

cx_Freeze - appdirs 包是必需的

Python 3.2:cx_freeze 编译我的程序,但处于调试模式

无法使用 cx_Freeze 编译

cx_Freeze- ImportError:无法导入名称设置

使用 cx_Freeze 时出现问题:“无法导入名称 'tf2'”