使用Maturin开发Python-Rust扩展时遇LNK1181:无法打开python3.11.lib
问题描述
我按照Maturin用户指南的步骤操作,文件及内容和教程完全一致。但激活虚拟环境后执行maturin develop命令时,出现如下错误:
Found pyo3 bindings 🐍 Found CPython 3.11 at D:\Studia\Studia\Informatyka\7 semestr\ZPR\PYTHON_RUST\guessing-game\.venv\bin\python.exe 📡 Using build options features from pyproject.toml Compiling pyo3-build-config v0.21.2 Compiling pyo3-macros-backend v0.21.2 Compiling pyo3-ffi v0.21.2 Compiling pyo3 v0.21.2 Compiling pyo3-macros v0.21.2 Compiling guessing-game v0.1.0 (D:\Studia\Studia\Informatyka\7 semestr\ZPR\PYTHON_RUST\guessing-game) error: linking with `link.exe` failed: exit code: 1181 note: LINK : fatal error LNK1181: cannot open input file 'python3.11.lib' error: could not compile `guessing-game` (lib) due to 1 previous error 💥 maturin failed
我已经将以下路径添加到环境变量PATH中:
C:\Users\<username>\AppData\Local\Programs\Python\Python311\libs\ C:\Users\<username>\AppData\Local\Programs\Python\Python311\ C:\Users\<username>\AppData\Local\Programs\Python\Python311\Scripts\
且python3.lib确实存在于C:\Users\<username>\AppData\Local\Programs\Python\Python311\libs\目录下。
Cargo.toml 配置
[package] name = "guessing-game" version = "0.1.0" edition = "2021" [lib] name = "guessing_game" crate-type = ["cdylib"] [dependencies] rand = "0.8.4" [dependencies.pyo3] version = "0.21.2"
pyproject.toml 配置
[build-system] requires = ["maturin>=1.0,<2.0"] build-backend = "maturin" [tool.maturin] # "extension-module" tells pyo3 we want to build an extension module (skips linking against libpython.so) features = ["pyo3/extension-module"]
解决方法
复制并重命名Python库文件
链接器需要的是python3.11.lib,但你的Python目录里只有python3.lib。直接在C:\Users\<username>\AppData\Local\Programs\Python\Python311\libs\下复制python3.lib,重命名为python3.11.lib,然后重新执行maturin develop。修复Python安装
如果上述方法没用,可能是安装Python时没勾选调试相关组件。卸载Python后重新安装,确保勾选“Download debugging symbols”和“Download debug binaries”选项,让安装程序生成对应版本号的lib文件。显式设置链接器库路径
在执行maturin develop前,先在终端设置LIB环境变量:set LIB=C:\Users\<username>\AppData\Local\Programs\Python\Python311\libs;%LIB%然后在同一终端运行构建命令。
验证虚拟环境版本匹配
确认虚拟环境使用的Python 3.11和系统安装的Python版本完全一致,版本不匹配也会导致链接失败。
内容的提问来源于stack exchange,提问作者wojtek5739g
相关产品推荐
相关产品推荐

