You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

cargo check与rust-analyzer执行结果不一致问题求助

问题场景

终端中无论是否激活虚拟环境,所有构建命令均运行正常,但rust-analyzer持续报错,提示找不到Python 3.x解释器。

项目配置文件

Cargo.toml

[package]
name = "python_rust_demo"
version = "0.1.0"
edition = "2021"

[dependencies]
pyo3 = { version = ">=0", features = ["abi3-py312", "extension-module"] }
pyo3-polars = { version = ">=0", features = [
    "derive",
    "dtype-array",
    "dtype-struct",
] }
polars = { version = ">=0", features = [
    "performant",
    "lazy",
    "diff",
    "array_count",
    "abs",
    "cross_join",
    "rank",
    "ndarray",
    "log",
    "cum_agg",
    "round_series",
    "nightly",
    "dtype-array",
    "dtype-struct",
], default-features = false }
serde = { version = "*", features = ["derive"] }
ndarray = { version = "*" }
itertools = "*"
rand = "*"
rand_distr = "*"

pyproject.toml

[project]
name = "python-rust-demo"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
    "anytree",
    "pyvis",
    "pandas>=2",
    "numpy>=2",
    "statsmodels>=0",
    "matplotlib>=3",
    "seaborn>=0",
    "plotly>=5",
    "pyarrow>=18",
    "polars>=1",
    "scipy>=1",
    "xgboost>=2",
    "lightgbm>=4",
    "scikit-learn>=1",
    "numba>=0",
    "num2words>=0",
    "sqlalchemy>=2",
    "pygount",
]

[dependency-groups]
dev = ["ipykernel>=6", "pytest>=8", "ruff>=0", "maturin>=1"]

[build-system]
requires = ["maturin>=1,<2"]
build-backend = "maturin"

执行的命令

使用uv管理项目依赖:

uv venv
uv sync --all-extras

构建Python-Rust扩展:

maturin develop --uv --release

Rust模块代码(src/lib.rs)

use pyo3::prelude::*;

/// 将两个数的和格式化为字符串
#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
    Ok((a + b).to_string())
}

/// Rust实现的Python模块,此函数名必须与`Cargo.toml`中的`lib.name`一致,否则Python无法导入该模块
#[pymodule]
fn string_sum(m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
    Ok(())
}

rust-analyzer报错信息

部分包的构建脚本执行失败。

cargo check 无法启动:Cargo监视器运行失败,命令未生成有效元数据(退出码:ExitStatus(ExitStatus(101))):编译 pyo3-build-config v0.22.6 错误:执行 pyo3-build-config v0.22.6 的自定义构建命令失败。注意:要改进构建依赖的回溯信息,可设置环境变量CARGO_PROFILE_DEV_BUILD_OVERRIDE_DEBUG=true以启用调试信息生成。

原因:进程未正常退出:G:\tech_projects\python_rust_demo\target\debug\build\pyo3-build-config-c39c4a8839e7d80b\build-script-build(退出码:1)
--- 标准输出
cargo:rerun-if-env-changed=PYO3_CONFIG_FILE
cargo:rerun-if-env-changed=PYO3_NO_PYTHON
cargo:rerun-if-env-changed=PYO3_ENVIRONMENT_SIGNATURE
cargo:rerun-if-env-changed=PYO3_PYTHON
cargo:rerun-if-env-changed=VIRTUAL_ENV
cargo:rerun-if-env-changed=CONDA_PREFIX
cargo:rerun-if-env-changed=PATH

--- 标准错误
错误:未找到Python 3.x解释器

内容的提问来源于stack exchange,提问作者Kevin Li

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 22:35:01