swift on linux
This blog is a quick note on swift for linux.
swift has been designated by google as a major machine learning language. google appears to be rewriting tensorflow in swift. More info is here: https://www.tensorflow.org/swift.
While python is not going away, they are working hard in making python a smooth integration, much in the same way that the graal project is integrated in multiple languages into the jvm platform.
When you want to run python code in swift, the tensorflow example on their web site assume you are use google colab which already has the swift-python interop module installed. You can also install “swift for tensorflow” from binaries provided on github. Check out https://github.com/tensorflow/swift/blob/master/Installation.md for instructions. If you are not using ubuntu, you may be out of luck as the build process for tensorflow can be complex. If you are able to install this version, you will get a swift version that already has tensor flow and python interop installed. The tarball may work for you so give it a try even if you are not on ubuntu.
However, if you already have tensorflow installed for python, perhaps you would just like to use that python version in swift to get a feel for swift. To do this, you need your distribution’s default swift to have some python interop.
A widely accessible interop lib is at https://github.com/pvieito/PythonKit. This interop package is kept in sync with the tensorflow version on the tensorflow github repo.
Let’s use this version with a generic swift environment.
swift for your distribution, python interop via swift package manager
If you are on your workstation, you may want to have swift-python interop using a shell. Here’s how to do that:
- Install swift for your distirbution. I’m on a redhat fedora system so I do:
dnf install swift
With this install you could run a swift program. To use external packages you use the swift package manager (SPM) which is built into the build tools. More info is here: https://swift.org/package-manager/. Swift has a branching command structure to run swift build, you type
swift build and underneath the hood, it runs a
swift-build executable. That’s nice and extensible, so that one executable is not stuck with everything in it.
You’ll see that just like scala, the specification for building a program is a swift program itself and uses the swift programming language. I’ve not used it extensively to see how it compares to sbt.
To use its integration with python, we must integrate it in using SPM if we are doing a standard program build using
PythonKit site indicates how to do this. You can use this in any project. Just add the github location to your dependencies:
// Your Package.swift project file ... .package(url: "https://github.com/pvieito/PythonKit.git", .branch("master")), ...
You may want to try this from a quick swift script though.
If you want to run a swift shell for interactive work you can,
swift works fine. But like most languages that depend on libs that are statically compiled (e.g. c++, rust, etc.) you need to specify your dependencies somehow. scala has https://ammonite.io/ which allows robust scala script and, most importantly, it allows you to dynamically add packages as you need them via an ivy import process. It works well.
For swift, you can install swift-sh which at least allows you to write shell scripts that use external libs. Let’s use
swift-sh to show python interop.
We need to install swift-sh. There are 2 ways.
swift-sh install way #1
Install cask on linux using these instructions: https://docs.brew.sh/Homebrew-on-Linux. Once you have install cask, you can run
brew install swift-sh.
You can then create a script that looks like:
#!/usr/bin/swift sh import PythonKit // @pvieito ~> master let sys = Python.import("sys") print("Python \(sys.version_info.major).\(sys.version_info.minor)") print("Python Version: \(sys.version)") print("Python Encoding: \(sys.getdefaultencoding().upper())")
chmod u+x test.swift and run it, you get output similar to:
Python 3.7 Python Version: 3.7.6 (default, Jan 30 2020, 09:44:41) [GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] Python Encoding: UTF-8
swift-sh install way #2 (preferred)
Installing cask just to install swift-sh may be a bit too much overhead. You can instead, just build
swift-sh from git and copy it to your personal
1 git clone https://github.com/mxcl/swift-sh.git
2 cd swift-sh
3 swift build -c release
4 cp .build/release/swift-sh ~/bin/
Now we can run the script above but we should use the more general and preferred approach:
#!/usr/bin/env swift-sh ...
You will get the same output as before of course.