环境 Linux环境:CentOS 7.4 Python 3.8.6 PyInstaller 5.4.1 项目代码:Django 4.0 项目 存放路径:/
pip导出安装包 1 pip freeze > requirements.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 altgraph==0.17 .2 APScheduler==3.9 .1 asgiref==3.4 .1 backports.zoneinfo==0.2 .1 bcrypt==3.2 .0 certifi==2021.10 .8 cffi==1.15 .0 charset-normalizer ==2.0 .10 cPython==0.0 .6 cryptography==36.0 .1 Cython==0.29 .30 distlib==0.3 .6 Django==4.0 django-apscheduler ==0.6 .2 django-crontab ==0.7 .1 django-ranged-response ==0.2 .0 django-simple-captcha ==0.5 .17 django-sslserver ==0.22 filelock==3.8 .0 idna==3.3 lxml==4.7 .1 multidict==5.2 .0 mysqlclient==2.1 .0 numpy==1.21 .5 paramiko==2.9 .1 Pillow==9.2 .0 platformdirs==2.5 .2 pycparser==2.21 pyinstaller==5.4 .1 pyinstaller-hooks-contrib ==2022.10 pymongo==4.1 .1 PyMySQL==1.0 .2 PyMysqlDB==0.0 .2 PyNaCl==1.4 .0 pyOpenSSL==21.0 .0 PySocks==1.7 .1 pysqlite3==0.4 .6 python-libpcap ==0.4 .0 pytz==2021.3 pytz-deprecation-shim ==0.1 .0 .post0 pyvmomi==7.0 .3 PyYAML==6.0 requests==2.27 .1 sflow==1.8 .0.3 six==1.16 .0 snipy==0.0 .7 sqlparse==0.4 .2 suds-jurko ==0.6 tzdata==2022.2 tzlocal==4.2 urllib3==1.26 .8 uWSGI==2.0 .20 virtualenv==20.16 .5 wrapt==1.13 .3 yarl==1.7 .2
如果我们想自己安装,在cmd中输入一下命令:
1 2 3 pip install -r requirements.txt or pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
安装打包所需的Python环境 如果要通过共享库的方式安装 python, 可使用如下命令:
1 env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.8 .6
上述为了避免 打包时出现 “找到libpython3.7.so.1.0等库文件”
1 2 3 4 5 6 7 8 9 OSError: Python library not found: libpython3.7 .so.1.0 , libpython3.7 m.so.1.0 , libpython3.7 mu.so.1.0 This would mean your Python installation doesn't come with proper library files. This usually happens by missing development package, or unsuitable build parameters of Python installation. * On Debian/Ubuntu, you would need to install Python development packages * apt-get install python3-dev * apt-get install python-dev * If you' re building Python by yourself, please rebuild your Python with `--enable-shared ` (or, `--enable-framework ` on Darwin)
解决思路(一) 先检索以下系统中是否已有这几个文件:libpython3.7.so.1.0, libpython3.7m.so.1.0, libpython3.7mu.so.1.0
出现了错误:
1 2 find: paths must precede expression: libpython3.8.so Usage: find [-H] [-L] [-P] [-Olevel] [-D help |tree|search|stat |rates|opt|exec ] [path...] [expression]
然后就上网查了一下,结果搜索到一篇,大概是这样说的:多文件的查找的时候需要增加单引号,修改后:
1 find / -name 'libpython*'
解决思路(二) 重新编译python解释器,增加—enable-shared选项 要重装Python,进入到Python文件使用
1 ./configure --prefix =/usr/local/python3 --enable-shared --with-openssl =/usr/local/openssl
然后 make && make install就好了 现在再次打开python的时候,会报错,ImportError: libpython3.7m.so.1.0: cannot open shared object file: No such file or directory 需要依次执行echo "/usr/local/python3/lib/" >> /etc/ld.so.confldconfig
解决思路(三) 安装python3-devel-3.7的包。Debian/Ubuntu下包名为python3-dev,Centos下包名为python3-devel,注意选择3.7的版本。CentOS7的环境中基本上只能选择通过外网yum安装,因为CentOS7 Everything的ISO镜像源中提供的版本最高是3.6,而这个包的依赖层又非常多。
1 2 3 4 5 yum install python3-devel or yum install python* or pip3 install python3-dev
重新打包编译了python解释器,然后再执行以下, 将编译的依赖库拷贝到系统库中,
1 cp libpython3.so libpython3.8 .so.1.0 /usr/lib64/
项目打包 下载 Pyinstaller
制作项目的.spec文件 进入django项目所在路径,运行
1 pyi-makespec -D manage.py
以文本的方式打开.spec文件,spec文件格式如下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 block_cipher = None a = Analysis( ['manage.py' ], pathex=['/opt/cdpadmin' ], binaries=[], datas=[], hiddenimports=[ 'django.contrib.admin' , 'django.contrib.auth' , 'django.contrib.contenttypes' , 'django.contrib.sessions' , 'django.contrib.messages' , 'django.contrib.staticfiles' , 'cdpapp' , 'django_crontab' , 'django_apscheduler' , 'django.template.context_processors' , 'django.contrib.auth.context_processors' , 'django.contrib.messages.context_processors' , 'django.middleware.security' , 'django.contrib.sessions.middleware' , 'django.middleware.common' , 'django.middleware.csrf' , 'django.contrib.auth.middleware' , 'django.contrib.messages.middleware' , 'django.middleware' , 'django.db.backends' ], 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, argv_emulation=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' , )
一切准备好后,执行下面语句就OK
打包后在dist->manage文件中如下图所示 若需要导出mysql数据库信息
1 mysqldump -uroot -pgbnc2012 --all-databases > /root/hyb_cdp.sql
将django项目打包,执行以下命令行
1 vim /root/pyinstaller_hyb.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 rm /opt/cdpadmin/dist/manage -rf cp /root/manage.spec /opt/cdpadmin/manage.specpyinstaller manage.spec cp -dRp /opt/cdpadmin/cdpapp/* /opt/cdpadmin/dist/manage/cdpapp/rm /opt/cdpadmin/statics -rf python3 manage.py collectstatic cp -dRp /opt/cdpadmin/statics /opt/cdpadmin/dist/manage/cp -dRp /root/installpath/ /opt/cdpadmin/dist/manage/cd /opt/cdpadmin/dist/mv ./manage ./cdpadmintar -czvf hyb_cdp_v1.0 .tar.gz cdpadmin cp /opt/cdpadmin/dist/hyb_cdp_v1.0 .tar.gz /root/hyb_cdp_v1.0 .tar.gzrm -rf /opt/cdpadmin/dist/rm -rf /opt/cdpadmin/build/rm -rf /opt/cdpadmin/manage.spec
1 chmod +x /root/pyinstaller_hyb.sh
1 sh /root/pyinstaller_hyb.sh
运行下面的命令即可运行django项目
1 ./manage runserver 0.0 .0.0 :8080 --noreload
参考链接 1、Linux环境下使用Pyinstaller打包Django项目 2、使用pyinstaller打包django项目 3、Centos6、Centos7、Centos8关闭防火墙 4、pyinstaller 打包遇到问题:Python library not found: libpython3.9m.so.1.0, libpython3.9m.so, libpython3.9.so.1.0, libpython3.9.so, libpython3.9mu.so.1.0