使用pyinstaller的Django到桌面应用程序

时间:2013-08-28 17:36:41

标签: django pyinstaller

我正在尝试将django项目转换为桌面应用。 我已经下载了pyinstaller的开发者版本:github / pyinstaller / pyinstaller。

hookutils.py被修改如下所述:http://www.pyinstaller.org/ticket/754以便pyinstaller找到我的根目录: Django根目录c:\ Workspace \ mysite \ mysite

我正在尝试编译的项目是运行后获得的初始项目: django-admin.py startproject mysite ,因此尚未创建任何应用程序。

当遵循pyinstaller(http://www.pyinstaller.org/wiki/Recipe/DjangoApplication)中描述的步骤时,我在尝试运行服务器时遇到一些错误( 。\ dist \ mysite \ mysite.exe runserver localhost:8080):

c:\Workspace\compiled>.\dist\mysite\mysite.exe runserver
Traceback (most recent call last):
  File "<string>", line 11, in <module>
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.core.management", line 453, in execute_from_command_line
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.core.management", line 392, in execute
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.core.management", line 263, in fetch_command
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.core.management", line 109, in get_commands
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.conf", line 53, in __getattr__
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.conf", line 49, in _setup
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.conf", line 71, in _configure_logging
  File "C:\Workspace\pyinstaller-develop\PyInstaller\loader\pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.utils.log", line 6, in <module>
  File "C:\Workspace\pyinstaller-develop\PyInstaller\loader\pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.views.debug", line 11, in <module>
  File "C:\Workspace\pyinstaller-develop\PyInstaller\loader\pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.http", line 1, in <module>
  File "C:\Workspace\pyinstaller-develop\PyInstaller\loader\pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.http.cookie", line 5, in <module>
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.utils.six", line 84, in __get__
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.utils.six", line 103, in _resolve
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.utils.six", line 74, in _import_module
ImportError: No module named Cookie

如果我随后在manage.py中导入Cookie,则会出现另一个错误:

Unhandled exception in thread started by <bound method Command.inner_run of <django.core.management.commands.runserver.Command object at 0x02D86CF0>>
Traceback (most recent call last):
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.core.management.commands.runserver", line 92, in inner_run
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.core.management.base", line 280, in validate
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.core.management.validation", line 35, in get_validation_errors
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.db.models.loading", line 166, in get_app_errors
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.db.models.loading", line 75, in _populate
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.db.models.loading", line 96, in load_app
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.utils.importlib", line 35, in import_module
  File "C:\Workspace\pyinstaller-develop\PyInstaller\loader\pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.contrib.auth.models", line 18, in <module>
  File "C:\Workspace\pyinstaller-develop\PyInstaller\loader\pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.contrib.auth.hashers", line 8, in <module>
  File "C:\Workspace\pyinstaller-develop\PyInstaller\loader\pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.test", line 6, in <module>
  File "C:\Workspace\pyinstaller-develop\PyInstaller\loader\pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
  File "c:\Workspace\compiled\build\mysite\out00-PYZ.pyz\django.test.testcases", line 35, in <module>
ImportError: cannot import name _doctest

我试图导入doctest,但它没有帮助。

使用python 2.7.4和django 1.5.1

修改 因此,通过将django.test和HTMLParser添加到manage.py,我得到了它的工作:

import os
import sys
import Cookie

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "OTA.settings")

    from django.core.management import execute_from_command_line

    import django.test
    import HTMLParser
    execute_from_command_line(sys.argv)

1 个答案:

答案 0 :(得分:1)

Django导入本地doctest模块。也许试试import django.test

还有来自PyInstaller文档的关于包含其他模块的内容:

  
      
  • 您可以在PyInstaller命令行上提供其他文件。
  •   
  • 您可以在命令行上提供其他导入路径。
  •   
  • 您可以编辑PyInstaller第一次运行时写入的myscript.spec文件   它适合你的剧本。在spec文件中,您可以告诉PyInstaller有关脚本特有的代码和数据文件。
  •   
  • 您可以编写“钩子”文件,通知PyInstaller隐藏的导入。如果您“挂钩”其他用户也可能使用的包的导入,您可以将您的挂钩文件提供给PyInstaller。
  •   

如果PyInstaller无法满足您的需求,我也推荐使用py2exe。