Windows 11下Django安装后命令无法识别的问题求助
问题概述
正在学习StackSkills平台的《Django 2 & Python | The Ultimate Web Development Bootcamp》课程,作为Django新手,按要求安装指定版本Django 2.0.2(命令pip3 install django==2.0.2)后,执行django-admin命令时提示未识别。卸载后安装最新版本5.0.6(命令pip install Django),问题依旧。尝试python -m Django startproject first_project等命令也失败,使用Windows 11系统(Dell Inspiron 16 Plus),讲师用MacOS未提供Windows专属命令,联系讲师未获回复。
命令提示符操作记录
C:\Users\ngjph>pip install django Requirement already satisfied: django in c:\users\ngjph\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (5.0.6) Requirement already satisfied: asgiref<4,>=3.7.0 in c:\users\ngjph\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from django) (3.8.1) Requirement already satisfied: sqlparse>=0.3.1 in c:\users\ngjph\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from django) (0.5.0) Requirement already satisfied: tzdata in c:\users\ngjph\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from django) (2024.1)
C:\Users\ngjph>python -m django --version C:\Users\ngjph\AppData\Local\Programs\Python\Python311\python.exe: No module named django
C:\Users\ngjph>python -m django C:\Users\ngjph\AppData\Local\Programs\Python\Python311\python.exe: No module named django
C:\Users\ngjph>django-admin 'django-admin' is not recognized as an internal or external command, operable program or batch file.
管理员PowerShell操作记录
PS C:\Users\ngjph> pip install django Requirement already satisfied: django in c:\users\ngjph\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (5.0.6) Requirement already satisfied: asgiref<4,>=3.7.0 in c:\users\ngjph\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from django) (3.8.1) Requirement already satisfied: sqlparse>=0.3.1 in c:\users\ngjph\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from django) (0.5.0) Requirement already satisfied: tzdata in c:\users\ngjph\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from django) (2024.1)
PS C:\Users\ngjph> django-admin django-admin: The term 'django-admin' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
PS C:\Users\ngjph> python Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import django Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'django'
PS C:\Users\ngjph> django --version django: The term 'django' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
PS C:\Users\ngjph> python -m django --version C:\Users\ngjph\AppData\Local\Programs\Python\Python311\python.exe: No module named django
已安装包列表(pip list)
absl-py 2.1.0 anaconda 0.0.1.1 asgiref 3.8.1 astunparse 1.6.3 beautifulsoup4 4.12.3 bs4 0.0.2 certifi 2024.2.2 charset-normalizer 3.3.2 click 8.1.7 colorama 0.4.6 contourpy 1.2.1 cycler 0.12.1 Django 5.0.6 flatbuffers 24.3.25 fonttools 4.51.0 gast 0.5.4 google-pasta 0.2.0 grpcio 1.62.2 h5py 3.11.0 idna 3.7 joblib 1.4.0 keras 3.3.2 kiwisolver 1.4.5 libclang 18.1.1 lxml 5.2.2 Markdown 3.6 markdown-it-py 3.0.0 MarkupSafe 2.1.5 matplotlib 3.8.4 mdurl 0.1.2 ml-dtypes 0.3.2 mpmath 1.3.0 namex 0.0.8 nltk 3.8.1 numpy 1.26.4 opt-einsum 3.3.0 optree 0.11.0 packaging 24.0 pandas 2.2.2 pillow 10.3.0 pip 24.0 protobuf 4.25.3 pyfiglet 1.0.2 Pygments 2.17.2 pyparsing 3.1.2 Python 3.11.4 python-dateutil 2.9.0.post0 pytz 2024.1 regex 2024.5.10 requests 2.31.0 rich 13.7.1 scikit-learn 1.4.2 scipy 1.13.0 six 1.16.0 soupsieve 2.5 sqlparse 0.5.0 style 1.1.0 tensorboard 2.16.2 tensorboard-data-server 0.7.2 tensorflow 2.16.1 tensorflow-intel 2.16.1 tensorflow-io-gcs-filesystem 0.31.0 termcolor 2.4.0 Theano 1.0.5 threadpoolctl 3.4.0 tqdm 4.66.4 typing_extensions 4.11.0 tzdata 2024.1 update 0.0.1 urllib3 2.2.1 Werkzeug 3.0.2 wheel 0.43.0 wrapt 1.16.0
问题原因分析
从操作记录可看出核心矛盾:pip显示Django安装在c:\users\ngjph\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages,但系统调用的Python路径是C:\Users\ngjph\AppData\Local\Programs\Python\Python311\python.exe——两个路径不属于同一个Python环境。这是Windows上常见的Python环境冲突问题,大概率是同时安装了Microsoft Store版本的Python(路径带pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0)和官方独立版本的Python(路径在Programs\Python),pip绑定了Store版本,而终端调用的是独立版本,导致两边环境不互通。
解决步骤
1. 确认当前Python与pip的对应关系
在终端执行以下命令,查看输出路径是否一致:
python --version pip --version
若Python路径和pip指向的包路径不匹配,即可确认环境冲突。
2. 统一使用同一个Python环境
选项A:使用Microsoft Store版本的Python
在终端输入py -3.11进入Store版本的Python交互环境,尝试import django,若成功,后续所有命令用py -3.11替代python:
- 安装指定版本Django:
py -3.11 -m pip install django==2.0.2 - 创建项目:
py -3.11 -m django startproject first_project
选项B:使用官方独立版本的Python
找到独立版本Python的安装路径(如C:\Users\ngjph\AppData\Local\Programs\Python\Python311),将该路径下的Scripts文件夹添加到系统环境变量PATH:
- 右键“此电脑”→“属性”→“高级系统设置”→“环境变量”
- 在“系统变量”中找到
Path,点击“编辑” - 添加路径:
C:\Users\ngjph\AppData\Local\Programs\Python\Python311和C:\Users\ngjph\AppData\Local\Programs\Python\Python311\Scripts - 重启终端,执行
python --version确认路径为独立版本,再重新安装Django:python -m pip install django==2.0.2
3. 验证解决效果
执行以下命令,若能正常输出版本号则问题解决:
python -m django --version
内容的提问来源于stack exchange,提问作者Meganekogekkilove

