LoginSignup
1
1

More than 1 year has passed since last update.

JuliaからPythonの関数を呼び出す(Docker)

Posted at

ソース

Julia

jsample.jl
ENV["PYTHON"] = "/usr/local/bin/python"
import Pkg
using Pkg
Pkg.add(["PyCall"])
Pkg.build(["PyCall"])
using PyCall

if PyVector(pyimport("sys")["path"])[1] != ""
    pushfirst!(PyVector(pyimport("sys")["path"]), "")
end

sample = pyimport("pysample")
total = sample.sum(10, 20)
println("total:", total)

Python

pysample.py
def sum(num1, num2):
    return num1 + num2

docker-compose.yml

version: '3'
services:
  julia-python:
    restart: always
    build: .
    container_name: 'julia-python'
    working_dir: '/root/'
    tty: true
    volumes:
      - ./src:/root/

Dockerfile

FROM python:3
USER root

RUN apt-get update
RUN apt-get -y install locales && \
    localedef -f UTF-8 -i ja_JP ja_JP.UTF-8
ENV LANG ja_JP.UTF-8
ENV LANGUAGE ja_JP:ja
ENV LC_ALL ja_JP.UTF-8
ENV TZ JST-9
ENV TERM xterm

RUN pip install --upgrade pip
RUN pip install --upgrade setuptools
RUN wget https://julialang-s3.julialang.org/bin/linux/x64/1.7/julia-1.7.2-linux-x86_64.tar.gz
RUN tar -zxvf julia-1.7.2-linux-x86_64.tar.gz
RUN mv julia-1.7.2 /opt/
RUN ln -s /opt/julia-1.7.2/bin/julia /usr/local/bin/julia

環境

Dockerが実行できる環境

実行

cd julia-python
docker compose up -d --build
docker compose exec julia-python bash
julia jsample.jl

結果

total:30

JuliaからPythonの関数を呼び出すことに成功。

juliaファイルとpythonファイルのディレクトリが違う場合

ディレクトリ構成例

-julia.jl
-test
  └─python.py

ディレクトリをインポートする箇所を

pushfirst!(PyVector(pyimport("sys")["path"]), "test")

に変更する。

GitHub

参考文献

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1