inline_python: 在rust中编写python
准备
这个库需要nightly版本的rust,因此需要切换到nightly
rustup toolchain install nightly # 下载nightly版本
rustup override set nightly # 临时切换
rustup default nightly # 将默认版本设置为nightly
依赖
[dependencies]
inline-python = "0.10.0"
代码编写
use inline_python::python;
fn main() {
let who = "world";
let n = 5;
python! {
for i in range('n):
print(i, "Hello", 'who)
print("Goodbye")
}
}
conda
如果使用conda则可能遇到环境变量PYTHONHOME
缺失的问题,报错如下
Python path configuration:
PYTHONHOME = (not set)
PYTHONPATH = (not set)
program name = 'python'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = 'D:\\Rust\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\bin\\rustc.exe'
sys.base_prefix = 'D:\\anaconda3'
sys.base_exec_prefix = 'D:\\anaconda3'
sys.platlibdir = 'lib'
sys.executable = 'D:\\Rust\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\bin\\rustc.exe'
sys.prefix = 'D:\\anaconda3'
sys.exec_prefix = 'D:\\anaconda3'
sys.path = [
'D:\\anaconda3\\python39.zip',
'.\\DLLs',
'.\\lib',
'D:\\Rust\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\bin',
]
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 0x000045e8 (most recent call first):
<no Python frame>
error: could not compile `py-demo`
此时找到conda环境中python所在目录设置为PYTHONHOME
即可
参考
inline-python - crates.io: Rust Package Registry
Writing Python inside your Rust code — Part 1 - Mara's Blog (m-ou.se)
本文作者:七つ一旋桜
本文链接:https://www.cnblogs.com/poifa/p/16798920.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
标签:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步