"Python Kit" that calls a Python script from Swift

You can run Python scripts from Swift using PythonKit. This allows you to develop Mac apps that run Python scripts. (Does not work on iOS) スクリーンショット 2020-06-18 13.58.08.png

  1. Open your Xcode project. Go to "file-> Swift Packages-> Add Package Dependency". Copy and paste the Package Dependency URL from the PythonKit GitHub repository (https://github.com/pvieito/PythonKit). スクリーンショット 2020-06-18 12.21.49.png

  2. Select Branch: master. スクリーンショット 2020-06-18 12.25.27.png

  3. Create a Python file. For example, I wrote "example.py". ↓

example.py


def hello():
    print("Hello PythonKit")
}
  1. Make the following settings in the Xcode project settings "TARGETS → Signing & Capabilities". · Access files on your Mac instead of the app sandbox -Disable library validation スクリーンショット 2020-06-18 12.34.06.png

  2. Call the Python file from the view controller.

ViewController.swift


override func viewDidLoad() {
    super.viewDidLoad()
    let sys = Python.import("sys")
    sys.path.append("/Users/mlboy/PythonTest/") // example.Path to the directory where py is
    let example = Python.import("example") //Import python file
    example.hello() //Call a Python function
   }

Check the Xcode console. The Python file will be executed and the "Hello Python Kit" will be printed. スクリーンショット 2020-06-18 12.54.29.png

Recommended Posts

"Python Kit" that calls a Python script from Swift
Use Django from a local Python script
Run a Python script from a C # GUI application
Create a New Todoist Task from Python Script
Call a Python script from Embedded Python in C ++ / C ++
Run a python script from excel (using xlwings)
A python script that draws a band diagram from the VASP output file EIGENVAL
What's in that variable (when running a Python script)
Python script to create a JSON file from a CSV file
Run illustrator script from python
A python script that gets the number of jobs for a specified condition from indeed.com
A memo that reads data from dashDB with Python & Spark
From a book that programmers can learn ... (Python): About sorting
A Python script that saves a clipboard (GTK) image to a file.
How to run a Python program from within a shell script
Let's create a script that registers with Ideone.com in Python.
python Condition extraction from a list that I often forget
From a book that programmers can learn (Python): Decoding messages
A Python program that aggregates time usage from icalendar data
A set of script files that do wordcloud in Python3
A python script that converts Oracle Database data to csv
A Python script that compares the contents of two directories
A Python script that goes from Google search to saving the Search results page at once
A Python script that allows you to check the status of the server from your browser
Execute Python script from batch file
Call a Python function from p5.js.
Touch a Python object from Elixir
python / Make a dict from a list.
Launch a Python script as a service
Write a batch script with Python3.5 ~
[python] Script that (should) update pwsh
From a book that programmers can learn (Python): Find the mode
From a book that programmers can learn ... (Python): Review of arrays
[Python algorithm] A program that outputs Sudoku answers from a depth-first search
A script that returns 0, 1 attached to the first Python prime number
A python script that deletes ._DS_Store and ._ * files created on Mac
From a book that programmers can learn (Python): Statistical processing-deviation value
[Python] A program that creates stairs with #
A Python script that automatically collects typical images using bing image search
Execute a script from Jupyter to process
Extract lines that match the conditions from a text file with python
# 5 [python3] Extract characters from a character string
Run the Python interpreter in a script
From a book that programmers can learn (Python): Conditional search (maximum value)
Create a deb file from a python package
[Python] Create a LineBot that runs regularly
Generate a class from a string in Python
A typed world that begins with Python
Manipulate BigQuery tables from a Python client
A python script for Mac that zips without garbled characters on Windows
A script that morphologically parses a specified URL
A program that plays rock-paper-scissors using Python
Call a command from Python (Windows version)
From a book that makes the programmer's way of thinking interesting (Python)
[Python] A program that rounds the score
How to run a Maya Python script
From a book that programmers can learn (Python): Class declaration (public / private, etc.)
[Python, PyPDF2] A script that divides a spread PDF into two left and right
A Python script that reads a SQL file, executes BigQuery and saves the csv
I made a Docker image that can call FBX SDK Python from Node.js
In Python, I made a LINE Bot that sends pollen information from location information.