Unable to import Python file in VSCode Julia notebook

Viewed 161

I'm trying to import a Python file containing a single function into my Julia notebook in VSCode.

The file is called helloPython.py and contains a single function called helloFromPython.

It contains the following code.

def helloFromPython():
    print("Hello Julia, it's me Python!")

It is located in the same directory as my notebook file.

My first cell looks as follows.

import Pkg
Pkg.add("PyCall")
Pkg.build("PyCall")
using PyCall

Importing inbuilt Python functions works as expected, e.g.

math = pyimport("math")
math.sin(math.pi / 4)

Defining and calling Python code on the fly also works as expected, for example

begin
    py"""
def f(x):
    return x**3
"""
    f = py"f"
end

f(2) # prints 8

However, importing my local Python file does not work. My cell looks as follows.

pushfirst!(PyVector(pyimport("sys")."path"), "")
x = pyimport("helloPython")
x.helloFromPython()

There is no error message. The cell executes successfully and that's it.

I've tried the same using the inbuild terminal in VSCode starting the Julia command prompt and it works as expected.

What am I doing wrong here?

Edit: @alex338207 had a good idea here, so I'm updating my question.

Writing to a file works. Here is a minimal working example. The file helloPython.py contains the following code.

def helloFromPython():
    f = open("test.txt", "a")
    f.write("Hello Julia!")
    f.close()

Calling the above in VSCode as describe above, works as expected. A file is created with the provided content "Hello Julia!".

However, returning stuff doesn't work. Here is another example.

def helloFromPython():
    return "Hello Julia!"

Calling the above yields to the output nothing.

I've updated my issue on Github.

0 Answers
Related