One of the Python implementations. You can run Python modules normally. It runs much faster than running in plain Python. (A quick speed comparison at the end of this article) On the other hand, there seems to be a limit to the libraries that can be used. I don't know exactly, but the image is that you should think that third-party libraries can hardly be used (numpy seems to be usable as described later). You can use the standard library. Therefore, it is useful when you want to run a simple Python script that requires a standard library quickly.
I want to create a virtual environment because I don't want to pollute the environment of the local machine, but I couldn't find much information on how to create a virtual environment with PyPy. I've been studying Docker recently, and I think the behavior of the Python environment on Windows is suspicious, so I decided to create an environment with Docker.
--Windows 10 Home (version 20H2 build 19042.685) --Environment where Docker can be used with WSL2
Recently, Docker has started to work on WSL2 even on Windows 10 Home, making it very easy to use. For details on how to install WSL2, refer to Microsoft Official Page, for example. After that, you can specify to use WSL2 in the Docker Desktop settings.
You should be able to do it on your Mac in much the same way.
Now, I will explain the procedure for building the PyPy execution environment with Docker.
Configure the directory as follows:
│ docker-compose.yml
└─src
main.py
Create the docker-compose.yml
file as follows.
docker-compose.yml
version: '3'
services:
pypy:
image: pypy:3
volumes:
- ./src/:/src
working_dir: /src
tty: true
The service name is `pypy`
, but anything is fine. You will enter the container with this name later, so I think you should name the project so that it is easy for you to understand.
I brought PyPy image from Docker Hub with image
.
The src directory of the host PC is mounted on the src directory of the container with volumes
. The edits on the host PC will now be reflected in the container.
You can enter it later when you enter the container by specifying the src directory with working_dir
.
By setting tty
to true, you can enter the container by keeping it running after launching the container.
main.py just does Hello world like this:
#### **`main.py`**
```py
print("Hello, world!)
Open a command prompt in the directory containing docker-compose.yml
and start the container with the following command.
>docker-compose up -d
The first time will take some time.
By adding the option -d
, it will be executed in the background, and subsequent command operations will be possible.
Once the container is up, enter it with the following command.
>docker-compose exec pypy bash
Here, the argument `` pypy``` is the service name given in the docker-compose.yml file. The argument
bash` is required to operate in bash after entering the container. Or rather, an error will occur without this argument.
Now that we're in the src directory inside the container, we have main.py
directly underneath.
It can be executed with the pypy command.
# pypy main.py
Hello, world!
Since the src directory is mounted, the editing result on the host PC is also reflected in the container. For example, if you edit main.py on the host PC and execute it again in the container as shown below, you can see that it is reflected.
main.py
print("Hello, world!hogehoge)
# pypy main.py
Hello, world!hogehoge
The library can be installed with pip. For example, you can install numpy in a container with the following command.
# pip install numpy
When I write a script that executes numpy as shown below and try it, it is certainly executed.
np_test.py
import numpy as np
print(np.array([1, 2, 3]))
# pypy np_test.py
[1 2 3]
When you are done using it, use the exit command to exit the container.
# exit
You can leave the container up, but I think it's a good idea to drop it when you're done using it.
>docker-compose down
To use it, do docker-compose up -d
again. From the second time onward, the image remains on the host PC, so it will start up quickly.
I wanted to launch a container in one shot with the docker-compose up
command.
Of course, you can create a Dockerfile, build it, and then docker run, but then I thought it would be troublesome because I had to add various arguments to the command.
The Docker Hub PyPy Image Page (https://hub.docker.com/_/pypy) explains how to do this.
By the way, let's compare the speeds of Python and PyPy. With reference to the following article, I wrote a script to find the 39th Fibonacci number and compared the speed.
fib.py
import time
def fib(n):
if n < 2 :
return n
else:
return fib(n-2) + fib(n-1)
#Measure the time to find the 39th Fibonacci number
s = time.time()
print(fib(39))
e = time.time()
print("time: " + str(e - s) + "sec")
The result is as follows.
--PyPy: 0.958sec (version 7.3.3) --Python: 28.868sec (version 3.7.3)
PyPy is overwhelmingly fast. Original article compares the speeds of various languages, so for your reference.
I think PyPy is fast and convenient, but there is not much information available. I hope it helps someone. I created a Unexplored Village Search Tool and used PyPy for data preprocessing of this, but I was impressed that the processing that would not end forever in plain Python ended immediately. did. Please play with this tool if you like (promotion).
(Addition) As a sequel (?), "Building PyPy and Python execution environment with Docker has been released. This time, only PyPy execution environment was dealt with, but in this article, PyPy and plain Explains the environment where Python can be used together.
Recommended Posts