[PYTHON] Build PyPy execution environment with Docker

Introduction

What is PyPy?

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.

Why Docker?

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.

environment

--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.

procedure

Now, I will explain the procedure for building the PyPy execution environment with Docker.

Directory structure

Configure the directory as follows:

│  docker-compose.yml
└─src
        main.py

File creation

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!)

Run

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!

Edit source

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

Library management

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]

End of container

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.

Supplement

Why docker-compose?

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.

PyPy speed verification

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.

[Summary] I think I'm familiar with about 50 programming languages ​​just by the number of Fibonacci numbers

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.

in conclusion

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

Build PyPy execution environment with Docker
Build PyPy and Python execution environment with Docker
Build Mysql + Python environment with docker
Build Jupyter Lab (Python) environment with Docker
[Linux] Build a jenkins environment with Docker
Build NGINX + NGINX Unit + MySQL environment with Docker
[Linux] Build a Docker environment with Amazon Linux 2
Build Django + NGINX + PostgreSQL development environment with Docker
Go (Echo) Go Modules × Build development environment with Docker
[Python] Build a Django development environment with Docker
Build a python execution environment with VS Code
Build python3 environment with ubuntu 16.04
Build python environment with direnv
Preparing the execution environment of PyTorch with Docker November 2019
Build a development environment with Poetry Django Docker Pycharm
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
Build python virtual environment with virtualenv
[Memo] Build a development environment for Django + Nuxt.js with Docker
Build a go environment using Docker
Build a deb file with Docker
Build Flask environment with Dockerfile + docker-compose.yml
[Django] Build a Django container (Docker) development environment quickly with PyCharm
Build IPython Notebook environment with boot2docker
Build GPU environment with GCP and kaggle official image (docker)
Create a Todo app with Django ① Build an environment with Docker
Rebuild Django's development environment with Docker! !! !! !!
Data science environment construction with Docker
Build a Python execution environment using GPU with GCP Compute engine
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
How to build Python and Jupyter execution environment with VS Code
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Easily build a development environment with Laragon
Build a Fast API environment with docker-compose
Get a local DynamoDB environment with Docker
Build the execution environment of Jupyter Lab
Create Python + uWSGI + Nginx environment with Docker
Build Python environment with Anaconda on Mac
Launch environment with LineBot + Heroku + Docker + Python
Build a python virtual environment with pyenv
Build a modern Python environment with Neovim
Build AI / machine learning environment with Python
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Easy construction]
Build a CentOS Linux 8 environment with Docker and start Apache HTTP Server
Build a LAMP environment on your local Docker
Build a C language development environment with a container
Hello World with gRPC / go in Docker environment
Note: Prepare the environment of CmdStanPy with docker
Build a WardPress environment on AWS with pulumi
Analytical environment construction with Docker (jupyter notebook + PostgreSQL)
Build the fastest Django development environment with docker-compose
Build python environment with pyenv on EC2 (ubuntu)
Create execution environment for each language with boot2docker
Simply build a Python 3 execution environment on Windows
Build Python development environment with Visual Studio Code
Build a python environment with ansible on centos6
Create a python3 build environment with Sublime Text3
Build a Django environment with Vagrant in 5 minutes
Create Nginx + uWSGI + Python (Django) environment with docker
[Memo] Build a virtual environment with Pyenv + anaconda
Build a virtual environment with pyenv and venv
Build a Django development environment with Doker Toolbox