Hi! I am trying to load a database and operate with it with sql on jupyter notebooks (I’m studying data science and it’s a requirement to do it this way.
I am following the instructions but it doesn’t seem to work:
!pip install ipython-sql sqlalchemy
%load_ext sql
%sql sqlite:///love4pets.db
The output of running this on jupyter is:
Requirement already satisfied: ipython-sql in /home/alberto/pysql/lib/python3.12/site-packages (0.5.0)
Requirement already satisfied: sqlalchemy in /home/alberto/pysql/lib/python3.12/site-packages (2.0.2)
… (many lines saying "requirement already satisfied)
The sql module is not an IPython extension.
UsageError: Line magic function `%sql` not found.
If the requirements are satisfied, why doesn’t it run?
The %pip
magic ensures the current environment gets updated. The !
magic does whatever the underlying shell feels like.
Hi! Thanks for replying! If I use %pip install ipython-sql sqlalchemy
I get this error: externally-managed-environment
:
error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try 'pacman -S
python-xyz', where xyz is the package you are trying to
install.
If you wish to install a non-Arch-packaged Python package,
create a virtual environment using 'python -m venv path/to/venv'.
Then use path/to/venv/bin/python and path/to/venv/bin/pip.
If you wish to install a non-Arch packaged Python application,
it may be easiest to use 'pipx install xyz', which will manage a
virtual environment for you. Make sure you have python-pipx
installed via pacman.
note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
Note: you may need to restart the kernel to use updated packages.
The sql module is not an IPython extension.
UsageError: Line magic function `%sql` not found.
But it is already installed in the environment used. So, why can’t I use it?
Having now established that ipython
was installed at the system level, the paths forward are either:
- use a system package manager to install the packages in question
- follow the concise guidance, by creating a virtual environment
- ideally, with all the packages (including
ipython
and packages of interest) captured up-front in a requirements.txt
sudo pip install
is a pretty bad idea, and can leave a system (which likely uses python
to do some system things) in a bad state.
Thanks for replying! I’m not using sudo
here precisely because of this. I created a fresh new environment for this and activated it with python -m venv pysql
and source pysql/bin/activate
.
Here is the pip list
result:
Package Version
------------------------- --------------
anyio 4.4.0
argon2-cffi 23.1.0
argon2-cffi-bindings 21.2.0
arrow 1.3.0
asttokens 2.4.1
async-lru 2.0.4
attrs 23.2.0
Babel 2.15.0
beautifulsoup4 4.12.3
bleach 6.1.0
certifi 2024.7.4
cffi 1.16.0
charset-normalizer 3.3.2
comm 0.2.2
debugpy 1.8.2
decorator 5.1.1
defusedxml 0.7.1
executing 2.0.1
fastjsonschema 2.20.0
fqdn 1.5.1
greenlet 3.0.3
h11 0.14.0
httpcore 1.0.5
httpx 0.27.0
idna 3.7
ipykernel 6.29.5
ipython 8.26.0
ipython-genutils 0.2.0
ipython-sql 0.5.0
ipywidgets 8.1.3
isoduration 20.11.0
jedi 0.19.1
Jinja2 3.1.4
json5 0.9.25
jsonpointer 3.0.0
jsonschema 4.23.0
jsonschema-specifications 2023.12.1
jupyter 1.0.0
jupyter_client 8.6.2
jupyter-console 6.6.3
jupyter_core 5.7.2
jupyter-events 0.10.0
jupyter-lsp 2.2.5
jupyter_server 2.14.2
jupyter_server_terminals 0.5.3
jupyterlab 4.2.3
jupyterlab_pygments 0.3.0
jupyterlab_server 2.27.2
jupyterlab_widgets 3.0.11
MarkupSafe 2.1.5
matplotlib-inline 0.1.7
mistune 3.0.2
nbclient 0.10.0
nbconvert 7.16.4
nbformat 5.10.4
nest-asyncio 1.6.0
notebook 7.2.1
notebook_shim 0.2.4
overrides 7.7.0
packaging 24.1
pandocfilters 1.5.1
parso 0.8.4
pexpect 4.9.0
pip 24.1.2
platformdirs 4.2.2
prettytable 3.10.2
prometheus_client 0.20.0
prompt_toolkit 3.0.47
psutil 6.0.0
ptyprocess 0.7.0
pure-eval 0.2.2
pycparser 2.22
Pygments 2.18.0
python-dateutil 2.9.0.post0
python-json-logger 2.0.7
PyYAML 6.0.1
pyzmq 26.0.3
qtconsole 5.5.2
QtPy 2.4.1
referencing 0.35.1
requests 2.32.3
rfc3339-validator 0.1.4
rfc3986-validator 0.1.1
rpds-py 0.19.0
Send2Trash 1.8.3
setuptools 70.3.0
six 1.16.0
sniffio 1.3.1
soupsieve 2.5
SQLAlchemy 2.0.31
sqlparse 0.5.0
stack-data 0.6.3
terminado 0.18.1
tinycss2 1.3.0
tornado 6.4.1
traitlets 5.14.3
types-python-dateutil 2.9.0.20240316
typing_extensions 4.12.2
uri-template 1.3.0
urllib3 2.2.2
wcwidth 0.2.13
webcolors 24.6.0
webencodings 0.5.1
websocket-client 1.8.0
widgetsnbextension 4.0.11
I restarted jupyter several times and tried to load sql but I still get the same errors. Although, I noticed a difference between using %load
and %reload
: