What happens when importing lamindb and the instance is not yet setup?

Hide code cell content
!lamin init --storage ./testsetup3
!lamin disconnect
 resetting django module variables
 initialized lamindb: testuser1/testsetup3
 disconnected instance: testuser1/testsetup3
# pip install lamindb
import lamindb as ln
import pytest
! not connected, call: ln.connect('account/name')

If you try to use lamindb, it will raise an CurrentInstanceNotConfigured and ask you to init or connect an instance via the python API.

Hide code cell content
with pytest.raises(ln.setup.errors.CurrentInstanceNotConfigured):
    ln.track()

Now let us init an instance:

ln.setup.init(storage="./testsetup")
 resetting django module variables
 initialized lamindb: testuser1/testsetup

Now we can access functionality:

ln.track()
 created Transform('bqq3W7Q3W7LJ0000'), started new Run('tiiK8NbV...') at 2025-09-14 14:14:59 UTC
 notebook imports: lamindb==1.11.0 pytest==8.4.2
 recommendation: to identify the notebook across renames, pass the uid: ln.track("bqq3W7Q3W7LJ")

Let us try to init another instance in the same Python session.

with pytest.raises(ln.setup.errors.CannotSwitchDefaultInstance) as error:
    ln.setup.init(storage="./testsetup2")
assert error.exconly().endswith(
    "Cannot init new instance after connecting to an existing instance."
)

Let us try connecting to another instance:

with pytest.raises(ln.setup.errors.CannotSwitchDefaultInstance) as error:
    ln.connect("testsetup3")
assert error.exconly().endswith(
    "Cannot switch default instance while `ln.track()` is live: call `ln.finish()`"
)

It works once we call ln.finish(), which sets the run context None:

ln.context._transform = None

However, it prints a warning about unintended side effects given the Python system cache is reset:

ln.connect("testsetup3")
 resetting django module variables
 connected lamindb: testuser1/testsetup3
assert ln.setup.settings.instance.name == "testsetup3"
!lamin delete --force testsetup
!lamin delete --force testsetup3
 deleting instance testuser1/testsetup
 deleting instance testuser1/testsetup3