Windows下安装Z3的Python3版
GitHub官方仓库地址:Z3Prover/z3: The Z3 Theorem Prover
pip 安装(不推荐,很慢)
z3的pip仓库网址:z3-solver · PyPI
先尝试使用pip安装:
pip install z3-solver
报错如下:
(base) C:\Users\peter>pip install z3-solver
Collecting z3-solver
Downloading https://files.pythonhosted.org/packages/83/7e/9e3b84ed773945cfa3c00e64c3ef49c492bf4b6f60cf6223b8756053e7f4/z3-solver-4.8.0.0.post1.tar.gz (4.1MB)
100% |████████████████████████████████| 4.1MB 2.8MB/s
Building wheels for collected packages: z3-solver
Running setup.py bdist_wheel for z3-solver ... error
Complete output from command C:\Users\peter\AppData\Local\Continuum\anaconda3\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\peter\\AppData\\Local\\Temp\\pip-install-rowyaqt4\\z3-solver\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d C:\Users\peter\AppData\Local\Temp\pip-wheel-p4zt1rre --python-tag cp37:
running bdist_wheel
running build
Configuring Z3
opt = -x, arg =
New component: 'util'
New component: 'polynomial'
...
Component api_dll
...
Generated 'src\api\python\z3\z3core.py'
...
Listing 'src\\api\\python\\z3'...
Compiling 'src\\api\\python\\z3\\z3consts.py'...
Compiling 'src\\api\\python\\z3\\z3core.py'...
Generated python bytecode
Copied 'z3consts.py'
Copied 'z3core.py'
Copied 'z3consts.cpython-37.pyc'
Copied 'z3core.cpython-37.pyc'
Testing OpenMP...
64-bit: True
OpenMP: False
Writing build\Makefile
Copied Z3Py example 'all_interval_series.py' to 'build\python'
Copied Z3Py example 'example.py' to 'build\python'
Copied Z3Py example 'parallel.py' to 'build\python'
Copied Z3Py example 'socrates.py' to 'build\python'
Copied Z3Py example 'visitor.py' to 'build\python'
Makefile was successfully generated.
compilation mode: Release
platform: x64
To build Z3, open a [Visual Studio x64 Command Prompt], then
type 'cd C:\Users\peter\AppData\Local\Temp\pip-install-rowyaqt4\z3-solver\core\build && nmake'
Remark: to open a Visual Studio Command Prompt, go to: "Start > All Programs > Visual Studio > Visual Studio Tools"
Building Z3
error: [WinError 2] 系统找不到指定的文件。
----------------------------------------
Failed building wheel for z3-solver
Running setup.py clean for z3-solver
Failed to build z3-solver
Installing collected packages: z3-solver
Running setup.py install for z3-solver ... error
Complete output from command C:\Users\peter\AppData\Local\Continuum\anaconda3\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\peter\\AppData\\Local\\Temp\\pip-install-rowyaqt4\\z3-solver\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\peter\AppData\Local\Temp\pip-record-4321_vci\install-record.txt --single-version-externally-managed --compile:
running install
running build
Configuring Z3
Building Z3
error: [WinError 2] 系统找不到指定的文件。
----------------------------------------
Command "C:\Users\peter\AppData\Local\Continuum\anaconda3\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\peter\\AppData\\Local\\Temp\\pip-install-rowyaqt4\\z3-solver\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\peter\AppData\Local\Temp\pip-record-4321_vci\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\peter\AppData\Local\Temp\pip-install-rowyaqt4\z3-solver\
报错中提示需要使用Visual Studio x64 Command Prompt,所以我们打开Visual Studio x64 Command Prompt进行安装:
输入命令:
python -m pip install z3-solver
然后VS就开始编译DLL文件了,因为是单线程编译,所以需要等待一段时间,编译完就可以使用了。
使用微软官方构建好的DLL(推荐,快速)
找到Z3的最新的release:Releases · Z3Prover/z3
目前的最新版是Z3-4.8.4。
下载win-x64.zip,然后解压。
注意:更换目录为你的目录
- 添加Z3\bin的路径到PATH
- 设置PYTHONPATH=Z3\bin\python
测试代码:
from z3 import *
x = Real('x')
y = Real('y')
s = Solver()
s.add(x + y > 5, x > 1, y > 1)
print(s.check())
print(s.model())
运行成功: