Skip to content

Instantly share code, notes, and snippets.

@jiankaiwang
Created November 23, 2020 05:41
Show Gist options
  • Save jiankaiwang/600cced7b88e043819e3c1cd24b75021 to your computer and use it in GitHub Desktop.
Save jiankaiwang/600cced7b88e043819e3c1cd24b75021 to your computer and use it in GitHub Desktop.
Demonstrate on how to leverage Python and Tensorflow in a Swift runtime using XCode under a specific conda environment.

Swift for Tensorflow

Swift is on scheduling becoming the next generation programming language for Tensorflow. Swift APIs for Tensorflow are now released in beta. However, the transient peroid from Python to Swift is still not easy for the data scientists or AI enginners, etc. Fortunately, Swift leverages the Python interoperability. Before we get started to use Python in Swift, let's take a look into the workflow of data scientistics or AI-related engineers. Because of the rapid development of Tensorflow or other deep learning frameworks, their APIs or interfaces are on common changed a lot while a new version released. The scientistics or engineers first create the virtual or seperated environment through virtualenv, conda or even container. These behaviors bring challenges while introduing Python to Swift.

In the following, we demonstarte how to leverage Python in a Swift runtime using XCode under a specific conda environment.

Python Environment

# create a conda env
conda create --name xcodebased python=3.6

# activate the env
conda activate xcodebased

Byy default, the conda env would be at /Users/$USER/anaconda3/envs/xcodebased.

XCode Environment

Before you get started, please download the Swift for TensorFlow toolchain first. In Swift, using the library PythonKit to leverage Python.

# setup the Python environment first
export PYTHONHOME=/users/$USER/anaconda3/envs/xcodebased
export PYTHON_LIBRARY=/users/$USER/anaconda3/envs/xcodebased/lib/libpython3.6m.dylib
echo "PYTHON_LIBRARY: $PYTHON_LIBRARY"

# open the xcode under the environments
# this step is important for using Python in Swift
open -a xcode

Tests

Now install a package in the conda environment for testing.

pip install --no-cache-dir -q numpy

In Swift, you can load the python package using the PythonKit.

//
//  main.swift
//  Tensorflow Example
//
//  Created by Jiankai Wang on 2020/11.
//

import Foundation
import TensorFlow
import PythonKit

print(Python.version)

let np = Python.import("numpy")
print(np)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment