Skip to content

PythonCall not working in Jupyter Notebook in VSCode #601

Closed

Description

@AquaPore

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

Activity

  1. cjdoris commented on Apr 9, 2025

    @cjdoris
    Member

    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.

  2. AquaPore commented on Apr 9, 2025

    @AquaPore
    Author

    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
    
  3. AquaPore commented on Apr 9, 2025

    @AquaPore
    Author

    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
    
  4. cjdoris commented on Apr 9, 2025

    @cjdoris
    Member

    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.

  5. AquaPore commented on Apr 14, 2025

    @AquaPore
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions