---
title: "把django应用 打包 发布成 可独立运行的桌面程序"
author: "hqman"
date: "2011-10-26T23:20:07Z"
tags: []
categories: ["开源软件"]
slug: "django"
draft: false
lastmod: "2011-10-26T23:21:59Z"
id: 1217856
url: "https://hqman.iteye.com/blog/1217856"
---

把django应用 打包 发布成 可独立运行的桌面程序

 

需求：在pc上运行django+sqlite应用，无须安装django python 数据库 web服务器 。。。  

 

传统django 应用运行在 apache or nginx fastcgi，偽了方便我们用cherrypy来作WEB服务器。

 

cx\_freeze 跨平台的python 打包工具

 

步骤： 

 

1:下载安装 cx\_freeze cherrypy

 

2 依赖关系 提前导入 项目依赖的包

 

 

import django.contrib.auth  
import django.contrib.contenttypes  
import django.contrib.sessions  
import django.contrib.sites  
import django.contrib.admin

#these pertain to your application  
import subway.models  
import subway.views  
import urls  
import manage  
import settings

import django.db.models.sql.compiler  
from django.contrib.auth.backends import *  
from django.conf.urls.defaults import *  
#these are django imports  
import django.template.loaders.filesystem  
import django.template.loaders.app\_directories  
import django.middleware.common  
import django.contrib.sessions.middleware  
import django.contrib.auth.middleware  
import django.middleware.doc

import django.contrib.messages  
import django.contrib.staticfiles

import django.contrib.messages.middleware  
import django.contrib.sessions.backends.db  
import django.contrib.messages.storage.user\_messages

import django.db.backends.sqlite3.base  
import django.db.backends.sqlite3.introspection  
import django.db.backends.sqlite3.creation  
import django.db.backends.sqlite3.client

import django.contrib.auth.context\_processors  
from django.core.context\_processors import *

import django.contrib.messages.context\_processors

import django.contrib.auth.models  
import django.contrib.contenttypes.models  
import django.contrib.sessions.models  
import django.contrib.sites.models  
import django.contrib.messages.models  
import django.contrib.staticfiles.models  
import django.contrib.admin.models

import django.template.defaulttags  
import django.template.defaultfilters  
import django.template.loader\_tags

#dont need to import these pkgs  
#need to know how to exclude them  
import email.mime.audio  
import email.mime.base  
import email.mime.image  
import email.mime.message  
import email.mime.multipart  
import email.mime.nonmultipart  
import email.mime.text  
import email.charset  
import email.encoders  
import email.errors  
import email.feedparser  
import email.generator  
import email.header  
import email.iterators  
import email.message  
import email.parser  
import email.utils  
import email.base64mime  
import email.quoprimime  
import django.core.cache.backends.locmem  
import django.templatetags.i18n  
import django.templatetags.future  
import django.views.i18n

import django.core.context\_processors

import django.template.defaulttags  
import django.template.defaultfilters  
import django.template.loader\_tags

from django.conf.urls.defaults import *  
import django.contrib.admin.views.main  
import django.core.context\_processors  
import django.contrib.auth.views  
import django.contrib.auth.backends  
import django.views.static

import django.contrib.admin.templatetags.log

import django.contrib.admin.templatetags.adminmedia

import django.conf.urls.shortcut  
import django.views.defaults

#let us hook up cherrypy  
#is it possible to hook up the dev server itself?

import cherrypy  
from cherrypy import wsgiserver  
from django.core.handlers.wsgi import WSGIHandler  
from django.core.servers.basehttp import AdminMediaHandler  
from django.conf import settings  
from django.utils import translation

 

3： 编写setup文件

 

from cx\_Freeze import setup, Executable  
import cx\_imports  
setup(  
name = "ehome",  
version = "1.0",  
description = "the typical 'Hello, world!' script",  
options = dict(build\_exe = {'include\_files': \['templates','media']}),#拷贝 admin的模板 资源文件  
executables = \[Executable("ehome.py")])

 

4： 替换 urls.py中 admin.autodiscover() 成  

 

import django.contrib.auth.admin  
#import django.contrib.contenttypes.admin

import django.contrib.sites.admin

 否则 后台出现 无权限访问的提示

 

 

5： 启动服务

 

 

  

import cherrypy  
from cherrypy import wsgiserver  
from django.core.handlers.wsgi import WSGIHandler  
from django.core.servers.basehttp import AdminMediaHandler  
from django.conf import settings  
from django.utils import translation  
if \_\_name\__ == "\_\_main\_\_":

\# django.core.management.base forces the locale to en-us. We should  
\# set it up correctly for the first request (particularly important  
\# in the "--noreload" case).  
translation.activate(settings.LANGUAGE\_CODE)

try:  
path = './media'  
handler = AdminMediaHandler(WSGIHandler(), path)  
cherrypy.config.update({  
'server.socket\_host': 'localhost',  
'server.socket\_port': 8000,  
'log.error\_file': 'site.log',  
'engine.autoreload\_on': True  
})

cherrypy.config.update({'environment': 'production',  
'log.error\_file': 'site.log',  
'log.screen': False})

cherrypy.tree.graft(handler, '/')

cherrypy.engine.start()  
cherrypy.engine.block()  
finally:

cherrypy.engine.exit()

 

 

 

 

总结： 中间碰到好多问题

1：cx\_freeze 文档很少  

2：django 用了很多动态加载module的地方 都要手工 一一 import进来

 

 

参考：

 

[http://www.jjude.com/2008/04/make-your-django-application-as-a-stand-alone-desktop-application/](http://www.jjude.com/2008/04/make-your-django-application-as-a-stand-alone-desktop-application/)

[http://toolsforagile.com/blog/archives/51](http://toolsforagile.com/blog/archives/51)