Django项目打包成exe
问题描述:
第一次打包Django项目,虽然知道最后会生成dist文件夹,但是在build文件夹里也找到了manage.exe,经过测试…直接删除了build.
按道理来说不出意外manage.py会绑定一个manage.spec,如果没有就去网上找。
manage.spec代码:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['manage.py'],
pathex=[],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='manage',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True,
disable_windowed_traceback=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='manage')
打包命令
pyinstaller -D manage.py #结果成功的日志提示 1296 INFO: checking EXE 1312 INFO: checking COLLECT 1312 INFO: Building COLLECT because COLLECT-00.toc is non existent 1312 INFO: Building COLLECT COLLECT-00.toc 20854 INFO: Building COLLECT COLLECT-00.toc completed successfully.
- 第一个问题:
File "djangoutilsautoreload.py", line 244, in get_child_arguments
raise RuntimeError('script %s does not exist.' % py_script)
RuntimeError: script runserver does not exist.
[10512] Failed to execute script 'manage' due to unhandled exception!
解决方案:运行时加–noreload 开关,即:manage.exe runserver 8000 --noreload
但是我运行了三次还是有问题,后面在报错中看到了下面一个问题
- 第二个问题:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Usersfh.log' The above exception was the direct cause of the following exception: Traceback (most recent call last):
解决方案:在manage.exe文件同目录下创建一个logs的文件夹,因为本人在settings.py中设置过日志文件。
- 第三个问题:
raise TemplateDoesNotExist(template_name, chain=chain) django.template.exceptions.TemplateDoesNotExist: lfh/index.html ERROR basehttp 161 "GET / HTTP/1.1" 500 86294 WARNING log 230 Not Found: /favicon.ico WARNING basehttp 161 "GET /favicon.ico HTTP/1.1" 404 7879
警告这里可以在同级目录下放一个favicon.ico,至于没有这个的话就用一张图片改成这样的名字和后缀
至于模板错误 ,现在还没解决,怕解决templates问题的时候这些报错信息被del掉。
TemplateDoesNotExist at / lfh/index.html Request Method: GET Request URL: http://localhost:8000/ Django Version: 3.2.8 Exception Type: TemplateDoesNotExist Exception Value: lfh/index.html Exception Location: djangotemplateloader.py, line 19, in get_template Python Executable: C:UsersASUSDesktopgraduationDesignCurriculumDesigndistmanagemanage.exe Python Version: 3.7.4 Python Path: ['C:\Users\ASUS\Desktop\graduationDesign\CurriculumDesign\dist\manage\base_library.zip', 'C:\Users\ASUS\Desktop\graduationDesign\CurriculumDesign\dist\manage\lib-dynload', 'C:\Users\ASUS\Desktop\graduationDesign\CurriculumDesign\dist\manage'] Server time: Mon, 29 Nov 2021 16:33:19 +0800 Template-loader postmortem Django tried loading these templates, in this order: Using engine django: django.template.loaders.filesystem.Loader: C:UsersASUSDesktopgraduationDesignCurriculumDesigndistmanagetemplateslfhindex.html (Source does not exist) django.template.loaders.app_directories.Loader: C:UsersASUSDesktopgraduationDesignCurriculumDesigndistmanagedjangocontribadmintemplateslfhindex.html (Source does not exist) django.template.loaders.app_directories.Loader: C:UsersASUSDesktopgraduationDesignCurriculumDesigndistmanagedjangocontribauthtemplateslfhindex.html (Source does not exist)
第三个问题的解决方法:
因为缺少templates的文件夹,就把templates的文件夹放过去,期间还有关于static、media的图片;路径报错,就一并把static、media的全部放过去了



