# PythonCall - from X import Y

**URL:** <https://discourse.julialang.org/t/pythoncall-from-x-import-y/89353>\
**Category:** General Usage\
**Created:** [October 27, 2022, 9:11am UTC](https://discourse.julialang.org/t/pythoncall-from-x-import-y/89353 "2022-10-27T09:11:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [October 27, 2022, 9:11am UTC](https://discourse.julialang.org/t/pythoncall-from-x-import-y/89353/1 "2022-10-27T09:11:24Z")

</div>

When I want to import a module X, I can call

```julia
using PythonCall
pyimport("X")

```

I want to import `Image` from `PIL`, which in python is done by

```julia
from PIL import Image

```

However, I can not access `Image` from `PIL` directly:

```julia
>>> import PIL
>>> PIL.Image
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'PIL' has no attribute 'Image'

```

So I need to import them in one statement, or do something else than importing one at a time.

Below is a number of attempts:

```julia
julia> PIL = pyimport("PIL")
Python module: <module 'PIL' from '/usr/lib/python3/dist-packages/PIL/ __init__.py'>

julia> PIL.Image
ERROR: Python: AttributeError: module 'PIL' has no attribute 'Image'
Python stacktrace: none
Stacktrace:
 [1] pythrow()
   @ PythonCall ~/.julia/packages/PythonCall/Td3SH/src/err.jl:94
 [2] errcheck
   @ ~/.julia/packages/PythonCall/Td3SH/src/err.jl:10 [inlined]
 [3] pygetattr(x::Py, k::String)
   @ PythonCall ~/.julia/packages/PythonCall/Td3SH/src/abstract/object.jl:60
 [4] getproperty(x::Py, k::Symbol)
   @ PythonCall ~/.julia/packages/PythonCall/Td3SH/src/Py.jl:272
 [5] top-level scope
   @ REPL[9]:1

julia> PIL = pyimport("from PIL import Image")
ERROR: Python: ModuleNotFoundError: No module named 'from PIL import Image'
Python stacktrace: none
Stacktrace:
 [1] pythrow()
   @ PythonCall ~/.julia/packages/PythonCall/Td3SH/src/err.jl:94
 [2] errcheck
   @ ~/.julia/packages/PythonCall/Td3SH/src/err.jl:10 [inlined]
 [3] pyimport(m::String)
   @ PythonCall ~/.julia/packages/PythonCall/Td3SH/src/concrete/import.jl:11
 [4] top-level scope
   @ REPL[10]:1

julia> PIL = pyimport("PIL"=>"Image")
ERROR: Python: AttributeError: module 'PIL' has no attribute 'Image'
Python stacktrace: none
Stacktrace:
 [1] pythrow()
   @ PythonCall ~/.julia/packages/PythonCall/Td3SH/src/err.jl:94
 [2] errcheck
   @ ~/.julia/packages/PythonCall/Td3SH/src/err.jl:10 [inlined]
 [3] pygetattr(x::Py, k::String)
   @ PythonCall ~/.julia/packages/PythonCall/Td3SH/src/abstract/object.jl:60
 [4] pyimport(::Pair{String, String})
   @ PythonCall ~/.julia/packages/PythonCall/Td3SH/src/concrete/import.jl:12
 [5] top-level scope
   @ REPL[11]:1

```

Anyone know how I can import `Y` from `X` using `PythonCall`?

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [October 27, 2022, 9:14am UTC](https://discourse.julialang.org/t/pythoncall-from-x-import-y/89353/2 "2022-10-27T09:14:46Z")

</div>

`PIL.Image` is a module, so I would try importing it.

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [October 27, 2022, 10:03am UTC](https://discourse.julialang.org/t/pythoncall-from-x-import-y/89353/3 "2022-10-27T10:03:04Z")

</div>

Nice, thanks:

```julia
julia> PILImage = pyimport("PIL.Image")
Python module: <module 'PIL.Image' from '/usr/lib/python3/dist-packages/PIL/Image.py'>

```

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 27, 2022, 11:20am UTC](https://discourse.julialang.org/t/pythoncall-from-x-import-y/89353/4 "2022-10-27T11:20:44Z")

</div>

It’s a module in your case, but not always(?), so the general question hasn’t been answered. I’m still curious, and you may want to uncheck the solution box.

It’s probably the second option:

```julia
pyimport(m)
pyimport(m => k)
pyimport(m => (k1, k2, ...))
pyimport(m1, m2, ...)

```

In PyCall.jl you could do (while not recommended _for import_, for some reason I forget):

```julia
py"from PIL import Image"

```

The py macro is awesome, e.g. multi-line py"“” but I’m not sure it’s supported in PythonCall.jl. Is there similar (or a workaround)? Also as with RCall.jl a Python REPL mode would be awesome. It’ not there (yet), while there is a Pkg conda REPL mode…
