IPython Notebook and SQL: 'ImportError: No module named sql' when running '%load_ext sql'

29.7k Views Asked by At

Just set up an IPython Notebook on Ubuntu 16.04 but I can't use %load_ext sql. I get: ImportError: No module named sql

I've tried using pip and pip3 with and without sudo to install ipython-sql. All 4 times it installed without issue but nothing changes on the notebook.

Thanks in advance!

5

There are 5 best solutions below

1
Ashkan On

I know it's been a long time, but I faced the same issue, and Thomas' advice solved my problem. Just outlining what I did here.

When I ran sys.executable in the notebook I saw /usr/bin/python2, while the pip I used to install the package was /usr/local/bin/pip (to find out what pip you are using, just do which pip or sudo which pip if you are installing packages system-wide). So I reinstalled ipython-sql using the following command, and everything worked out just fine.

sudo -H /usr/bin/python2 -m pip install ipython-sql

This is odd since I always install my packages using pip. I'm wondering maybe there's something special about the magic functions in Jupyter.

0
David Jung On

I doubt you're using different IPython Notebook kernel other than which you've installed ipython-sql in.
IPython Notebook can have more than one kernel. If it is the case, make sure you're in the right place first.

1
Han Parlak On

If you're trying to connect the IBM database and came across this problem and the above solutions couldn't do it for you, you could give this a chance. (By the way, this error usually means one of your package installations doesn't meet the requirements or more probably: you're in the wrong kernel/virtual environment and the Jupyter instance can't run your command from the specified packages.)

From JupyterLab or Jupyter Notebook go to Kernel>Change Kernel and change the kernel that you've installed the packages. Wait for it to establish a connection. Then use 0, 0 to restart kernel (or Kernel>Restart Kernel

Go to any cell and run the below commands to install packages in the current kernel.

!pip install sqlalchemy==1.3.9
!pip install ibm_db_sa
!pip install ipython-sql

Now try %load_ext sql

0
Fredrik HD On

I know this answer will be (very) late to contribute to the discussion but maybe it will help someone. I found out what worked for me by following Thomas, who commented above. However, with a bit of a caveat, that I was using pyenv to setup and manage python on my local machine.

So when running sys.executable in a jupyter notebook cell I found out my python path was /usr/local/Cellar/jupyterlab/3.2.8/libexec/bin/python3.9, while I expected it to be somewhere along the lines of '/Users/<USER_NAME>/.pyenv/versions/3.9.2/bin/python'.

This error was attributed to me having installed jupyter through command brew install jupyter instead of pyenv exec pip install jupyter. I proceeded to uninstall jupyter with brew and then executing the second command, which now got jupyter up and running!

(note that you would first have to have pyenv setup properly).

0
Meelad Ghazipour On

Wherever you installed jupyter, is where jupyter loads exts. You are using the jupyter installed outside of virtual environment, then activated virtual environment, then installed ipython-sql, then trying to load_ext from different level.

Just deactivate the virtual environment, install ipython-sql, and then activate the environment.

If you want to install ipython-sql only in the virtual environment, then at least you need to install jupyter inside the virtual environment too. Both puppies should be in the same level.