Skip to content

PythonCall not working in Jupyter Notebook in VSCode #601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AquaPore opened this issue Apr 8, 2025 · 5 comments
Closed

PythonCall not working in Jupyter Notebook in VSCode #601

AquaPore opened this issue Apr 8, 2025 · 5 comments
Labels
bug Something isn't working

Comments

@AquaPore
Copy link

AquaPore commented Apr 8, 2025

Affects: PythonCall

Describe the bug
The PythonCall is not working in Jupyter Notebook in VScode.

The answer to fib(100) problem see below is nothing. Nevertheless the code works beautifully in REPL.

Many thanks for any help you may provide.

Your system
Please provide detailed information about your system:

  • The operating system: Windows 10
  • The version of Julia, Python, PythonCall, JuliaCall: latest

Additional context

using PythonCall

fib(x) = pyexec("""
def fib(n):
	a, b = 0, 1
	while a < n:
		print(a, end=' ')
		a, b = b, a+b
	print()
fib(y)
""", Main, (y=x,))

print(fib(10))
@AquaPore AquaPore added the bug Something isn't working label Apr 8, 2025
@cjdoris
Copy link
Collaborator

cjdoris commented Apr 9, 2025

pyexec always returns nothing when called like that, it would have been the same in the REPL. In the REPL it probably just looked like it returned something because you print the answer inside fib.

To extract values you can pass a named tuple type specifying what to keep, like

pyexec(@NamedTuple{ans::BigInt}, """
.......
ans = fib(y)
""", Main, (y=x,))

Also see the @pyexec macro which makes this a bit easier.

@AquaPore
Copy link
Author

AquaPore commented Apr 9, 2025

Hi Christopher,

A great thanks for your kind help.

Thanks for your suggestion by using pyexec, nevertheless, the following code still does not work as I get the following error message:

cannot convert this Python 'NoneType' to a Julia 'BigInt'

Did I do something wrong. Also I am confused why you suggest to use BigInt as I taught Float64 would be more appropriate.

Many thanks for your kind help,
Joseph

function PYTHON_2_JULIA(x)

fib(x) = pyexec(@NamedTuple{ans::BigInt},"""
def fib(n):
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a+b
    print()
ans = fib(y)
""", Main, (y=x,))

C = fib(x)

println(C)

return C
end

@AquaPore
Copy link
Author

AquaPore commented Apr 9, 2025

I found an alternative solution by using as you suggested by using ** PythonCall.@PYEXEC**

function PYTHON_2_JULIA(P_Int, P_Float, P_String, P_Vector)

    PythonCall.@pyexec """
        def PYTHON_2_JULIA(P_Int, P_Float, P_String, P_Vector):
            P_Int1 = P_Int + 1
            P_Float1 = P_Float * 2.0
            P_String1 = P_String + P_String 
            P_Vector1 = P_Vector + P_Vector
            return P_Int1, P_Float1, P_String1, P_Vector1
        """=> PYTHON_2_JULIA

    P_Int1, P_Float1, P_String1, P_Vector1 = PythonCall.pyconvert(Any, PYTHON_2_JULIA(P_Int, P_Float, P_String, P_Vector))

return P_Int1, P_Float1, P_String1, P_Vector1
end

To run it:

P_Int=1; P_Float=2.0; P_String=3; P_Vector = [1.0, 2.0, 3.0, 4.0]
P_Int1, P_Float1, P_String1, P_Vector1 = .PYTHON_2_JULIA(P_Int, P_Float, P_String, P_Vector)

@show P_Int1, P_Float1, P_String1, P_Vector1

P_Vector1[1]

This gives as expected

(2, 4.0, 6, [2.0, 4.0, 6.0, 8.0])
2.0

@cjdoris
Copy link
Collaborator

cjdoris commented Apr 9, 2025

Glad it's working now, I'll close the issue as resolved.

BTW the reason the previous fib wasn't working is because it didn't actually return anything.

@cjdoris cjdoris closed this as completed Apr 9, 2025
@AquaPore
Copy link
Author

Thanks @cjdoris , the idea is to wrap up some pyflwdir python package. Would you know how to call for e.g. numpy package into the example?

Thanks for any help you may provide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants