最近在研究Windows系统(Windows XP/Windows 7/Windows 10)的GUI自动化方案,整理一下相关的知识。

系统兼容性

1
As specified in PEP 11, a Python release only supports a Windows platform while Microsoft considers the platform under extended support. This means that Python 3.11 supports Windows 8.1 and newer. If you require Windows 7 support, please install Python 3.8.

上面的文字引用自Using Python on Windows

Python for Windows Downloads页面也可以看出各个Windows系统可以安装的Python版本。

Windows 10 +

到目前为止,Windows 10/Windows 11仍然是微软在维护的版本,因此最新的Python版本都是支持的。根据需要选择即可。

Windows 7

要支持Windows 7, 必须安装Python 3.8或之前的版本。

1
According to the release calendar specified in PEP 569, Python 3.8 is now in security fixes only stage of its life cycle: 3.8 branch only accepts security fixes and releases of those are made irregularly in source-only form until October 2024. Python 3.8 isn't receiving regular bugfixes anymore, and binary installers are no longer provided for it. Python 3.8.10 was the last full bugfix release of Python 3.8 with binary installers.

目前官方提供的二进制安装包最新只到3.8.10。可在官网或者我的网盘中获取。

Windows XP

要支持Windows XP, 必须要安装 Python 3.4或之前的版本。

目前官方提供的二进制安装包只到3.4.4。可在官网或者我的网盘中获取。

命令行安装

1
2
$ python-3.4.3.msi /quiet /norestart
$ python-3.8.10-amd64.exe /quiet /norestart

Python 3.4默认安装的位置是%SystemDrive%\Python34。

Python3.8默认安装的位置是%LocalAppData%\Programs\Python\Python38。

NSIS脚本安装

1
Exec '"$INSTDIR\python-3.8.10-amd64.exe" /quiet /norestart'

virtualenv

venv

python3.6及更高版本中内置了venv,功能与virtualenv类似。无需额外安装。

1
$ python3 -m venv .venv

virtualenv

在有网环境中,使用pip安装

1
2
$ python -m pip install --user virtualenv
$ python -m virtualenv --help

创建

1
$ virtualenv .venv

删除

1
$ rm -rf .venv

启用

1
$ .venv\Scripts\activate.bat

禁用

1
$ .venv\Scripts\deactivate.bat

在无网环境中,使用zipapps。你可以在官网或者我的网盘获取virtualenv.pyz。

创建

1
$ python virtualenv.pyz .venv

无网环境中安装包

使用pip

  1. 在相同环境(Windows, Python)的有网机器上执行download

    1
    $ pip download -r requirements.txt -d wheelfiles --trusted-host mirrors.aliyun.com

    如果使用了阿里云的镜像,要加上–trusted-host mirrors.aliyun.com。

    如果是较老的pip版本,需要用pip install –download替代pip download。

  2. 在无网机器上执行

    1
    $ pip install -r requirements.txt --no-index --find-links path/to/wheels

使用virtualenv

  1. 确保有网机器和无网机器的Python版本、操作系统、architecture一致。
  2. 拷贝整个虚拟环境目录到目标机