PyTorch with rust-analyzer in VSCode


I use VSCode with Rust-Analyzer, for my work with rust. And I needed the libtorch package, but since my OS (Fedora) doesn’t provide one with its repositories. I’ve created a virtual python environment in my rust project folder (via: pipenv install torch==2.2.0).
Now, in order to activate that environment I need to run pipenv shell.
Then I can run the following: LIBTORCH_USE_PYTORCH=1 cargo run and it works just fine.
I can also run a command inside that virtual environment without entering it via: pipenv run the-command.

so, I’ve configured my vscode terminal to use that environment, via configuring the python.defaultInterpreterPath in vscode.

now my problem is, how to tell rust-analyzer to use that environment, and prefix all its commands with this env variable LIBTORCH_USE_PYTORCH=1 ?

I tried all its configurations, but no luck.


Never mind, I found a solution.
In case anyone faces the issue, here is how to solve it.

  1. cd to your project path. ex: cd ~/dev/proj1.
  2. enter the venv. ex: pipenv shell.
  3. open code. ex code ..

You’re welcome :wink: