-
Notifications
You must be signed in to change notification settings - Fork 65
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
How to access Python variables from %%Julia in ipython? #278
Comments
it seems that when starting a Python Jupyter notebook and activating This leads to numerous problems that Python objects cannot be successfully transferred to julia. Many are represented lazily by references (which is PythonCall's style), however those references are invalid in the julia's python process. |
After further research, I've found that |
It's important to note that from juliacall import Main as jl and set/get variables on However I have just pushed a change (undocumented and experimental) to the main branch which allows you to specify a set of variables to synchronise between Julia and Python:
Try it out and let me know what you think. You can install it with
|
Looks awesome! Can you check whether the new experimental feature can also transfer python lists? That was failing completely for me |
Can you be more specific in what didn't work, e.g. show some code+output? |
I found again time looking into PythonCall/JuliaCall (the motivation being more consistent and better benchmark timings of julia code when compared to PyCall/PyJulia) I tried to install dev, however it fails during import of juliacall: ERROR: InitError: AssertionError: CTX.which == :PyCall
Stacktrace:
[1] (::PythonCall.C.var"#35#43"{PythonCall.C.var"#python_cmd#41"})()
@ PythonCall.C ~/.julia/packages/PythonCall/dsECZ/src/cpython/context.jl:148
[2] with_gil
@ ~/.julia/packages/PythonCall/dsECZ/src/cpython/gil.jl:10 [inlined]
[3] with_gil
@ ~/.julia/packages/PythonCall/dsECZ/src/cpython/gil.jl:9 [inlined]
[4] init_context()
@ PythonCall.C ~/.julia/packages/PythonCall/dsECZ/src/cpython/context.jl:145
[5] __init__()
@ PythonCall.C ~/.julia/packages/PythonCall/dsECZ/src/cpython/CPython.jl:21
[6] _include_from_serialized(pkg::Base.PkgId, path::String, depmods::Vector{Any})
@ Base ./loading.jl:831
[7] _require_search_from_serialized(pkg::Base.PkgId, sourcepath::String, build_id::UInt64)
@ Base ./loading.jl:1039
[8] _require(pkg::Base.PkgId)
@ Base ./loading.jl:1315
[9] _require_prelocked(uuidkey::Base.PkgId)
@ Base ./loading.jl:1200
[10] macro expansion
@ ./loading.jl:1180 [inlined]
[11] macro expansion
@ ./lock.jl:223 [inlined]
[12] require(into::Module, mod::Symbol)
@ Base ./loading.jl:1144
[13] top-level scope
@ none:8
during initialization of module C
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
Cell In[2], line 1
----> 1 from juliacall import Main as jl
2 get_ipython().run_line_magic('load_ext', 'juliacall')
File ~/Projects/Jolin.io/workshop-accelerate-Python-with-Julia/.venv/lib/python3.10/site-packages/juliacall/__init__.py:218
214 raise Exception('PythonCall.jl did not start properly')
216 CONFIG['inited'] = True
--> 218 init()
220 def load_ipython_extension(ip):
221 import juliacall.ipython
File ~/Projects/Jolin.io/workshop-accelerate-Python-with-Julia/.venv/lib/python3.10/site-packages/juliacall/__init__.py:214, in init()
212 res = jl_eval(script.encode('utf8'))
213 if res is None:
--> 214 raise Exception('PythonCall.jl did not start properly')
216 CONFIG['inited'] = True
Exception: PythonCall.jl did not start properly help is appreciated |
I have made a release, so if you update to JuliaCall v0.9.13 then it should work. |
A new try, and again a failure. I am running v0.9.13 on both sites In jupyter with python 3.10.9 kernel doing
gives
|
I can tell from the stacktrace that you are not on JuliaCall 0.9.13. Note however that this bit of your code:
is not doing what you think. That very first In general, it is not recommended to switch projects in Julia mid-workflow because you can end up with incompatible packages loaded. If you want to use an existing Julia project, you should just do this:
|
I owe you two beers, at least 🙂 . Thank you so much for your help. Yeah the confusion of the juliacall versions was kicking me massively and without your help I wouldn't have noticed. This works lovely with the new juliacall version. I am very happy that I still can present it today evening. Thank you also for the hint with the environments - I will include that right away. |
I'm glad you got it working. I don't suppose you have a recording of your presentation? |
indeed the session was recorded 👍 , it was a 1.5 hours meetup, presenting both PyCall.jl and PythonCall.jl as well as a final benchmark demo Julia vs cpp here the youtube link |
Hello,
after successfully pip installing juliacall and loading its ipython extension, I cannot find a way to access variables defined on the python side.
Say I have a normal python cell
I now would like to access this from julia
I haven't found a way. Can you help and provide some documentation?
The text was updated successfully, but these errors were encountered: