虽然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内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!