2

I need to integrate two files, one in python the other in julia. More precisely, I need to call a julia function, snapshot_sphere(b,h,data,m,r,d), from a python file. Moreover,I need to provide the parameters via the command line. I had difficulties in using PyJulia. For the moment an error message is persisting:

ModuleNotFoundError: No module named '_ctypes'

I found some dependencies related to libffi-dev. Installing it did not change anything. What should I do to call the julia function from the python file ?

Here is the code of the python file:

import argparse
from julia import Main  
from PIL import Image, ImageShow
parser = argparse.ArgumentParser(description="Projection of the picture")
parser.add_argument(
    "-f", 
    "--filename", 
    type=str, 
    action="store", 
    dest="fname",
    help="Name of the picture"
)
parser.add_argument(
    "-d", 
    "--density", 
    type=int, 
    action="store", 
    dest="density",
    help="Number samples per Pixel"
)
parser.add_argument(
    "-r", 
    "--radius", 
    type=float, 
    action="store", 
    dest="radius",
    help="Radius of the sphere"
)
parser.add_argument(
    "-x", 
    type=float, 
    action="store", 
    dest="x",
    help="X coordinate of the center"
)
parser.add_argument(
    "-y", 
    type=float, 
    action="store", 
    dest="y",
    help="Y coordinate of the center"
)
parser.add_argument(
    "-z",
    type=float, 
    action="store", 
    dest="z",
    help="Z coordinate of the center"
)


args = parser.parse_args()
print(args)
m = (args.x, args.y, args.z)
print(m)
im = Image.open(args.fname)
b, h = im.size

data = list(im.getdata())

Main.b = b
Main.h = h
Main.m = m
Main.r = args.radius
Main.d = args.density
Main.data = data
Main.include("projekt1.jl")
result = Main.eval("snapshot_sphere(b,h,data,m,r,d)")
user249018
  • 505
  • 2
  • 5
  • 18
  • 1
    Maybe this [article](https://towardsdatascience.com/how-to-embed-your-julia-code-into-python-to-speed-up-performance-e3ff0a94b6e) is useful in some way. – Gealber Jul 01 '21 at 22:12
  • 1
    I advice to put your function `snapshot_sphere` in a module and load the module from Python and then call the function. Here is a detailed manual https://stackoverflow.com/questions/64241264/i-have-a-high-performant-function-written-in-julia-how-can-i-use-it-from-python/64241265#64241265 If you have somewhere different requirements let me know. – Przemyslaw Szufel Jul 02 '21 at 11:33
  • @ Przemyslaw Szufel. Many thanks. Except for calling the `snapshot_sphere` function, I'll then need to transfer a final list of values from the julia environment back to the python environment. From the python environment, I then need to generate the output which is a sphere with a projected picture onto it. Thus, it starts and ends with python, only the intermediate steps are completed in julia. So, it is intricate to move from one environment into the other seamlessly. Do you think this is possible ? – user249018 Jul 02 '21 at 12:10
  • @Przemyslaw Szufel. I get an error message when i do this: `from julia import MyPackage`. This throws the error: `Traceback (most recent call last): File "c:/Users/bhaye/MyPackage/src/call_julia.py", line 13, in from julia import MyPackage File "C:\Python37\lib\site-packages\julia\core.py", line 260, in load_module raise ImportError("{} not found".format(juliapath)) ImportError: MyPackage not found`. I tried moving MyPackage to several different directories with no luck. – user249018 Jul 02 '21 at 19:08
  • What happened when you run ` Pkg.activate(".\\MyPackage")` – Przemyslaw Szufel Jul 03 '21 at 09:07

0 Answers0