2014-07-26 45 views
2

我試圖用cx_freeze捆綁django。通過我的setup.py,我可以將它捆綁在一起,但是當我調用生成的可執行文件時,我得到下面的導入錯誤。我嘗試了各種方法來解決這個問題,但無法解決。使用cx_freeze無法捆綁django.utils.six.moves

版本: 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 
+0

你能顯示凍結的輸出嗎? 'django.utils.six'凍結好嗎? –

+0

您好托馬斯,我看着凍結輸出,看到cx_freeze無法凍結django.utils.six。它顯示在缺少的模塊部分。你知道我需要做些什麼來凍結django.utils.six。 – Pritam

+0

如果將'django'放入'packages'而不是'includes',cx_Freeze將複製其所有子模塊,這將有望解決此問題。我[提交了一個錯誤](https://bitbucket.org/anthony_tuininga/cx_freeze/issue/99/module-finder-tries-to-find-faked-imports)。 –

回答

2

如果您在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。我試圖將它添加到包含可執行文件,但似乎沒有工作。我假設你可以使用python2版本的import htmlenditydefs。