|
| 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