打包历程
- 在与
manage.py
同目录下创建一个py文件,命名为:runserver.py
,内容如下:
from django.core.management import execute_from_command_line
if __name__ == "__main__":
args = ['manage.py', 'runserver', 'localhost:8001'] # 可以添加更多参数,例如端口号
execute_from_command_line(args)
- 然后运行
pyi-makespec.exe -D .\runserver.py
提示:
Wrote E:\Project\peopleinfo\personinfo\runserver.spec.
Now run pyinstaller.py to build the executable.
表示运行成功。然后执行
pyinstaller.exe .\runserver.spec --noconfirm
执行成功显示
266 INFO: PyInstaller: 6.3.0
266 INFO: Python: 3.11.4
277 INFO: Platform: Windows-10-10.0.22621-SP0
280 INFO: Extending PYTHONPATH with paths
['E:\\Project\\peopleinfo\\personinfo']
448 INFO: checking Analysis
885 INFO: checking PYZ
989 INFO: checking PKG
990 INFO: Bootloader C:\Python311\Lib\site-packages\PyInstaller\bootloader\Windows-64bit-intel\run.exe
991 INFO: checking EXE
1020 INFO: checking COLLECT
1058 INFO: Removing dir E:\Project\peopleinfo\personinfo\dist\runserver
2666 INFO: Building COLLECT COLLECT-00.toc
8505 INFO: Building COLLECT COLLECT-00.toc completed successfully.
会生成build
和dist
文件夹
3. 进入./dist/runserver
文件夹,运行命令:
./runserver.exe
报错
Traceback (most recent call last):
File "runserver.py", line 6, in <module>
File "django\core\management\__init__.py", line 442, in execute_from_command_line
File "django\core\management\__init__.py", line 436, in execute
File "django\core\management\base.py", line 412, in run_from_argv
File "django\core\management\commands\runserver.py", line 74, in execute
File "django\core\management\base.py", line 458, in execute
File "django\core\management\commands\runserver.py", line 81, in handle
File "django\conf\__init__.py", line 89, in __getattr__
File "django\conf\__init__.py", line 69, in _setup
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
[21692] Failed to execute script 'runserver' due to unhandled exception!
错误原因: Django 的设置没有正确配置。当尝试运行 Django 项目时,它需要知道从哪里加载设置。这通常是通过设置环境变量 DJANGO_SETTINGS_MODULE 来完成的,该环境变量应指向 Django 项目的设置模块。
解决办法: 在runserver.py里执行命令前,加入os.environ['DJANGO_SETTINGS_MODULE'] = 'personinfo.settings'
from django.core.management import execute_from_command_line
if __name__ == "__main__":
os.environ['DJANGO_SETTINGS_MODULE'] = 'personinfo.settings'
args = ['manage.py', 'runserver', 'localhost:8001'] # 可以添加更多参数,例如端口号
execute_from_command_line(args)
然后重新生成spec文件,执行pyinstaller的命令。接着运行./runserver.exe
,报错
Traceback (most recent call last):
File "runserver.py", line 6, in <module>
File "django\core\management\__init__.py", line 442, in execute_from_command_line
File "django\core\management\__init__.py", line 436, in execute
File "django\core\management\base.py", line 412, in run_from_argv
File "django\core\management\commands\runserver.py", line 74, in execute
File "django\core\management\base.py", line 458, in execute
File "django\core\management\commands\runserver.py", line 111, in handle
File "django\core\management\commands\runserver.py", line 118, in run
File "django\utils\autoreload.py", line 673, in run_with_reloader
File "PyInstaller\hooks\rthooks\pyi_rth_django.py", line 24, in _restart_with_reloader
File "django\utils\autoreload.py", line 272, in restart_with_reloader
File "django\utils\autoreload.py", line 229, in get_child_arguments
IndexError: list index out of range
[19296] Failed to execute script 'runserver' due to unhandled exception!
算了,心累了。。。。换种方法吧。
- 在与manage.py同目录下,创建一个文本文件,命名为:
runserver.bat
。 - 打开文件输入内容,不会打开的可以右键,选择用记事本打开:
python manage.py runserver localhost:8001
- 保存退出。
- 完成。想要运行项目时,双击这个文件就可以运行。
第二天,书接上回
上面那个错误是由于 Django 的自动重载机制与 PyInstaller 打包后的环境不兼容所导致的。Django 的 runserver 命令在开发模式下使用了自动重载功能,当检测到代码变化时会自动重启服务器。然而,在 PyInstaller 打包的可执行文件中,这种机制可能无法正常工作,因为它依赖于特定的文件系统和环境布局,这在打包后可能会改变。
**解决办法: ** 再加一个参数:'--noreload'
,变成
from django.core.management import execute_from_command_line
import os
if __name__ == "__main__":
os.environ['DJANGO_SETTINGS_MODULE'] = 'personinfo.settings'
args = ['manage.py', 'runserver', 'localhost:8001', '--noreload'] # 可以添加更多参数,例如端口号
execute_from_command_line(args)
然后重新生成spec文件,执行pyinstaller的命令。接着运行./runserver.exe
,报错
Traceback (most recent call last):
File "runserver.py", line 7, in <module>
File "django\core\management\__init__.py", line 442, in execute_from_command_line
File "django\core\management\__init__.py", line 416, in execute
File "django\__init__.py", line 24, in setup
File "django\apps\registry.py", line 91, in populate
File "django\apps\config.py", line 178, in create
File "importlib\__init__.py", line 126, in import_module
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1126, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1140, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'personbusiness'
[8784] Failed to execute script 'runserver' due to unhandled exception!
找不到相关模块的错误,解决办法是:在runserver.spec
里,找到
a = Analysis(
['runserver.py'],
pathex=[],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
noarchive=False,
)
目前,hiddenimports
是空的,将personbusiness
填入进去。之后会碰到很多找不到的模块,一一填入就可以了。下面是我最终填好的。
a = Analysis(
['runserver.py'],
pathex=[],
binaries=[],
datas=[],
hiddenimports=[
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'personbusiness',
'personbusiness.apps',
'personbusiness.urls'
],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
noarchive=False,
)
重新执行pyinstaller
,注意不要执行pyi-makespec.exe -D .\runserver.py
,否则会把你更新的内容给刷新掉。
pyinstaller.exe .\runserver.spec --noconfirm
运行./runserver.exe
就可以开启web服务了。
但是打开浏览器,输入web地址会发现,报错:找不到模板以及静态文件;或者界面显示不正常。这是因为没有指定模板文件和静态文件的位置。
解决办法: 在runserver.spec
里,找到a = Analysis()
,找到里面的datas
,加入指定模板文件和静态文件的位置。
a = Analysis(
['runserver.py'],
pathex=[],
binaries=[],
datas=[
('E:\\Project\\peopleinfo\\personinfo\\static', 'static'),
('E:\\Project\\peopleinfo\\personinfo\\templates','templates')
],
hiddenimports=[
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'personbusiness',
'personbusiness.apps',
'personbusiness.urls'
],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
noarchive=False,
)
重新执行pyinstaller
,结束。
将打包好的应用程序注册为windows服务
下载nssm工具,
- 注册为服务
nssm install MyService "C:\Path\to\your\application.exe"
- 开启服务
nssm start MyService
- 停止服务
nssm stop MyService