pyinstaller打包django项目成exe文件

第一步  安装PyInstaller

pip install pyinstaller  

第二步 制作项目的.spec文件

进入到项目的所在的路径中,执行如下命令生成 .spec文件,文件位于当前路径下

pyi-makespec -D manage.py

manage.spec文件 示例

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(['manage.py'],
             pathex=['D:\\000001资料\\attendance'],
             binaries=[],
             datas=[],
             hiddenimports=[
              "Django","xlwt","pytz","wheel","att.apps"  # 刚生成的时候是空的 需要自己填写
              ],
             hookspath=[],
             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 )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='manage')

第三步 生产exe文件

  • 运行如下命令,生成打包的exe文件

    pyinstaller manage.spec
  • 打包程序的运行方式

    # 在manage.exe的位置打开cmd,输入如下命令,运行服务即可
    manage.exe runserver

      

遇见的问题

问题1:运行服务是会提示No module named XXX

这是因为Django有些module不会自动收集,需要手动添加
解决方法:在manage.spec文件中修改
hiddenimports=[]为
    
hiddenimports=["Django","xlwt","pytz","wheel","att.apps"],
提示缺少什么module就在此处添加什么。

  

问题2:打开网页出现TemplateDoesNotExist 错误

解决方法:根据错误的提示消息,把项目中的模板文件templates拷贝到相应的位置,刷新页面即可。

  

问题3:网页丢失CSS、JS

1、首先在项目中的settings文件中添加如下代码,其中static是项目中的静态文件位置,static_root是static下的一个空文件夹,然后执行python manage.py collectstatic命令将静态文件收录到static_root中

STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static_root')

2、然后在urls.py中添加如下代码:

from django.conf.urls import static
from project_1 import settings
 
urlpatterns += static.static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

  注意:我在操作了之后 还是没有css 我又进行了下面一步 就完美解决

datas=[(r'D:\000001资料\attendance\static\static_root',r'.\static\static_root')], # 元祖 元祖

  上面代码是出现在  manage.spec 文件中 是一个元祖哟

 

posted @ 2021-02-26 21:16  流年中渲染了微笑  阅读(554)  评论(0编辑  收藏  举报