Skip to content
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 pass a numpy array into a Julia function? #38

Closed
ShuhuaGao opened this issue Sep 27, 2021 · 9 comments
Closed

How to pass a numpy array into a Julia function? #38

ShuhuaGao opened this issue Sep 27, 2021 · 9 comments

Comments

@ShuhuaGao
Copy link
Contributor

Consider a dummy function inside my custom package EOS:

function dummy(x::AbstractVector)
    @show typeof(x)
end

From Python, I call it as follows

jl.EOS.dummy(np.array([1.1, 2.3]))

and the following error occurs

TypeError: Julia: MethodError: no method matching dummy(::PythonCall.PyIterable{PythonCall.Py})
Closest candidates are:
  dummy(!Matched::AbstractVector{T} where T) at /home/shuhua/Work/opt/EOS/src/EOS.jl:26

According to Conversion to Julia, I expect that a numpy array is converted to a Julia array automatically. Did I miss anything?

@cjdoris
Copy link
Collaborator

cjdoris commented Sep 27, 2021

Ah sorry, the package was recently rewritten and that functionality got overlooked - numpy arrays should automatically get converted to PyArray but that doesn't happen yet.

For now, on the Julia side, you can do PyArray(x) or pyconvert(PyArray, x) or pyconvert(Array, x) or pyconvert(AbstractArray, x) to convert x to a Julia array. From Python you can do dummy = jl.eval("x->EOS.dummy(PyArray(x))") then call dummy(x).

@cjdoris
Copy link
Collaborator

cjdoris commented Sep 27, 2021

Fixed on master branch - try it out with ] add PythonCall#master.

@cjdoris cjdoris closed this as completed Sep 27, 2021
@ShuhuaGao
Copy link
Contributor Author

Does it work on the Python side? I am using juliacall to pass a numpy array into a Julia function. The issue reported above still exists.

I first removed the old juliacall and then installed the latest master branch.

pip uninstall juliacall
pip install git+https://github.com/cjdoris/PythonCall.jl@master

@cjdoris
Copy link
Collaborator

cjdoris commented Sep 28, 2021

You need to do an editable install pip install -e ... to get the dev version of PythonCall, otherwise you get the latest release which doesn't have this fix yet.

@ShuhuaGao
Copy link
Contributor Author

Thanks. I tried

pip install -e git+https://github.com/cjdoris/PythonCall.jl@master#egg=juliacall

but failed to install it.

Installing collected packages: juliacall
  Running setup.py develop for juliacall
    ERROR: Command errored out with exit status 1:
     command: /home/shuhua/miniconda3/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/home/shuhua/Work/opt/src/juliacall/setup.py'"'"'; __file__='"'"'/home/shuhua/Work/opt/src/juliacall/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' develop --no-deps
         cwd: /home/shuhua/Work/opt/src/juliacall/
    Complete output (5 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/home/shuhua/miniconda3/lib/python3.9/tokenize.py", line 392, in open
        buffer = _builtin_open(filename, 'rb')
    FileNotFoundError: [Errno 2] No such file or directory: '/home/shuhua/Work/opt/src/juliacall/setup.py'

@cjdoris
Copy link
Collaborator

cjdoris commented Sep 28, 2021

Sorry I'm no pip expert but it looks like an error on your end. Maybe your pip is out of date? That exact same command works for me in a fresh environment. You could see what's been put into /home/shuhua/Work/opt/src/juliacall. Or you could try cloning this repo yourself and doing pip install -e /path/to/repo.

@ShuhuaGao
Copy link
Contributor Author

Now it works after updating pip.

Testing with the old pip of version 20.3.1

Or you could try cloning this repo yourself and doing pip install -e /path/to/repo.
The error is

pip install -e ./PythonCall.jl/
ERROR: File "setup.py" not found. Directory cannot be installed in editable mode: /home/shuhua/Work/opt/PythonCall.jl
(A "pyproject.toml" file was found, but editable mode currently requires a setup.py based build.)

Apparently, pip requires a setup.py to proceed.

Testing with pip 21.2.4

  1. First update to the latest pip.
pip install --upgrade pip
  1. Then everything works well.

Since there is indeed no setup.py in the repository, it seems that only the newer pip (perhaps >= 21.x.x) behaves. It deserves mentioning this requirement on pip in the doc.

@cjdoris
Copy link
Collaborator

cjdoris commented Sep 28, 2021

Will do thanks.

@cjdoris cjdoris reopened this Sep 28, 2021
@ShuhuaGao
Copy link
Contributor Author

For your information, the minimal pip version is 21.1.
https://pip.pypa.io/en/stable/news/#v21-1

Add support for editable installs for project with only setup.cfg files.

@ShuhuaGao ShuhuaGao mentioned this issue Sep 29, 2021
@cjdoris cjdoris closed this as completed Oct 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants