安装:
pip install pyinstaller
在urls.py
from SheepMasterOneToOne import settings
from django.conf.urls.static import static
urlpatterns = [
path("admin/", admin.site.urls),
path('generate_report/export/', ReportAdmin(models.Report, admin.site).generate_report, name='generate_report'),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
在settings.py
STATIC_URL = "static/"
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
收集静态文件
python manage.py collectstatic
在项目根目录下
# 二者选其一
pyinstaller --onefile manage.py
pyi-makespec -D manage.py
生成
如果报错就修改manage.spec
# -*- mode: python ; coding: utf-8 -*-
a = Analysis(
['manage.py'],
pathex=[],
binaries=[],
datas=[
# 自己的模版 打包后的存放路径
('templates/admin/*', 'templates/admin'),
# 收集的静态文件 -> 打包后的存放路径
('staticfiles','static'),
# 第三方app的静态文件
('/path/to/yourproject/venv/lib/python3.9/site-packages/simpleui/static','simpleui/static'),
# # 第三方app的模版文件
('/path/to/yourproject/venv/lib/python3.9/site-packages/simpleui/templates','simpleui/templates'),
],
# 注册的 app
hiddenimports=[
"simpleui",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"sheep.apps.SheepConfig",
'sheep.apps'
],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
noarchive=False,
optimize=0,
)
pyz = PYZ(a.pure)
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,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
coll = COLLECT(
exe,
a.binaries,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='manage',
)
修改以后 执行 pyinstaller manage.spec 重新打包
pyinstaller manage.spec
打包成功 最后运行
./dist/manage runserver 8080 --noreload
启动成功