Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions .github/workflows/standalone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: Standalone executables

on:
push:
pull_request:
release:
types: [created]

permissions:
contents: read

jobs:
build-and-test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- os: ubuntu-22.04
platform: linux-x86_64
- os: macos-14
platform: macos-arm64
- os: windows-latest
platform: windows-x86_64
steps:
- uses: actions/checkout@v7
- name: Set up Python
uses: actions/setup-python@v7
with:
python-version: "3.14"
cache: pip
cache-dependency-path: pyproject.toml
- name: Install build and test dependencies
run: pip install . --group executable --group dev
- name: Build standalone executable
run: python packaging/build.py
- name: Test standalone executable
shell: bash
run: |
executable="$(find dist -maxdepth 1 -type f -name 'sqlite-utils-*' -print -quit)"
test -n "$executable"
SQLITE_UTILS_EXECUTABLE="$executable" pytest -v tests/test_standalone.py
- name: Save executable for release upload
if: github.event_name == 'release'
uses: actions/upload-artifact@v4
with:
name: sqlite-utils-${{ matrix.platform }}
path: dist/sqlite-utils-*
if-no-files-found: error

upload-release-assets:
if: github.event_name == 'release'
needs: [build-and-test, test-build-time-plugin]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Download platform executables
uses: actions/download-artifact@v4
with:
path: dist
pattern: sqlite-utils-*
merge-multiple: true
- name: Upload executables to release
env:
GH_TOKEN: ${{ github.token }}
run: gh release upload "$GITHUB_REF_NAME" dist/* --clobber

test-build-time-plugin:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- name: Set up Python
uses: actions/setup-python@v7
with:
python-version: "3.14"
cache: pip
cache-dependency-path: pyproject.toml
- name: Install build and test dependencies
run: pip install . --group executable --group dev
- name: Install test plugin
run: pip install ./tests/standalone_plugin
- name: Build standalone executable with plugin
run: python packaging/build.py
- name: Test bundled plugin
shell: bash
run: |
executable="$(find dist -maxdepth 1 -type f -name 'sqlite-utils-*' -print -quit)"
SQLITE_UTILS_EXECUTABLE="$executable" \
SQLITE_UTILS_STANDALONE_TEST_PLUGIN=sqlite-utils-standalone-test-plugin \
pytest -v tests/test_standalone.py
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Read more on my blog, in this series of posts on [New features in sqlite-utils](

pip install sqlite-utils

Standalone executables that do not require Python are available from the
[GitHub releases](https://github.com/simonw/sqlite-utils/releases). See the
[standalone installation instructions](https://sqlite-utils.datasette.io/en/stable/installation.html#standalone-executables).

Or if you use [Homebrew](https://brew.sh/) for macOS:

brew install sqlite-utils
Expand Down
48 changes: 48 additions & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,54 @@ Using pipx

pipx install sqlite-utils

.. _installation_standalone:

Standalone executables
======================

Standalone executables for Linux, macOS and Windows are attached to each
`GitHub release <https://github.com/simonw/sqlite-utils/releases>`__. They
include Python and the required ``sqlite-utils`` dependencies, so Python does
not need to be installed separately. Download the executable for your operating
system and processor architecture, then run it directly. Release assets are
named ``sqlite-utils-linux-x86_64``, ``sqlite-utils-macos-arm64`` and
``sqlite-utils-windows-x86_64.exe``. On macOS or Linux you may need to make the
downloaded file executable first::

chmod +x sqlite-utils-*

The Linux executable is built on Ubuntu 22.04 and requires a compatible glibc
runtime. Executables are specific to their operating system and CPU architecture.
The SQLite library is the one bundled with the Python runtime used to build the
executable; available SQLite features therefore depend on that build. SQLite
extensions such as SpatiaLite are not included and must be supplied separately
as compatible native libraries.

The executable includes plugins installed in the build environment. Since it
does not use a system Python installation, plugins installed after downloading
the executable are not discovered. The ``install`` and ``uninstall`` commands
are therefore unavailable in the executable. To use additional plugins, install
them before building your own executable as described below. Other optional
integrations such as ``numpy``, ``pandas``, ``sqlite_dump`` and ``pysqlite3`` are
included when installed in the build environment. Modules imported dynamically
by custom Python code may need an explicit PyInstaller hook.

Building a standalone executable
--------------------------------

Builds must be performed on the operating system and architecture for which the
executable is intended; PyInstaller does not cross-compile. From a checkout of
the ``sqlite-utils`` source, install the executable build dependencies and run
the build script::

pip install . --group executable
python packaging/build.py

The platform-specific executable will be written to ``dist/``. To include
plugins, install them into the same Python environment before running the build
script. PyInstaller collects ``sqlite_utils`` entry points and their modules
from that environment.

.. _installation_sqlite3_alternatives:

Alternatives to sqlite3
Expand Down
67 changes: 67 additions & 0 deletions packaging/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import platform
import shutil
import subprocess
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
DIST = ROOT / "dist"


def main() -> None:
system = platform.system().lower()
machine = platform.machine().lower()
architectures = {
"amd64": "x86_64",
"aarch64": "arm64" if system == "darwin" else "aarch64",
"arm64": "arm64",
"x86_64": "x86_64",
}
if system not in {"darwin", "linux", "windows"}:
raise SystemExit(f"Unsupported operating system: {system}")
try:
architecture = architectures[machine]
except KeyError:
raise SystemExit(f"Unsupported architecture: {machine}") from None

if DIST.exists():
shutil.rmtree(DIST)
DIST.mkdir(parents=True)

subprocess.run(
[
sys.executable,
"-m",
"PyInstaller",
"--clean",
"--noconfirm",
"--onefile",
"--name",
"sqlite-utils",
"--copy-metadata",
"sqlite-utils",
"--additional-hooks-dir",
str(ROOT / "packaging" / "pyinstaller" / "hooks"),
"--distpath",
str(DIST),
"--workpath",
str(ROOT / "build" / "pyinstaller"),
"--specpath",
str(ROOT / "build" / "pyinstaller"),
str(ROOT / "packaging" / "standalone.py"),
],
cwd=ROOT,
check=True,
)

executable_os = {"darwin": "macos"}.get(system, system)
extension = ".exe" if system == "windows" else ""
executable = DIST / f"sqlite-utils{extension}"
asset = DIST / f"sqlite-utils-{executable_os}-{architecture}{extension}"
asset.parent.mkdir(exist_ok=True)
shutil.move(str(executable), str(asset))
print(f"Built {asset}")


if __name__ == "__main__":
main()
7 changes: 7 additions & 0 deletions packaging/pyinstaller/hooks/hook-sqlite_utils.db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from PyInstaller.utils.hooks import can_import_module

hiddenimports = [
module
for module in ("numpy", "pandas", "sqlite_dump", "pysqlite3", "pysqlite3.dbapi2")
if can_import_module(module)
]
3 changes: 3 additions & 0 deletions packaging/pyinstaller/hooks/hook-sqlite_utils.plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from PyInstaller.utils.hooks import collect_entry_point

datas, hiddenimports = collect_entry_point("sqlite_utils") # codespell:ignore datas
4 changes: 4 additions & 0 deletions packaging/standalone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from sqlite_utils.cli import cli

if __name__ == "__main__":
cli()
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ docs = [
"sphinx-autobuild",
"sphinx-copybutton",
]
executable = [
"pyinstaller==6.22.3",
]

[project.urls]
Homepage = "https://github.com/simonw/sqlite-utils"
Expand All @@ -76,6 +79,9 @@ sqlite-utils = "sqlite_utils.cli:cli"
requires = ["setuptools>=77"]
build-backend = "setuptools.build_meta"

[tool.setuptools.packages.find]
include = ["sqlite_utils*"]

[tool.flake8]
max-line-length = 160
# Black compatibility, E203 whitespace before ':':
Expand Down
10 changes: 10 additions & 0 deletions sqlite_utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3149,6 +3149,7 @@ def _analyze(db, tables, columns, save, common_limit=10, no_most=False, no_least
)
def install(packages, upgrade, editable):
"""Install packages from PyPI into the same environment as sqlite-utils"""
_ensure_not_frozen_for_package_management()
args = ["pip", "install"]
if upgrade:
args += ["--upgrade"]
Expand All @@ -3164,10 +3165,19 @@ def install(packages, upgrade, editable):
@click.option("-y", "--yes", is_flag=True, help="Don't ask for confirmation")
def uninstall(packages, yes):
"""Uninstall Python packages from the sqlite-utils environment"""
_ensure_not_frozen_for_package_management()
sys.argv = ["pip", "uninstall"] + list(packages) + (["-y"] if yes else [])
run_module("pip", run_name="__main__")


def _ensure_not_frozen_for_package_management():
if getattr(sys, "frozen", False):
raise click.ClickException(
"Package installation and removal are not supported by the standalone "
"executable. Install plugins before building the executable instead."
)


def _generate_convert_help():
help = textwrap.dedent("""
Convert columns using Python code you supply. For example:
Expand Down
14 changes: 14 additions & 0 deletions tests/standalone_plugin/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "sqlite-utils-standalone-test-plugin"
version = "1.0"
requires-python = ">=3.10"

[project.entry-points.sqlite_utils]
standalone_test_plugin = "sqlite_utils_standalone_test_plugin"

[tool.setuptools]
py-modules = ["sqlite_utils_standalone_test_plugin"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import click
import sqlite_utils


@sqlite_utils.hookimpl
def register_commands(cli):
@cli.command()
def standalone_plugin_test():
click.echo("Bundled plugin works")
Loading
Loading