Swift: Python interoperability does not work: cannot call value of non-function type 'PyValue'

Created on 28 Apr 2018  路  4Comments  路  Source: tensorflow/swift

Running this code:

let np = Python.import("numpy")
let a = np.arange(15).reshape(3, 5)

throws

py.swift:5:18: error: cannot call value of non-function type 'PyValue'
var a = np.arange(15).reshape(3, 5)
        ~~~~~~~~~^

While np is <module 'numpy' from '/usr/local/lib/python2.7/dist-packages/numpy/__init__.pyc'>

Bu the way, how to link Swift to Python 3?

Most helpful comment

You can pass keyword arguments using the kwargs argument of PyValue.call(with:kwargs:):

import Python
let np = Python.import("numpy")
let matrix = np.ones.call(with: [2, 3], kwargs: [("dtype", np.double)])

You can find look at the python_runtime.swift test file for more Python usage examples. Notably, PyValue conforms to many ExpressibleByXXLiteral protocols, allowing you to write code like:

let dict: PyValue = [1: "hello", 2: 3.14]

Python 3 is not supported, the compiler is currently hard-coded to use Python 2.7. I filed issue #13 to address Python 3 and describe some possible solutions.

All 4 comments

Currently, you have to call np.arange.call(with: 15).reshape.call(with: 3, 5).
The need to call .call(with) explicitly will go away with the Dynamic Callable proposal, but it is yet to be implemented.

Thanks very much! That helped.

How to pass named arguments then?

And what about Python 3?

You can pass keyword arguments using the kwargs argument of PyValue.call(with:kwargs:):

import Python
let np = Python.import("numpy")
let matrix = np.ones.call(with: [2, 3], kwargs: [("dtype", np.double)])

You can find look at the python_runtime.swift test file for more Python usage examples. Notably, PyValue conforms to many ExpressibleByXXLiteral protocols, allowing you to write code like:

let dict: PyValue = [1: "hello", 2: 3.14]

Python 3 is not supported, the compiler is currently hard-coded to use Python 2.7. I filed issue #13 to address Python 3 and describe some possible solutions.

Thanks a lot!

Was this page helpful?
0 / 5 - 0 ratings