Skip to content

Commit 4e01af0

Browse files
authored
Document how to import sqliteimport and load a database (#114)
2 parents c247c1a + 032f438 commit 4e01af0

File tree

7 files changed

+184
-37
lines changed

7 files changed

+184
-37
lines changed

.pre-commit-config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,11 @@ repos:
7474
rev: "v1.0.0"
7575
hooks:
7676
- id: "verify-consistent-pyproject-toml-python-requirements"
77+
78+
- repo: "https://github.com/shellcheck-py/shellcheck-py"
79+
rev: "v0.11.0.1"
80+
hooks:
81+
- id: "shellcheck"
82+
args: [
83+
"-e", "SC1091", # Ignore non-existent 'source ...' targets.
84+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Documentation
2+
-------------
3+
4+
* Document how to import sqliteimport and load a database of packages.
5+
6+
This now includes runnable scripts showing fully-automated loading
7+
using ``sitecustomize.py`` and the ``PYTHONPATH`` environment variable.
8+
In addition, more information is provided
9+
for manually importing sqliteimport and loading a database.

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Table of contents
5555
.. toctree::
5656
:maxdepth: 1
5757

58-
load
58+
load/index
5959
bytecode
6060
flake8/index
6161
isort/index

docs/load.rst

Lines changed: 0 additions & 36 deletions
This file was deleted.

docs/load/assets/automatic.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Install sqliteimport in a virtual environment.
2+
python -m venv demo-venv
3+
& ./demo-venv/bin/Activate.ps1
4+
python -m pip install 'sqliteimport[cli]'
5+
6+
# Install flake8 and bundle it into a database.
7+
python -m pip install flake8 --target flake8/
8+
sqliteimport bundle flake8/ flake8.sqlite3
9+
Remove-Item -Recurse -Force flake8/
10+
11+
# Create a `sitecustomize.py` file.
12+
$site_packages_directory=python -c 'import site; print(site.getsitepackages()[0])'
13+
Write-Host "Creating a sitecustomize.py file in ${site_packages_directory}"
14+
Write-Output 'import sqliteimport' > "${site_packages_directory}/sitecustomize.py"
15+
16+
# Point PYTHONPATH at the database.
17+
$env:PYTHONPATH="flake8.sqlite3"
18+
19+
# Run flake8, and show where it's imported from.
20+
python -m flake8 --version
21+
python -c 'import flake8; print(f"Imported flake8 from {flake8.__file__!r}")'

docs/load/assets/automatic.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
# Install sqliteimport in a virtual environment.
3+
python -m venv demo-venv
4+
source demo-venv/bin/activate
5+
python -m pip install sqliteimport[cli]
6+
7+
# Install flake8 and bundle it into a database.
8+
python -m pip install flake8 --target flake8/
9+
sqliteimport bundle flake8/ flake8.sqlite3
10+
rm -rf flake8/
11+
12+
# Create a `sitecustomize.py` file.
13+
site_packages_directory="$(python -c 'import site; print(site.getsitepackages()[0])')"
14+
echo "Creating a sitecustomize.py file in ${site_packages_directory}"
15+
echo 'import sqliteimport' > "${site_packages_directory}/sitecustomize.py"
16+
17+
# Point PYTHONPATH at the database.
18+
export PYTHONPATH="flake8.sqlite3"
19+
20+
# Run flake8, and show where it's imported from.
21+
python -m flake8 --version
22+
python -c 'import flake8; print(f"Imported flake8 from {flake8.__file__!r}")'

docs/load/index.rst

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
..
2+
This file is a part of sqliteimport <https://github.com/kurtmckee/sqliteimport>
3+
Copyright 2024-2025 Kurt McKee <[email protected]>
4+
SPDX-License-Identifier: MIT
5+
6+
7+
Loading sqliteimport
8+
####################
9+
10+
sqliteimport must be imported and configured
11+
before packages stored in sqlite can be imported.
12+
There are two considerations:
13+
14+
#. When and where sqliteimport is *imported*
15+
#. How the path to the sqlite database is *configured*
16+
17+
Table of contents
18+
=================
19+
20+
* :ref:`automatic`
21+
* :ref:`manual-import`
22+
* :ref:`manual-load`
23+
24+
25+
.. _automatic:
26+
27+
Automatic everything
28+
====================
29+
30+
The simplest way to import sqliteimport and configure a database of packages
31+
is to create a custom ``sitecustomize`` module
32+
and set the ``PYTHONPATH`` environment variable.
33+
34+
This technique allows a database of packages to be used
35+
*without modifying any application code*.
36+
37+
.. code-block:: python
38+
:caption: ``sitecustomize.py``
39+
40+
import sqliteimport
41+
42+
.. code-block:: bash
43+
:caption: Linux/macOS
44+
45+
export PYTHONPATH='path/to/packages.sqlite3'
46+
47+
.. code-block:: powershell
48+
:caption: Windows Powershell
49+
50+
$env:PYTHONPATH='path/to/packages.sqlite3'
51+
52+
.. note::
53+
54+
``sitecustomize.py`` must be in a directory that Python's ``site`` module
55+
automatically searches.
56+
Please read the `Python site module`_ documentation for full details.
57+
58+
.. warning::
59+
60+
This technique may fail under some circumstances.
61+
62+
For example, Python interpreters installed by Homebrew
63+
include a custom ``sitecustomize`` that will be found and used
64+
before any that are created in a virtual environment.
65+
66+
67+
Examples
68+
--------
69+
70+
The examples below demonstrate how to use a ``sitecustomize.py`` file
71+
with a ``PYTHONPATH`` environment variable.
72+
73+
.. literalinclude:: assets/automatic.sh
74+
:caption: Linux/macOS
75+
:language: bash
76+
:lines: 2-
77+
78+
.. literalinclude:: assets/automatic.ps1
79+
:caption: Windows Powershell
80+
:language: powershell
81+
82+
83+
84+
.. _manual-import:
85+
86+
Manual imports
87+
==============
88+
89+
In some circumstances it may be helpful, or even necessary,
90+
to import sqliteimport in your application code.
91+
The obvious requirement is that sqliteimport must be imported
92+
before any packages that are bundled in a sqlite database.
93+
94+
Linters that sort Python imports may move ``import sqliteimport``
95+
and prevent your code from executing.
96+
See the :doc:`../isort/index` and :doc:`../ruff/index` pages
97+
for suggestions how to configure those linters.
98+
99+
100+
.. _manual-load:
101+
102+
Manual database loading
103+
=======================
104+
105+
If you cannot configure or control the ``PYTHONPATH`` environment variable
106+
to point to a sqlite database, you'll need to load a database manually.
107+
108+
This is accomplished by calling ``sqliteimport.load()`` with a path
109+
to the database, represented as either a string or ``pathlib.Path`` instance.
110+
111+
.. code-block:: python
112+
113+
import sqliteimport
114+
115+
sqliteimport.load("path/to/packages.sqlite3")
116+
117+
import example_package_from_database
118+
119+
120+
.. Links
121+
.. -----
122+
..
123+
.. _Python site module: https://docs.python.org/3/library/site.html

0 commit comments

Comments
 (0)