创建Django项目虚拟环境时遇‘py’未识别错误求助
py命令仍无法识别的解决方案 错误信息
py : The term 'py' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
- py --version
~~- CategoryInfo : ObjectNotFound: (py:String) [], CommandNotFoundException
- FullyQualifiedErrorId : CommandNotFoundException
解决步骤
确认Python Launcher组件已安装
py是Python Launcher的专属命令,并非Python主程序自带。如果安装Python时未勾选Install launcher for all users选项,会导致该命令缺失。重新运行Python安装包,选择Modify,确保勾选此组件并完成安装。手动添加Python Launcher路径到环境变量
Python Launcher的py.exe通常位于C:\Windows\目录下。检查系统环境变量Path是否包含该路径,若未添加,手动将C:\Windows\加入后保存。直接使用Python命令创建虚拟环境
如果python或python3命令能正常运行(执行python --version验证),可绕过py命令,直接用以下命令创建Django虚拟环境:python -m venv myenv(将
myenv替换为你的虚拟环境名称)重启终端或系统
环境变量修改后,PowerShell可能无法即时加载新配置。重启终端,或执行refreshenv命令(需提前安装Chocolatey)刷新环境变量;若仍无效,尝试重启系统。
内容的提问来源于stack exchange,提问作者Sule Isaac

