PyInstaller打包Django程序后runserver报错:找不到rest_framework模块
解决PyInstaller打包Django+DRF后runserver找不到模块的问题
问题重现
用PyInstaller 5.12打包Django 3.2.25应用后,执行runserver直接失效,添加--noreload参数后出现DRF模块缺失错误,具体报错:
C:\Users\GAMING\Documents\pythonexe1\project\dist>manage runserver --noreload Performing system checks... Traceback (most recent call last): File "rest_framework\settings.py", line 179, in import_from_string File "django\utils\module_loading.py", line 17, in import_string File "importlib\__init__.py", line 127, in import_module File "<frozen importlib._bootstrap>", line 1006, in _gcd_import File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked ModuleNotFoundError: No module named 'rest_framework.authentication' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "manage.py", line 19, in <module> File "manage.py", line 16, in main File "django\core\management\__init__.py", line 419, in execute_from_command_line File "django\core\management\__init__.py", line 413, in execute File "django\core\management\base.py", line 354, in run_from_argv File "django\core\management\commands\runserver.py", line 61, in execute File "django\core\management\base.py", line 398, in execute File "django\core\management\commands\runserver.py", line 96, in handle File "django\core\management\commands\runserver.py", line 105, in run File "django\core\management\commands\runserver.py", line 118, in inner_run File "django\core\management\base.py", line 423, in check File "django\core\checks\registry.py", line 76, in run_checks File "django\core\checks\urls.py", line 13, in check_url_config File "django\core\checks\urls.py", line 23, in check_resolver File "django\urls\resolvers.py", line 416, in check File "django\utils\functional.py", line 48, in __get__ File "django\urls\resolvers.py", line 602, in url_patterns File "django\utils\functional.py", line 48, in __get__ File "django\urls\resolvers.py", line 595, in urlconf_module File "importlib\__init__.py", line 127, in import_module File "<frozen importlib._bootstrap>", line 1006, in _gcd_import File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module File "project\urls.py", line 18, in <module> File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module File "mqtt_app\urls.py", line 2, in <module> File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module File "mqtt_app\views.py", line 5, in <module> File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module File "rest_framework\views.py", line 17, in <module> File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module File "rest_framework\schemas\__init__.py", line 33, in <module> File "rest_framework\settings.py", line 227, in __getattr__ File "rest_framework\settings.py", line 170, in perform_import File "rest_framework\settings.py", line 170, in <listcomp> File "rest_framework\settings.py", line 182, in import_from_string ImportError: Could not import 'rest_framework.authentication.SessionAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. ModuleNotFoundError: No module named 'rest_framework.authentication'. [41700] Failed to execute script 'manage' due to unhandled exception!
问题原因
PyInstaller依赖静态代码分析识别需要打包的模块,但Django REST Framework(DRF)大量使用动态导入(比如从字符串路径加载认证类、权限类),导致PyInstaller无法自动检测到这些隐式依赖的子模块。
解决方案
方法1:修改PyInstaller spec文件手动添加隐藏导入
生成spec文件:
pyi-makespec manage.py这会生成
manage.spec文件。编辑
manage.spec,找到hiddenimports参数,添加DRF缺失的模块:a = Analysis( ['manage.py'], pathex=[], binaries=[], datas=[], hiddenimports=[ 'rest_framework.authentication', 'rest_framework.schemas', # 可根据后续报错补充其他DRF子模块,比如rest_framework.permissions等 ], hookspath=[], hooksconfig={}, runtime_hooks=[], excludes=[], noarchive=False, )用spec文件重新打包:
pyinstaller manage.spec
方法2:自定义PyInstaller Hook文件
创建一个名为
hook-django_rest_framework.py的文件,内容如下:from PyInstaller.utils.hooks import collect_submodules hiddenimports = collect_submodules('rest_framework')collect_submodules会自动收集rest_framework下的所有子模块,避免遗漏。打包时指定hook文件目录(假设hook文件放在当前目录的
hooks文件夹下):pyinstaller --additional-hooks-dir=hooks manage.py
额外提示
- 打包Django应用时,可根据需求添加
--noconsole(隐藏命令行窗口)或--onefile(生成单个exe文件)参数,注意--onefile会增加启动时间。 - 如果自定义Django app中也存在动态导入的模块,同样需要在
hiddenimports中添加对应模块路径。
内容的提问来源于stack exchange,提问作者ahmed tahri
相关产品推荐
相关产品推荐

