PyCall.jl icon indicating copy to clipboard operation
PyCall.jl copied to clipboard

Add the `from module import a as b` syntax to PyCall

Open heetbeet opened this issue 3 years ago • 5 comments

I have implemented a macro to replicate the from module import a as b, c as d syntax as @pyfrom module import a as b, c as d https://github.com/AutoActuary/PyFrom.jl

Would it be possible to get something like this into PyCall?

heetbeet avatar Jun 02 '21 20:06 heetbeet

no one opposed this, you should file a PR!

CarloLucibello avatar Dec 08 '21 08:12 CarloLucibello

I agree, or at least state in the readme that it's not supported and what the canonical way of importing submodules is (I couldn't find it). Alternatively a more Julian way, like @pyimport NiceStuff: nice, DOG could also be nice.

undefdev avatar Mar 03 '22 15:03 undefdev

PythonCall has something like this with @py.

stevengj avatar Mar 05 '22 21:03 stevengj

I had a look at this issue again today and saw that @pyimport is marked as deprecated in the source code: https://github.com/JuliaPy/PyCall.jl/blob/b27bd5a9ecabec4a30ba6062c3754768746e3adb/src/PyCall.jl#L413

Is this correct and intended? In this case, should @pyimport even be considered?

Is does remove quite a bit of boilerplate code in the case of a nested imports or renaming, e.g. @pyfrom scipy.ndimage import gaussian_filter or @pyfrom scipy.ndimage import gaussian_filter as blur

heetbeet avatar May 03 '22 16:05 heetbeet

This works and is not much more code than the proposed syntax:

gaussian_filter = pyimport("scipy.ndimage").gaussian_filter
blur = pyimport("scipy.ndimage").gaussian_filter

In general, I'm not super excited about imitating Python syntax in Julia.

stevengj avatar May 03 '22 20:05 stevengj