math = pyimport("math") # returns a freshly created wrapper module
math.sin(3)
In fact, such wrapping functionality could probably be pretty independent of the wrapped language, so perhaps there could be a JuliaWrap package that could be used by interfaces to many different ones.
A weak reference is not what we want here, because it is only for cached resources I don't know of a way to inject something like this on the Python side, but ifthat are allowed to disappear. We need the Python object to have a "real" reference to the Julia object, in that the Julia object *must* not disappear as long as Python wrappers around it exist.
julia> using PyCall
julia> pyinitialize() # will go away once #2378 is fixed
No, pyinitialize() can't go in the module load. The user has to have the option to call pyinitialize() manually (after loading the module but before calling any Python functions), for example to specify a different version of Python to load.
--SGJ
Does this work by initializing Python with whatever initialization parameters are set if Python is not already whenever the first call to Python functionalities is made ?
I'm not sure I follow this bit (which is probably my fault for not
looking at the code): couldn't you have shelled out to something like
'env python' or 'which python' instead?
On Monday, February 25, 2013 4:21:20 AM UTC-5, lgautier wrote:Does this work by initializing Python with whatever initialization parameters are set if Python is not already whenever the first call to Python functionalities is made ?
Not sure I understand your question, but let me just explain in more detail.
Every (high-level) PyCall function first checks an "initialized" global to see whether Python is initialized (shared library loaded, Python interpreter started, etcetera). If not, it calls pyinitialize() to initialize Python with the default parameters (via running the "python" executable to query the Python library name). Optionally, the user can call pyinitialize manually in order to initialize using a different Python version or at a different time.
The problem with #2378 (which is now fixed anyway) had to do with calling the "python" executable from the @pyimport macro, which deadlocked with the parser. I worked around this by changing the @pyimport macro so that most of the heavy lifting is done from a pywrap function called by the macro. That way, the initialization is moved from parse-time to run-time, which seemed like a good idea anyway.
--SGJ
Using the weakref mechanism in Python is a clean way to get a notification when the Python object is dead so that the Julia object can be removed from this collection.I'm not sure how to do this purely in Julia, but here is an example of getting a callback from Python->Julia when a Python object is finalized after pydecref:Note that the cleanup callback passes the weakref object, not the original object.