#工作记录
本次记录是用python3.11部署和运行stable diffusion webui后怎么消除python版本不匹配的输出的方法,理论上python3.11在一些包的兼容性和可用性会更好一些。
我使用的.venv虚拟环境,是通过预备的anaconda python3.11虚拟环境新建而来的,也就是用"D:\ProgramData\anaconda3\envs\python311\python.exe"作为PyCharm的基础python版本新建的本地项目virtualenv虚拟环境。
题外话:就我个人而言,我还是比较推荐这种环境管理方式,尽量把python.exe相关虚拟环境留在项目本地目录,而不是conda。因为anaconda时间久了比较容易出现问题,当你有数量级的conda虚拟环境之后anaconda,一旦出现软件级的错误,会导致所有conda虚拟环境无法被项目软件识别和使用,并且anaconda的修复和conda虚拟环境的恢复太麻烦了,修复也不一定能回到原先的状态。
当我们在虚拟环境中使用python3.11版本时,启动stable diffusion webui 的安装会报出以下提醒:

但不得不说,python3.11版本在windows11系统下安装stable diffusion webui 的各种依赖和扩展的依赖时都比python3.10更占优势,适应性也表现的更好(这种体验仅限windows11系统)。
如果我们想消除这个烦人的启动提示:
1、可以在启动参数中添加它给出的参数跳过python版本的检查"--skip-python-version-check"
--skip-python-version-check
或者试试接下来要说的这个方法:
2、 修改modules/launch_utils.py 文件中,用于检查 Python 版本的代码段
在 modules/launch_utils.py 文件中,用于检查 Python 版本的代码段如下:
从modules/launch_utils.py 文件中的第34行开始是用于检查 Python 版本的代码段,但是,我们仅需要修改一处或几处就行了(修改1处就足够,其他修改只是文本性的逻辑性的修改)
def check_python_version():
is_windows = platform.system() == "Windows"
major = sys.version_info.major
minor = sys.version_info.minor
micro = sys.version_info.micro
if is_windows:
supported_minors = [10] # 修改这里以支持 Python 3.11
else:
supported_minors = [7, 8, 9, 10, 11] # 修改这里以支持 Python 3.11
if not (major == 3 and minor in supported_minors):
import modules.errors
modules.errors.print_error_explanation(f"""
INCOMPATIBLE PYTHON VERSION
This program is tested with 3.10.6 Python, but you have {major}.{minor}.{micro}.
If you encounter an error with "RuntimeError: Couldn't install torch." message,
or any other error regarding unsuccessful package (library) installation,
please downgrade (or upgrade) to the latest version of 3.10 Python
and delete current Python and "venv" folder in WebUI's directory.
You can download 3.10 Python from here: <url id="cvjbqgonnlrf6od5ec90" type="url" status="parsed" title="Python Release Python 3.10.6" wc="3729">https://www.python.org/downloads/release/python-3106/</url>
{"Alternatively, use a binary release of WebUI: <url id="cvjbqgonnlrf6od5ec9g" type="url" status="parsed" title="Release v1.0.0-pre · AUTOMATIC1111/stable-diffusion-webui" wc="614">https://github.com/AUTOMATIC1111/stable-diffusion-webui/releases/tag/v1.0.0-pre</url>" if is_windows else ""}
Use --skip-python-version-check to suppress this warning.
""")
代码解析
- sys.version_info :
• sys.version_info 是一个包含 Python 版本信息的元组,格式为 (major, minor, micro, releaselevel, serial) 。
• major 是主版本号(例如 3), minor 是次版本号(例如 10), micro 是修订号(例如 6)。
- supported_minors :
• 这是一个列表,定义了脚本支持的 Python 次版本号。
• 在 Windows 系统下, supported_minors 被设置为 [10] ,表示只支持 Python 3.10。
• 在非 Windows 系统下, supported_minors 被设置为 [7, 8, 9, 10, 11] ,表示支持 Python 3.7 到 3.11。
- 版本检查逻辑:
• if not (major == 3 and minor in supported_minors): 这行代码检查当前 Python 版本是否符合要求。
• 如果当前 Python 版本不符合要求,会调用 modules.errors.print_error_explanation 函数,打印一条错误信息,提示用户当前使用的 Python 版本不兼容,并建议用户切换到支持的版本。
修改为支持 Python 3.11如果你想让脚本支持 Python 3.11,可以将 supported_minors 列表中的值修改为包含 11:
if is_windows:
supported_minors = [11] # 修改为支持 Python 3.11
代码的其他几处本来也应一起修改,不然从注释和标注的逻辑上看有些说不通,但本着最少修改的原则,仅修改这一处就可以了,其他文本性不影响代码运行的标注不改也是可以的, 而且不影响stable diffusion webui的正常运行。
如果要完整的修改,该代码段如下:
# ...之前的代码...
def check_python_version():
is_windows = platform.system() == "Windows"
major = sys.version_info.major
minor = sys.version_info.minor
micro = sys.version_info.micro
if is_windows:
supported_minors = [11] # 修改为 [11],第 41 行
else:
supported_minors = [7, 8, 9, 10, 11] # 保持不变,第 43 行
if not (major == 3 and minor in supported_minors): # 第 45 行
import modules.errors
modules.errors.print_error_explanation(f"""
INCOMPATIBLE PYTHON VERSION
This program is tested with 3.11 Python, but you have {major}.{minor}.{micro}. # 修改为 3.11,第 51 行
If you encounter an error with "RuntimeError: Couldn't install torch." message,
or any other error regarding unsuccessful package (library) installation,
please downgrade (or upgrade) to the latest version of 3.11 Python # 修改为 3.11,第 54 行
and delete current Python and "venv" folder in WebUI's directory.
You can download 3.11 Python from here: https://www.python.org/downloads/ # 修改为 3.11 的下载链接,第 57 行
{"Alternatively, use a binary release of WebUI: <url id="cvjbqgonnlrf6od5ec9g" type="url" status="parsed" title="Release v1.0.0-pre · AUTOMATIC1111/stable-diffusion-webui" wc="614">https://github.com/AUTOMATIC1111/stable-diffusion-webui/releases/tag/v1.0.0-pre</url>" if is_windows else ""}
Use --skip-python-version-check to suppress this warning.
""") # 第 61 行
修改的具体位置(完整代码中的行号)
- 第 41 行:
• 原始代码: supported_minors = [10]
• 修改为: supported_minors = [11]
• 说明:将 Windows 系统支持的 Python 次版本从 3.10 修改为 3.11。
- 第 51 行:
• 原始代码: This program is tested with 3.10.6 Python, but you have {major}.{minor}.{micro}.
• 修改为: This program is tested with 3.11 Python, but you have {major}.{minor}.{micro}.
• 说明:将提示信息中的 3.10.6 修改为 3.11 。
- 第 54 行:
• 原始代码: please downgrade (or upgrade) to the latest version of 3.10 Python
• 修改为: please downgrade (or upgrade) to the latest version of 3.11 Python
• 说明:将提示信息中的 3.10 修改为 3.11 。
- 第 57 行:
• 原始代码: You can download 3.10 Python from here: <url id="cvjbqgonnlrf6od5ec90" type="url" status="parsed" title="Python Release Python 3.10.6" wc="3729">https://www.python.org/downloads/release/python-3106/\</url>
• 修改为: You can download 3.11 Python from here: https://www.python.org/downloads/
• 说明:将 Python 3.10 的下载链接修改为 Python 3.11 的通用下载链接。
最后还要提醒请注意,某些依赖包可能不兼容当前的 Python 版本,可能会导致运行时错误。即使目前还没发现问题,但即使发现了也不一定完全是python版本的问题。
运行后如下:

可以看到python版本不匹配的大段提醒已经没有了。


