15

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!

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
user1933275
  • 481
  • 1
  • 5
  • 9
  • 2
    You may be using pip from a different Python installation to the one you're running the notebook in. Check `sys.executable` in a notebook to find the Python you're running, and then use `path/to/python -m pip install ...` to install things for that. – Thomas K May 11 '16 at 10:24
  • Add the output from `which -a python` and `which -a pip` – Padraic Cunningham May 11 '16 at 10:26
  • I switched to jupyter to installed with conda. same problem. $ which -a python `/home/hftbot/anaconda3/bin/python /usr/bin/python` – user1933275 May 13 '16 at 23:35
  • I switched to jupyter to installed with conda. same problem. $ which -a python `/home/hftbot/anaconda3/bin/python /usr/bin/python` should I mix pip into this or is there a way to do it with conda? – user1933275 May 13 '16 at 23:41
  • used conda to install mysql, pymysql. no effect. – user1933275 May 14 '16 at 15:25
  • If using conda, make sure you have jupyter installed in your conda environment, otherwise it will use the conda system one – qbert65536 Aug 10 '22 at 01:24

5 Answers5

6

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.

Ashkan
  • 1,643
  • 5
  • 23
  • 45
5

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

KHAN
  • 537
  • 5
  • 12
  • I had the same problem while doing the IBM DB2 Coursera assignment. The problem got solved when I replace the line starting from ! with line starting with %. I commented the line starting from ! too. #!pip install ipython-sql %pip install ipython-sql – Emil Jun 29 '22 at 12:55
1

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

Fredrik HD
  • 105
  • 6
0

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.

David Jung
  • 376
  • 5
  • 8
0

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.

Azhar Khan
  • 3,829
  • 11
  • 26
  • 32