JuliaPy / PyCall.jl

Package to call Python functions from the Julia language
MIT License
1.47k stars 187 forks source link

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

Open heetbeet opened 3 years ago

heetbeet commented 3 years ago

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?

CarloLucibello commented 2 years ago

no one opposed this, you should file a PR!

undefdev commented 2 years ago

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.

stevengj commented 2 years ago

PythonCall has something like this with @py.

heetbeet commented 2 years ago

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

stevengj commented 2 years ago

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.