For macOS, here is how you can setup RustPython as an interpreter for PyCharm and Visual Studio Code.
You will need:
Here are the commands to install them:
xcode-select --installcurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh(those might change over time)
First run:
cargo install --git https://github.com/RustPython/RustPython
If you want RustPython with ssl support, try:
cargo install --git https://github.com/RustPython/RustPython --features ssl
Go to Cargo’s bin directory with cd ~/.cargo/bin. Run ls, you should see the binary ~/.cargo/bin/rustpython.
If you type rustpython at the terminal, you should get the welcome message:

In PyCharm, you can add an interpreter by using:
The full docs are on this link. This blog post is for the two options: virtual environment and system interpreter.
Go to “Add Python Interpreter” -> System Interpreter -> Click on the browse icon.

Navigate to /Users/YOURUSERNAME/.cargo/bin - selecting rustpython as the interpreter gives this error:

There is an easy fix. Create a link/shortcut called python that points to rustpython.
cd ~/.cargo/bin
ln -s rustpython python
PyCharm is happy. Test out that things work by creating a python file in PyCharm and running
import sys
print(sys.executable)
In the screen above, if you try to use the virtual option, you get this error.

However, you can still manually create the virtual environment. First, create a directory, cd into into it and run:
~/.cargo/bin/rustpython -m venv env --without-pip
This would create a virtual environement that looks like this.

Go the directory, open PyCharm, now you can set the interpreter from the virtual environement.
Install the Python Extension by Micorsoft. Create your Python file and press Command + Shift + P, then search for “Python: Select Interpreter”, add a new interpreter by using “Enter Interpreter Path” then browse your filesystem, go to ~/.cargo/bin/rustpython
You can repeat the same steps for creating a virtual environement and using that with Visual Studio Code.
]]>7380 commits and 1940 pull requests later, pip and get-pip.py are working with RustPython 🎉 🎉. pip can install itself and setuptools!
This update is brought to you from @coolreader18’s terminal by asciinema 🍿.
(Update 2021-02-08): Everything has been merged to master!
$ cargo install --git https://github.com/RustPython/RustPython --features ssl # or however you build
$ curl https://bootstrap.pypa.io/get-pip.py -O
$ rustpython get-pip.py
...lots of work...
$ rustpython -m pip --version
pip 21.0 from /home/.../.local/lib/rustpython3.9/site-packages/pip (python 3.9)
There’s undoubtedly still a lot that doesn’t work, so if there’s something simple-ish that errors inside of pip or a setup script, feel free to open an issue for it (note that C extensions won’t be supported anytime soon, and a ctypes implementation is in progress).
(Previous instructions):
This update is so fresh, things are not merged into the master or release branches yet. For now, to test this out, you have to checkout the pip-merg branch, and make sure to build with the ssl feature:
$ cd RustPython
$ git fetch --all
$ git checkout pip-merg
$ cargo install --path . --features ssl
$ curl https://bootstrap.pypa.io/get-pip.py -O
$ rustpython get-pip.py
...lots of work...
$ rustpython -m pip --version
pip 21.0 from /home/.../.local/lib/rustpython3.9/site-packages/pip (python 3.9)
At the very end of 2019, we finally reached one of our short-term goals: CPython unittest support which makes finding CPython compatibility errors easier than ever.
This will probably be the major source of contributions for new contributors this year. Here is a simple guideline.
Let’s find an incompatibility issue and fix it.
Lib/test directory of the project. There are many test_ prefixed files like test_unicode.py.TODO: RUSTPYTHON in the files. There are tons of skipped, marked as an expected failure or commented out tests.skip, expectedFailure or comments.Here’s how you run a single unittest file:
$ RUSTPYTHONPATH=Lib cargo run --release Lib/test/test_unicode.py
Because CPython unittest doesn’t work perfectly in RustPython, we are adding test files one by one. Here’s how:
git clone https://github.com/python/cpython.git.Lib/testBecause RustPython is not perfect, “try to edit it until it runs” doesn’t mean to make it run 100% of the tests successfully. The common methods to make the test file pass are:
unittest can cause issues. Sometimes RustPython bugs cause issues too.SyntaxError, you’ll have to comment that part out.@unittest.skip('TODO: RUSTPYTHON') to skip it.@unittest.expectedFailure.We prefer the reversed order of above methods. The later, the more strict, so it’s easier to detect any progress or regression.
When we temporarily disable parts of unittest due to RustPython caveats, we mark them to make it easier to find (and re-enable!) them later. Please see the examples below or search for TODO: RUSTPYTHON in the Lib/test directory to check actual usage.
Comment out:
# TODO: RUSTPYTHON
#
# def ... # commented out tests
skip:
@unittest.skip("TODO: RUSTPYTHON")
def ... # skipped tests
expectedFailure:
# TODO: RUSTPYTHON
@unittest.expectedFailure
def ... # failed tests
For a general introduction to RustPython development, please visit the RustPython development guide
]]>This post goes over the RustPython parser. You can see the source code in the rustpython-parser crate.
When you write code in Python and run it, an interpreter, such as the RustPython interpreter, acts as the translator between you and your machine.
The interpreter has the job of turning your human code into bytecode that a Python virtual machine can run. Bytecode is an intermediate code between source code and machine code. This makes it portable across multiple hardware and operating systems. Bytecode “works” as long as you implement a virtual machine (vm) that can run it. There is a performance penalty for this flexibility. RustPython also has a vm that interprets the generated bytecode, other posts will go into the details of that vm but now let’s figure out how to turn code into bytecode.
Seeing is believing. To see what bytecode looks like, you can use a Python module called dis. “dis” is short of for _dis_assembler. You can write source code then see how its bytecode looks like. Here is an example:

Here are the main steps that RustPython currently goes through:
This list of steps introduces some new concepts like: tokens and abstract syntax trees. We’ll explain and demistify those.
The fastest way to understand what tokens are, is to see them. Conveniently, Python comes with a tokenizer. Here is what happens if I run the tokenizer on the function that I created above.
$ python -m tokenize file.py
file.py has the function that I used in the previous example.
def add(x,y):
return x+y
Tokenize output:

A picture IS worth a thousand words 😛 Those are the tokens. They are the basic “units” of the programming language. They are the keywords and operators that you typed. Even new lines and identation count.
If you want to sound fancy:
The code for the lexing stage lives in lex.rs of the parser crate.
If you want to dive into the details of lexical analysis, check out Python in a nutshell / Lexical structure
In the previous step, if you add random stuff to your function and tokenize it, it will work and still tokenize.

So don’t hate on the whole interpreter when you get error messages! or at least don’t hate on the tokenizer!
To determine if the tokens are valid syntax, first you need a definition of what a valid syntax is. Python has a defined “grammar” or set of rules. The official reference is on this link. There, you will find a machine readable file. You may read a book to know the rules of Python, but words are too “fluffy”, an algorithm that verifies if the rules are followed needs a very strict set of rules encoded in a file. This video explains the Python grammar and the file’s notation. As the presenter puts it, this is the spirit of the beast (Python) and it is only ~10KB 😭 (compare that to the size of the Python books you had to read!)
So, we have the rules or grammar of a programming language in a machine encoded format… now we need to write something that verifies that those rules were followed… This sounds like something that other people could use and like something that should exist as an open source project! 🤔
Sure enough, there is a whole Rust framework called LALRPOP. It takes the tokens generated by the lexer, verifies the syntax and turns the tokens into an AST (Abstract Syntax Tree). More information and a tutorial can be found in the LALRPOP book.
RustPython does one nice extra thing on top of LALRPOP. It masks the errors and provides you with safer, nicer errors. You can see the code for this in RustPython/parser/src/error.rs
Using RustPython to generate an AST
You can do:
use rustpython_parser::{parser, ast};
let python_source = "print('Hello world')";
let python_ast = parser::parse_expression(python_source).unwrap();
As a recap, when you write a line of Python code and “run it”, here is what the RustPython interpreter does:
INPUT: your code (in file.py or interactive shell)
⬇️ parse the line of source code into tokens
⬇️ determine if the tokens are valid syntax
⬇️ create an Abstract Syntax Tree (AST)
⬇️ compile the AST into bytecode
OUTPUT: bytecode (in __pycache__/file.pyc or in memory)
The compiler is located in the rustpython-compiler crate. Keep an eye on the blog for a future post about the details or the compiler. In the meantime, check out the parser source code in rustpython-parser.
]]>