From the course: Using Rust with Python

PyO3 installation

- [Instructor] Here we have Py03 user guide with a Py03 user guide. You can see that there's a Rust tool chain, a Python environment, and then a way to build. So first up here, you need to make sure you have Rust, you need to have Python, and you also need to have some sort of virtual environment. So let's go ahead and get that all set up here. So first up here, let's just make sure that I have Python. There we go, we see you have Python3, great. Also, let's make sure we have Rust and we can see that we can do dash dash version. If you go to the Rust up webpage, if you don't have Rust, you can actually install Rust. So all right, now we can go ahead and create a virtual environment. So we'll go ahead and say Python3 -m venv ~/.venv. So this is one of the little hacks I like to do is put the virtual environment in home directory. That way it's kind of easy to remember. In this case, I actually don't have it, so I'll have to install it, which is not a big deal. There we go, here we go. And then it's pretty easy to actually source this. I can just say source ~/.venv/bin/activate. And if you want to be really lazy, you could actually put that in your bash rc. So you always source this virtual environment anytime you code, which is can be a helpful thing to do depending on what it's you're doing. Now the other thing we're going to need to do here is we're going to need to do a pip install of the maturin. So we'll type in M-A-T-U-R-I-N and that will actually go ahead and put that package into the virtual environment. And then we'll also need to now create a new project. So let's go ahead and make a new project. We'll follow the tutorial here and say Py03 example. And this will create a directory right inside. And I'll go ahead and CD into there. Now once I'm inside of here, I can type in maturin init, we're actually going to do the Py03 here. We're going to use the default setup. Great, now we've got that set up. The next thing that we can do is actually build out something. So all we need to do is check out the source code right here. And we see that we've got a lib.rs. And what's kind of cool about this is you can see the structure of a basic project and then I have a a function here that goes inside. And then here's the Python module that's implemented in Rust. And you can see here that I've got this Python module that's integrated in Rust. So we've got basically everything all set up here to have a bridge from Python to Rust. And then if we go to the cargo file, you can see what it set up is it installs Py03 and it also allows the library to be set up here as well. Really the only other thing we need to do here is actually get this thing to run. And so in order to do that, we just go through here and we say maturin, and that's going to go ahead and compile the project. Perfect, and then we just type in Python and now we can just type in import example. Perfect, and then we can type in py03_example.sum_as_string and something together. There we go, we're able to actually use Python code from Rust, which gives us some great performance enhancement. So pretty straightforward actually to get started with the Py03 bridge here. And I'm going to go ahead and check this in and that will be the last thing that I need to do. So we'll go ahead and say git status and then say git add here, py03-example, and we'll go ahead and commit this adding example.

Contents