使用pyinstaller打包django的方法實現(xiàn)
雖然django項目我們一般通過部署服務(wù)器進行發(fā)布,但是也有些情況,可能就是一個小小的數(shù)據(jù)管理應(yīng)用,也就內(nèi)部幾個人使用,想直接打包成一個應(yīng)用,在沒有任何python環(huán)境的普通的機器上就能運行,內(nèi)網(wǎng)能訪問就可以了。
pyinstaller 就能夠用來將python應(yīng)用打包成可執(zhí)行文件。
Step 1: 生成spec文件
pyi-makespec -D manage.py
執(zhí)行成功后,會顯示如下信息,表示可以去構(gòu)建可執(zhí)行文件了
now run pyinstaller.py to build the executable
在目錄下面會生成一個 manage.spec的文件,相當于一個構(gòu)建可執(zhí)行文件的配置文件。打開文件,可以看一下,主要有兩個地方需要配置:
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 構(gòu)建可執(zhí)行文件
pyinstaller manage.spec
待上述命令執(zhí)行完,在目錄下面會生成dist和build目錄,在dist/manage目錄下,有一個可執(zhí)行文件manage
cd dist/manage目錄下,命令行執(zhí)行manage文件
./manage runserver ip:port --noreload
--noreload參數(shù)如果不加,有可能會報錯: 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)
到此這篇關(guān)于使用pyinstaller打包django的方法實現(xiàn)的文章就介紹到這了,更多相關(guān)pyinstaller打包django內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。