新闻动态

使用pyinstaller打包django的方法实现

发布日期:2021-12-29 12:28 | 文章来源:站长之家

虽然django项目我们一般通过部署服务器进行发布,但是也有些情况,可能就是一个小小的数据管理应用,也就内部几个人使用,想直接打包成一个应用,在没有任何python环境的普通的机器上就能运行,内网能访问就可以了。
pyinstaller 就能够用来将python应用打包成可执行文件。

Step 1: 生成spec文件

pyi-makespec -D manage.py 

执行成功后,会显示如下信息,表示可以去构建可执行文件了

now run pyinstaller.py to build the executable

在目录下面会生成一个 manage.spec的文件,相当于一个构建可执行文件的配置文件。打开文件,可以看一下,主要有两个地方需要配置:

1.datas=[] 该配置用于配置static文件和templates文件
hiddenimports=[] 把settings里的install_apps 拷贝过来

 datas=[('/Users/huanghuan/Documents/python学习/django/loftyha/static','./static')],
 hiddenimports=[ 'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'shift',],

Step 2: 使用pyinstaller 构建可执行文件

pyinstaller manage.spec 

待上述命令执行完,在目录下面会生成dist和build目录,在dist/manage目录下,有一个可执行文件manage
cd dist/manage目录下,命令行执行manage文件

./manage runserver ip:port --noreload

--noreload参数如果不加,有可能会报错: RuntimeError('Script %s does not exist.' % py_script)

Traceback (most recent call last):
File "manage.py", line 23, in <module>
File "manage.py", line 19, in main
File "django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "django/core/management/commands/runserver.py", line 61, in execute
super().execute(*args, **options)
File "django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "django/core/management/commands/runserver.py", line 96, in handle
self.run(**options)
File "django/core/management/commands/runserver.py", line 103, in run
autoreload.run_with_reloader(self.inner_run, **options)
File "django/utils/autoreload.py", line 640, in run_with_reloader
exit_code = restart_with_reloader()
File "PyInstaller/hooks/rthooks/pyi_rth_django.py", line 72, in _restart_with_reloader
File "django/utils/autoreload.py", line 257, in restart_with_reloader
args = get_child_arguments()
File "django/utils/autoreload.py", line 244, in get_child_arguments
raise RuntimeError('Script %s does not exist.' % py_script)

到此这篇关于使用pyinstaller打包django的方法实现的文章就介绍到这了,更多相关pyinstaller打包django内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。

相关文章

实时开通

自选配置、实时开通

免备案

全球线路精选!

全天候客户服务

7x24全年不间断在线

专属顾问服务

1对1客户咨询顾问

在线
客服

在线客服:7*24小时在线

客服
热线

400-630-3752
7*24小时客服服务热线

关注
微信

关注官方微信
顶部