打包django项目 报错:ModuleNotFoundError: No module named ‘django‘

我用pyinstaller打包django项目,打包成功后,运行项目:

.\runserver.exe

报错:

Traceback (most recent call last):
  File "runserver.py", line 1, in <module>
ModuleNotFoundError: No module named 'django'
[19204] Failed to execute script 'runserver' due to unhandled exception!

在网上没有找到原因,于是去看打包命令

154 INFO: PyInstaller: 5.11.0
154 INFO: Python: 3.9.6
166 INFO: Platform: Windows-10-10.0.22621-SP0
167 INFO: UPX is not available.
169 INFO: Extending PYTHONPATH with paths
['E:\\Project\\peopleinfo\\personinfo']
pygame 2.4.0 (SDL 2.26.4, Python 3.9.6)
Hello from the pygame community. https://www.pygame.org/contribute.html
681 INFO: Appending 'datas' from .spec
792 INFO: checking Analysis
804 INFO: Building because hiddenimports changed
804 INFO: Initializing module dependency graph...
806 INFO: Caching module graph hooks...
823 INFO: Analyzing base_library.zip ...
2197 INFO: Loading module hook 'hook-encodings.py' from 'c:\\python39\\lib\\site-packages\\PyInstaller\\hooks'...
3255 INFO: Loading module hook 'hook-pickle.py' from 'c:\\python39\\lib\\site-packages\\PyInstaller\\hooks'...
3878 INFO: Loading module hook 'hook-heapq.py' from 'c:\\python39\\lib\\site-packages\\PyInstaller\\hooks'...
4338 INFO: Caching module dependency graph...
4438 INFO: running Analysis Analysis-00.toc
4454 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\python39\python.exe
4624 INFO: Analyzing E:\Project\peopleinfo\personinfo\runserver.py
4625 INFO: Analyzing hidden import 'django.contrib.staticfiles'
4626 ERROR: Hidden import 'django.contrib.staticfiles' not found
4626 INFO: Analyzing hidden import 'django.contrib.admin'
4626 ERROR: Hidden import 'django.contrib.admin' not found
4626 INFO: Analyzing hidden import 'django.contrib.auth'
4626 ERROR: Hidden import 'django.contrib.auth' not found
4626 INFO: Analyzing hidden import 'django.contrib.contenttypes'
4626 ERROR: Hidden import 'django.contrib.contenttypes' not found
4626 INFO: Analyzing hidden import 'django.contrib.sessions'
4626 ERROR: Hidden import 'django.contrib.sessions' not found
4626 INFO: Analyzing hidden import 'django.contrib.messages'
4626 ERROR: Hidden import 'django.contrib.messages' not found
4626 INFO: Analyzing hidden import 'django.contrib.staticfiles'
4627 ERROR: Hidden import 'django.contrib.staticfiles' not found
4627 INFO: Analyzing hidden import 'personbusiness.apps.PersonBusinessConfig'
4657 ERROR: Hidden import 'personbusiness.apps.PersonBusinessConfig' not found
4657 INFO: Processing module hooks...
4667 INFO: Looking for ctypes DLLs
4669 INFO: Analyzing run-time hooks ...
4710 INFO: Looking for dynamic libraries
141 INFO: Extra DLL search directories (AddDllDirectory): []
141 INFO: Extra DLL search directories (PATH): ['C:\\Python311\\Scripts\\', 'C:\\Python311\\', 'C:\\Python39\\Scripts\\', 'C:\\Python39\\', 'C:\\Windows\\system32', 'C:\\Windows', 'C:\\Windows\\System32\\Wbem', 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\', 'C:\\Windows\\System32\\OpenSSH\\', 'C:\\Program Files\\Intel\\WiFi\\bin\\', 'C:\\Program Files\\Common Files\\Intel\\WirelessCommon\\', 'C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR', '%JAVA_HOME%\\bin', 'F:\\Java\\bin', 'C:\\WINDOWS\\system32', 'C:\\WINDOWS', 'C:\\WINDOWS\\System32\\Wbem', 'C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\', 'C:\\WINDOWS\\System32\\OpenSSH\\', 'C:\\ProgramData\\chocolatey\\bin', 'D:\\MinGW\\bin', 'C:\\Users\\qdlxg\\pip\\pip.ini', 'D:\\NodeJS\\node_global', 'C:\\Users\\qdlxg\\AppData\\Roaming\\Python\\Python311\\Scripts', 'D:\\sqlite', 'D:\\NodeJS\\', 'C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common', 'C:\\Users\\qdlxg\\AppData\\Local\\Microsoft\\WindowsApps', 'F:\\Latex\\texlive\\2019\\bin\\win32', 'F:\\Java\\bin', 'D:\\pycharm\\PyCharm 2023.3.2\\bin', 'C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin', 'D:\\MinGW\\bin', 'C:\\Windows\\System32', 'D:\\VSCode\\bin', 'D:\\NodeJS\\node_global', 'D:\\sqlite', 'C:\\Users\\qdlxg\\AppData\\Roaming\\npm']
5417 INFO: Looking for eggs
5428 INFO: Using Python library c:\python39\python39.dll
5429 INFO: Found binding redirects:
[]
5430 INFO: Warnings written to E:\Project\peopleinfo\personinfo\build\runserver\warn-runserver.txt
5447 INFO: Graph cross-reference written to E:\Project\peopleinfo\personinfo\build\runserver\xref-runserver.html
5479 INFO: checking PYZ
5488 INFO: checking PKG
5489 INFO: Bootloader c:\python39\lib\site-packages\PyInstaller\bootloader\Windows-64bit-intel\run.exe
5489 INFO: checking EXE
5502 INFO: checking COLLECT
WARNING: The output directory "E:\Project\peopleinfo\personinfo\dist\runserver" and ALL ITS CONTENTS will be REMOVED! Continue? (y/N)y
On your own risk, you can use the option `--noconfirm` to get rid of this question.
14441 INFO: Removing dir E:\Project\peopleinfo\personinfo\dist\runserver
14710 INFO: Building COLLECT COLLECT-00.toc
16360 INFO: Building COLLECT COLLECT-00.toc completed successfully.

发发现第二行显示:

154 INFO: Python: 3.9.6

使用的是python3.9.6,可是我明明默认的python版本是3.11。

于是怒删3.9.6,删除之后,再次执行打包程序命令:

pyinstaller.exe .\runserver.spec

出现报错:

Fatal error in launcher: Unable to create process using '"c:\python39\python.exe"  "C:\Python39\Scripts\pyinstaller.exe" .\runserver.spec': ???????????

分析一下,删除python39后,找不到"C:\Python39\Scripts\pyinstaller.exe"程序了,说明使用的pyinstaller一直用的py39,而我安装的django是在py311下的,所有会报没有这个模块的错误。

接下来的做法:

  1. 从环境变量中把py39的环境变量删除
  2. 卸载之后会有残留,需要把对应的python39文件夹删除
  3. 然后看一下 python311/scripts 文件夹下有没有pyinstaller,如果没有,执行
pip install pyinstaller

进行安装。看到 python311/scripts 有了pyinstaller。
再次执行打包命令,就会发现,使用的python版本变成3.11了。

282 INFO: PyInstaller: 6.3.0
282 INFO: Python: 3.11.4
294 INFO: Platform: Windows-10-10.0.22621-SP0
299 INFO: Extending PYTHONPATH with paths
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值