欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-01-07 15:12:04
...

把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://toolsforagile.com/blog/archives/51

上一篇:

下一篇: