python 3.9起,官方不支持在windows7及以下版本中安装和运行,为了不重装操作系统,经过尝试,找到了如下解决办法。
1.下载非安装版本
Windows embeddable package (64-bit)
https://www.python.org/ftp/python/3.9.1/python-3.9.1-embed-amd64.zip
2.解压缩文件到D:\python39,进入此目录,执行python,提示缺少api-ms-win-core-path-l1-1-0.dll,从网上下载了一个,将6.2.9200.16384版本放到解压缩目录中。
输出
D:\python39>python
Python path configuration:
PYTHONHOME = (not set)
PYTHONPATH = (not set)
program name = 'python'
isolated = 1
environment = 1
user site = 1
import site = 0
sys._base_executable = '\u0158\x08'
sys.base_prefix = 'D:\\python39'
sys.base_exec_prefix = 'D:\\python39'
sys.platlibdir = 'lib'
sys.executable = '\u0158\x08'
sys.prefix = 'D:\\python39'
sys.exec_prefix = 'D:\\python39'
sys.path = [
'D:\\python39',
'D:\\python39',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
Current thread 0x0000058c (most recent call first):
<no Python frame>
3.下载源代码,
https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tar.xz
再下载visual studio 2017,自己编译出python的二进制文件。不幸,也是同样的错误。
4.下载python 3.8非安装版本
https://www.python.org/ftp/python/3.8.7/python-3.8.7-embed-amd64.zip
5.可执行文件替换
将python 3.8的可执行文件python.exe解压缩,复制到D:\python39,替换原有的同名文件。
运行python,提示缺少python38.dll文件,将python39.dll重命名为python38.dll,python39.zip改名为python38.zip。
再次执行,成功。
D:\python39>python
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
测试3.9版新功能。 https://docs.python.org/zh-cn/3.9/whatsnew/3.9.html
>>> x = {"key1": "value1 from x", "key2": "value2 from x"}
>>> y = {"key2": "value2 from y", "key3": "value3 from y"}
>>> x | y
{'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}
也成功了。
D:\python310>python
Python 3.10.0a4 (tags/v3.10.0a4:445f7f5, Jan 4 2021, 19:55:53) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.