Python interop with PythonKit

I've been really interested in the Python integration provided by Swift for Tensorflow and I got the impression from reading the README that PythonKit ( should work on regular swift. However I'm getting an error: error: no such module 'PythonKit'. I'm using the latest dev version of swift (2018-08-29). Are there any requirements I need to fulfil to be able to use it? I installed it on a Ubuntu docker image (modified from the official ones to use the dev toolkit).

Also does anyone know if there's a way to be able to import a local python project with it? I guess I could install it on the global interpreter with pip install -e /path/to/module/ but it's less than ideal.

I haven’t tried PythonKit, but its read‐me instructions are just basic package dependency set‐up.

Depending on how far you have gotten already, general information about the package manager can be found here, and details about its manifest format are here.

There could be many other causes for “no such module”, but I hypothesize the following: If you just followed PythonKit’s instructions to the letter, pasting their snip‐it into your manifest, then you would still have to declare a dependency from your target to their product. (At this point the package manager knows that you want to use their package and at what version, but it doesn’t know which of your modules are supposed to depend on which of their products.) Notice how each target also has its own dependency declaration in the example at the top of the manifest API documentation.

1 Like

Wow, can't believe I missed something so simple. In hindsight I should have chosen a more mainstream library as my first import so I got familiar with SPM. It worked flawlessly thanks @SDGGiesbrecht.

Does anyone know if you can import local python modules like you can do with C?

I am not familiar with PythonKit (or even Python for that matter), but the example in the PythonKit read‐me contains a code translation example with these lines:


import sys


let sys = try Python.import("sys")

There are more such code translation examples here that involve other modules.

If that does not fully answer your question, you should probably ask the PythonKit folks instead.

Sure, I've seen all these but they are all examples of using libraries installed in the global interpreter. Python is different to other languages in this regard since when you install a library with pip it gets installed globally not just for your project (unless you use a virtualenv). Those are the ones I can import easily. That's why I suggested that I can install a module into my global interpreter so I can import it but it's not ideal. What I wanted to do is to be able to import python files relative to my path.

Still I found a way to do it. Python can do relative imports to the path of your file, but this doesn't work from swift. The other way is to add the base path of your python file to the PYTHONPATH which you can do from swift. Say you have a python file called with a function named hello on the root of your swift project, you can call it from swift like so:

let os = Python.import("os")
let sys = Python.import("sys")

let aaa = Python.import("aaa")

In case it helps anyone.

Terms of Service

Privacy Policy

Cookie Policy