我在Windows 7上安装了Python 2.6和Python 3并设置了环境变量:path = d:\python2.6.
当我python在cmd中运行时,它显示的是python版本是2.6,这是正确的!
但是当我在bat文件中编写脚本并运行它时,显示的python版本为3.1.
这有什么不对?
bat文件中的脚本代码:
import sys
print (sys.version)
Run Code Online (Sandbox Code Playgroud)
Mar*_*nen 52
您安装的最后一个在环境中注册的Python是默认的(我不记得安装程序中的确切措辞,但它是第一个选项).有许多设置,以确保它们全部注册,只需重新安装您想要的默认版本.
如果要安装其他版本但不希望将其作为默认版本,则只需在安装期间禁用注册选项.
编辑(2012年5月19日)
从Python 3.3开始,Python安装程序将安装Python Launcher for Windows.此程序(py.exe)与Python文件扩展名相关联,并查找"shebang"注释以指定要运行的python版本.这允许许多版本的Python共存,并允许Python脚本明确指定使用哪个版本(如果需要).如果未指定,则默认使用当前体系结构(x86或x64)的最新Python 2.X版本.可以通过py.ini文件或PY_PYTHON环境变量自定义此默认值.有关更多详细信息,请参阅文档.
只需确保Python 3.3是最后安装的用Windows注册的Python.如果以后安装了其他版本的Python,请确保不要在环境中注册它们,因此启动器将保持默认状态.
以下是如何从控制台检查启动器是否正确注册的方法:
C:\>assoc .py
.py=Python.File
C:\>ftype Python.File
Python.File="C:\Windows\py.exe" "%1" %*
Run Code Online (Sandbox Code Playgroud)
上面,.py文件与Python.File类型相关联.命令行Python.File是Python Launcher,它安装在Windows目录中,因为它始终位于PATH中.
要使关联工作,请从命令行运行脚本script.py,而不是"python script.py",否则python将运行而不是py. py.exe也可以使用开关运行以强制Python版本:
py -3 script.py # force latest Python 3.X version to be used.
Run Code Online (Sandbox Code Playgroud)
此外,添加.py;.pyw;.pyc;.pyo到PATHEXT环境变量,然后命令行可以script没有扩展名.
Vla*_*den 43
运行'py'命令会告诉你运行的版本.如果您当前运行3.x并且需要切换到2.x,则需要使用开关'-2'
py -2
Run Code Online (Sandbox Code Playgroud)
如果你需要从python 2.x切换到python 3.x,你将不得不使用'-3'开关
py -3
Run Code Online (Sandbox Code Playgroud)
如果您希望将Python 3.x作为默认版本,则需要创建环境变量"PY_PYTHON"并将其值设置为3.
Adi*_*nde 29
如果您同时安装了两个版本。
去 This PC -> Right-click -> Click on Properties -> Advanced System Settings.
您将看到System Properties。从这里导航到"Advanced"选项卡->单击Environment Variables。
你会看到上半部分为用户变量和下半部分的System variables。
选中,System Variables然后双击Path(以编辑路径)。
检查Python的路径(您希望运行该路径,即Python 2.x或3.x)并将其移至 “路径”列表的顶部。
重新启动命令提示符,现在当您检查Python版本时,它应该正确显示所需的版本。
Ehs*_*jad 14
;
; This is an example of how a Python Launcher .ini file is structured.
; If you want to use it, copy it to py.ini and make your changes there,
; after removing this header comment.
; This file will be removed on launcher uninstallation and overwritten
; when the launcher is installed or upgraded, so don't edit this file
; as your changes will be lost.
;
[defaults]
; Uncomment out the following line to have Python 3 be the default.
;python=3
[commands]
; Put in any customised commands you want here, in the format
; that's shown in the example line. You only need quotes around the
; executable if the path has spaces in it.
;
; You can then use e.g. #!myprog as your shebang line in scripts, and
; the launcher would invoke e.g.
;
; "c:\Program Files\MyCustom.exe" -a -b -c myscript.py
;
;myprog="c:\Program Files\MyCustom.exe" -a -b -c
Run Code Online (Sandbox Code Playgroud)
因此,在我的系统上,我在py.exe存在的地方创建了一个py.ini文件,c:\windows\其中包含以下内容:
[defaults]
python=3
Run Code Online (Sandbox Code Playgroud)
现在,当您双击.py文件时,它将由新的默认版本运行.现在我只#! python2在我的旧脚本上使用Shebang .
Ami*_*imi 14
如果您知道Environment variables并且系统变量称为path,请考虑任何较早出现的二进制文件的任何版本都将用作默认值。
看下图,我有 3 个不同的 python 版本,但 python3.8将用作默认值,因为它比其他两个版本早。(在提到图像的情况下,越早意味着越高!)
Cas*_*all 13
如果你是 Windows 用户并且你有 Python 3.3 或更高版本,你应该在你的机器上安装Python Launcher for Windows,这是用于启动所有 python 脚本的推荐方式(无论脚本需要什么 python 版本) )。
作为用户
从命令行运行脚本时始终键入py而不是python。
设置您的“打开方式...”资源管理器默认程序关联 C:\Windows\py.exe
设置命令行文件扩展名关联以使用适用于 Windows 的 Python 启动器(这将使键入成为py可选):
ftype Python.File="C:\windows\py.exe" "%L" %*
ftype Python.NoConFile="C:\Windows\pyw.exe" "%L" %*
通过设置PY_PYTHON环境变量来设置您的首选默认版本(例如PY_PYTHON=3.7)。您可以通过键入来查看默认的python 版本py。您还可以设置PY_PYTHON3或PY_PYTHON2指定默认的python 3 和python 2 版本(如果您有多个)。
如果需要运行特定版本的python,可以使用py -M.m(哪里M是major version,哪里是mminor version)。例如,py -3将运行任何已安装版本的 python 3。
列出已安装的 python 版本py -0。
作为编剧
在脚本顶部包含一个 shebang 行,指示所需的 Python 的主要版本号。如果脚本与任何其他次要版本不兼容,请包括次要版本号。例如:
#!/usr/bin/env python3
您也可以使用 shebang 行来表示虚拟环境(请参阅下面的 PEP 486)。
也可以看看
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\python.exe\default.py文件python.exe在我的Windows 11 操作系统上安装Python 版本2.7、3.7、3.9、 和 时,我在使用之前建议的解决方案时遇到了问题。然而,我发现了一种对我有用的简单方法。3.11
首先,让我们了解如何使用py命令设置默认的Python版本。运行py --help会提供提示,包括对py.ini位于%LOCALAPPDATA%\py.ini. 该文件允许我们指定默认的 Python 版本。
要设置特定的默认 Python 版本:
打开文本编辑器或创建一个%LOCALAPPDATA%\py.ini.
将以下内容添加到py.ini文件中:
[defaults]
python=3.7
Run Code Online (Sandbox Code Playgroud)
现在,当您py --version在控制台中运行时,它应该显示指定的默认 Python 版本。
请注意以下需要考虑的其他要点:
设置默认Python版本的目的:设置特定的默认Python版本有助于确保与某些依赖项的兼容性或保持项目之间的一致性。
位置%LOCALAPPDATA%:%LOCALAPPDATA%环境变量通常指的是C:\Users\<username>\AppData\Local。如果py.ini该位置不存在该文件,您可以手动创建它。
这对我有用:
去
Control Panel\System and Security\System
Run Code Online (Sandbox Code Playgroud)
选择
Advanced system settings from the left panel
from Advanced tab click on Environment Variables
Run Code Online (Sandbox Code Playgroud)
在系统变量部分搜索(如果不存在则创建)
PYTHONPATH
Run Code Online (Sandbox Code Playgroud)
并设置
C:\Python27\;C:\Python27\Scripts;
Run Code Online (Sandbox Code Playgroud)
或您想要的版本
您需要重新启动 CMD。
如果它仍然不起作用,您可能只想在 PATH 变量中保留您想要的版本。
如果要使用 python 3.6,则必须将 python3.6 移到列表顶部。
这同样适用于 python2.7 如果您想将 2.7 作为默认值,请确保将 python2.7 移动到列表的最顶部。
第1步
第2步
第 3 步
蟒蛇--版本
>>> Python 3.6
Run Code Online (Sandbox Code Playgroud)
pho*_*oji -4
尝试修改 Windows 注册表中的路径 (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment)。
注意:不要破坏注册表:)
| 归档时间: |
|
| 查看次数: |
196264 次 |
| 最近记录: |