Search code examples
pythonrustwindows-10rust-cargotoolchain

Rust and Python on W10: LNK1181


I'm trying to write a Rust module which can be called from Python. I'm following this page:

https://developers.redhat.com/blog/2017/11/16/speed-python-using-rust#edit_src_lib_rs

On cargo build --release I get this error:

= note: LINK : fatal error LNK1181: cannot open input file 'python39.lib'

This has come up before, and this seems the most recent and relevant answer.

I seem to have tried all the possible solutions in that answer, including locating and running vcvars64.bat, as detailed here. No joy.

I have MS Visual Studio (2019) installed, with the C++ and W10 SDK components.

Significantly, this error sometimes does not occur when I don't include the --release switch. Without it, the program (sometimes) compiles and runs OK. Given that one reason for wanting to make a Rust module is performance, however, I'd like to solve this problem. Also I have now found that this successul building of a "debug" build is a) intermittent and b) partial: when it fails, some of the desired output files are created but not others.

And...

According to the page at the first link, it seems that after build, I should then be looking for a file ending .so. In a W10 OS I'm looking for a .dll file.

If it builds completely successfully, under target\debug I have myrustlib.d, myrustlib.dll, myrustlib.dll.exp, myrustlib.dll.lib, myrustlib.pdb, pyext_myrustlib.pdb, pyext_myrustlib.d and pyext_myrustlib.exe. None of these, renamed and/or given an .dll extension as applicable, can be successfully imported as a Python module, at least as documented on that page.

I've also found this more recent page for doing the same thing. I get the same 1181 error.

python39.lib

I found out where this is located in my system, under ... Python\Python39\libs. I modified my PATH env var to specifically include this path (and rebooted). Still the same error.


Solution

  • Workaround:

    This page, using PyO3 instead of rust-cpython (actually a fork of the latter), appears to do things successfully, on my machine + OS at least.

    NB at the time of writing the second example appears to contain a typo: I believe it should be

    fn hashset(_: Python, m: &PyModule) -> PyResult<()> {
    

    rather than

    fn hash_set(_: Python, m: &PyModule) -> PyResult<()> {
    

    if you want it to compile OK (author has been notified). I have managed to create a module (hashset.cp39-win_amd64.pyd in a W10 OS) which successfully enables you to import this Rust construct into Python.