PyPy is an implementation of Python written in RPython (Reduced Python) language, and it is seen as a replacement for CPython.
PyPy claims that it is almost a drop-in replacement for CPython and can beat it on speed and memory usage. It supports libraries like Django, NumPy, Scikit-learn, and Twisted, and comes with the stackless mode, providing micro-threads for massive concurrency.
In this article, we’ll see how to install and use PyPy in our system.
Installation
PyPy comes with Python3 and Python2. Click here to download the pre-built binary that is compatible with your system.
After downloading PyPy, you’ll get a compressed folder in your system. Now extract the content of the PyPy’s build folder into the desired path, and that’s it.
But how to use it because we didn’t add PyPy’s path to system variables like we used to do in the case of CPython installation?
How to Use PyPy?
It is recommended to use PyPy using a virtual environment. Let’s see how we can use PyPy from a virtual environment.
We’ll use virtualenv package to create a virtual environment. It is compatible with both CPython and PyPy. You’ll need to install this package (pip install virtualenv
) to get started.
Open the command prompt or terminal and run the following command:
1 |
PS > virtualenv -p D:/SACHIN/pypy310/pypy.exe pypyenv |
Let’s understand what the above command does:
virtualenv
: A command to create a virtual environment usingvirtualenv
.-p
: Specifies the path to the Python interpreter that will be used in the virtual environment. It can also be written--python
.D:/SACHIN/pypy310/pypy.exe
: Path to PyPy interpreter.pypyenv
: Species the name of the virtual environment.
Run the command and PyPy will be ready to use.
You can check if you are using PyPy by running the following command:
1 2 3 4 5 6 |
PS > cd pypyenv PS > Scripts/activate (pypyenv) PS > cd .. (pypyenv) PS > python --version Python 3.10.14 (39dc8d3c85a7, Aug 27 2024, 14:33:33) [PyPy 7.3.17 with MSC v.1929 64 bit (AMD64)] |
You’ll notice that we used python
command to check the version of PyPy installed on the system because in the virtual environment, the python
command becomes a symlink (or equivalent) to the interpreter used when creating the virtual environment.
Installing Modules Using PyPy
The following command is used to install third-party modules using PyPy:
1 |
PS > pypy -m pip install module_name |
You can directly install modules without writing pypy -m
, if you are using PyPy as the main interpreter. If you are using PyPy within an isolated environment then activate the environment and use the above command to avoid any error.
Some third-party libraries such as Scipy create problems during the installation, so in that case, you can use conda with pypy3.9.
That’s all for now.
Keep Coding✌✌