diff --git a/docs/src/pycall.md b/docs/src/pycall.md index 852b8f53..89d95e86 100644 --- a/docs/src/pycall.md +++ b/docs/src/pycall.md @@ -10,7 +10,7 @@ On this page, we give some tips for migrating between the two modules and a comp - On Unix (Linux, Mac, etc.) the Python interpreter used by PythonCall and PyCall must be the same (see below). - On Windows, it appears to be possible for PythonCall and PyCall to use different interpreters. - To force PythonCall to use the same Python interpreter as PyCall, set the environment variable `JULIA_PYTHONCALL_EXE` to `"@PyCall"`. Note that this will opt out of automatic dependency management using CondaPkg. -- Alternatively, to force PyCall to use the same interpreter as PythonCall, set the environment variable `PYTHON` to `PythonCall.C.CTX.exe_path` and then `Pkg.build("PyCall")`. You will need to do this each time you change project, because PythonCall by default uses a different Python for each project. +- Alternatively, to force PyCall to use the same interpreter as PythonCall, set the environment variable `PYTHON` to `PythonCall.python_executable_path()` and then `Pkg.build("PyCall")`. You will need to do this each time you change project, because PythonCall by default uses a different Python for each project. ## Comparison diff --git a/src/C/C.jl b/src/C/C.jl index b81ab7d4..4ceda829 100644 --- a/src/C/C.jl +++ b/src/C/C.jl @@ -17,6 +17,7 @@ include("pointers.jl") include("extras.jl") include("context.jl") include("gil.jl") +include("api.jl") function __init__() init_context() diff --git a/src/C/api.jl b/src/C/api.jl new file mode 100644 index 00000000..00930fbc --- /dev/null +++ b/src/C/api.jl @@ -0,0 +1,27 @@ +""" + python_executable_path() + +Path to the Python interpreter, or `missing` if not known. +""" +python_executable_path() = CTX.exe_path + +""" + python_library_path() + +Path to libpython, or `missing` if not known. +""" +python_library_path() = CTX.lib_path + +""" + python_library_handle() + +Handle to the open libpython, or `C_NULL` if not known. +""" +python_library_handle() = CTX.lib_ptr + +""" + python_version() + +The version of Python, or `missing` if not known. +""" +python_version() = CTX.version diff --git a/src/PythonCall.jl b/src/PythonCall.jl index 78eb785f..618b99d4 100644 --- a/src/PythonCall.jl +++ b/src/PythonCall.jl @@ -17,13 +17,16 @@ include("Compat/Compat.jl") for m in [:Core, :Convert, :PyMacro, :Wrap, :JlWrap, :Compat] for k in names(@eval($m)) if k != m - @eval using .$m: $k + @eval const $k = $m.$k @eval export $k end end end # non-exported API +for k in [:python_executable_path, :python_library_path, :python_library_handle, :python_version] + @eval const $k = C.$k +end for k in [:pynew, :pyisnull, :pycopy!, :getptr, :pydel!, :unsafe_pynext, :PyNULL, :CONFIG] @eval const $k = Core.$k end diff --git a/test/C.jl b/test/C.jl index 46409041..69f8697a 100644 --- a/test/C.jl +++ b/test/C.jl @@ -1 +1,18 @@ -# TODO +@testitem "python info" begin + @testset "python_executable_path" begin + @test PythonCall.python_executable_path() isa String + @test occursin("python", PythonCall.python_executable_path()) + end + @testset "python_library_path" begin + @test PythonCall.python_library_path() isa String + @test occursin("python", PythonCall.python_library_path()) + end + @testset "python_library_handle" begin + @test PythonCall.python_library_handle() isa Ptr{Cvoid} + @test PythonCall.python_library_handle() != C_NULL + end + @testset "python_version" begin + @test PythonCall.python_version() isa VersionNumber + @test PythonCall.python_version().major == 3 + end +end