You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PyInstaller打包Django应用报错:找不到'django.contrib.admin.apps'模块

Troubleshooting ModuleNotFoundError: No module named 'django.contrib.admin.apps' with PyInstaller 3.2 & Django 1.10.1

Hey there! Let's break down why this error is popping up and how to fix it—since you already tried older solutions without luck, let's focus on the specific quirks of your PyInstaller/Django version combo.

Why This Happens

PyInstaller 3.2 has limited automatic detection for Django's optional modules like django.contrib.admin. Even though your app runs fine locally, PyInstaller fails to bundle these admin-related modules because they aren't explicitly imported in your code (Django loads them dynamically via INSTALLED_APPS). The switch from ImportError to ModuleNotFoundError is just Python's updated error naming in newer versions—same root problem: missing modules in the bundled app.

Step-by-Step Fixes

1. Manually Add Hidden Imports via a Spec File

PyInstaller uses spec files to customize bundling. Here's how to update it:

  • First, generate a spec file if you haven't already:
    pyi-makespec path/to/your/manage.py
    
  • Open the generated manage.spec file in a text editor. Look for the hiddenimports list (it might be empty initially). Add the missing admin modules and its dependencies:
    hiddenimports=[
        'django.contrib.admin',
        'django.contrib.admin.apps',
        'django.contrib.admin.views.decorators',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
    ]
    
    These are core dependencies the admin module relies on—adding them ensures PyInstaller bundles them into your app.
  • Rebuild your app using the updated spec file:
    pyinstaller manage.spec
    

2. Verify Your Django Settings

Double-check that django.contrib.admin is properly listed in your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    # ... your custom apps
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

If it's missing, add it first—PyInstaller can't bundle a module Django isn't even configured to use.

3. Bundle from the Correct Directory

Make sure you run PyInstaller directly from your Django project's root directory (the folder containing manage.py). This helps PyInstaller correctly resolve relative paths and Django's module structure, avoiding missing module issues.

4. Confirm Virtual Environment Activation

If you're using a virtual environment, double-check that it's activated before running PyInstaller. Sometimes PyInstaller picks up system-wide Python packages instead of your venv's, leading to mismatched or missing modules.

Why Older Solutions Might Have Failed

Older fixes for ImportError often focused on adding manual imports in your code, but PyInstaller 3.2 doesn't reliably pick up these implicit imports for Django's dynamically loaded modules. Using the spec file's hiddenimports is a more direct way to force PyInstaller to include the exact modules you need.

内容的提问来源于stack exchange,提问作者cfleming93

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:03:51