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

使用PyInstaller打包Django项目为.exe时遇模块缺失问题求助

Fixing ModuleNotFoundError When Packaging Django Project to .exe with PyInstaller

Hey there, I’ve helped plenty of folks work through this exact PyInstaller + Django packaging headache. Let’s break down how to squash that ModuleNotFoundError and get your app running as a clickable .exe.

First, Fix Your Incomplete manage.py

Your provided manage.py snippet cuts off, which might be part of the issue. Make sure it’s fully implemented like this (double-check the settings module path matches your project):

# -*- coding: utf-8 -*-
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH? Did you forget to activate your virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

Step 1: Use a PyInstaller Spec File (Critical!)

PyInstaller’s auto-detection often misses Django’s dynamically imported apps and modules. The fix is to use a .spec file to explicitly tell it what to include.

  1. Generate a base spec file from your manage.py:

    pyinstaller --name django_dashboard_app manage.py
    

    This creates django_dashboard_app.spec in your root project folder.

  2. Open the spec file and update these key sections:

    • Hidden Imports: Add all your Django apps and missing modules to the hiddenimports list. For your project structure, this would look like:

      hiddenimports=[
          'proj.settings',
          'proj.urls',
          'proj.wsgi',
          'Dashboard',
          'Dashboard.urls'
      ],
      

      If you get a specific ModuleNotFoundError: No module named 'X', just add 'X' to this list.

    • Include Static Files: Bundle your static_cdn folder by adding a datas entry:

      datas=[
          ('static_cdn', 'static_cdn'),  # (source path in your project, path inside the exe bundle)
      ],
      

      If you have a templates folder, add it here too: ('templates', 'templates').

Step 2: Adjust Django Settings for Packaging

Tweak your proj/settings.py to play nice with the bundled exe:

  • Set ALLOWED_HOSTS = ['127.0.0.1', 'localhost'] (Django blocks browser access otherwise)
  • For static files, confirm STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn') and STATIC_URL = '/static/'—this ensures the exe can find your assets.
  • For testing, leave DEBUG = True, but for production builds, set DEBUG = False (run python manage.py collectstatic first to gather all static files into static_cdn).

Step 3: Build the Exe with the Modified Spec File

Run PyInstaller again using the spec file instead of manage.py:

pyinstaller django_dashboard_app.spec

Step 4: Test the Exe

  • Head to the dist folder PyInstaller creates, find your django_dashboard_app.exe, and double-click it.
  • A command prompt will open showing the Django dev server starting (look for Starting development server at http://127.0.0.1:8000/).
  • Open your browser and navigate to that URL—your Django dashboard should load up!

Troubleshooting Remaining Module Errors

If you still hit ModuleNotFoundError:

  • Add the exact missing module name from the error to hiddenimports in the spec file.
  • Ensure your virtual environment is activated when running PyInstaller (so it picks up all installed packages).
  • If using third-party Django apps (like DRF), add those to hiddenimports too (e.g., 'rest_framework').

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:28:26