2x ModuleNotFoundError Jupyter notebook

ghz 昨天 ⋅ 1 views

While attempting to run jupyter notebook in python3.6.3 on my gnome terminal I get the following output, running on UBUNTU:

Traceback (most recent call last):
  File "/home/willem/.local/lib/python3.6/site-packages/notebook/services/sessions/sessionmanager.py", line 10, in <module>
     import sqlite3
  File "/usr/local/lib/python3.6/sqlite3/__init__.py", line 23, in <module>
     from sqlite3.dbapi2 import *
  File "/usr/local/lib/python3.6/sqlite3/dbapi2.py", line 27, in <module>
     from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/willem/.local/bin/jupyter-notebook", line 7, in <module>
     from notebook.notebookapp import main
  File "/home/willem/.local/lib/python3.6/site-packages/notebook/notebookapp.py", line 81, in <module>
     from .services.sessions.sessionmanager import SessionManager
  File "/home/willem/.local/lib/python3.6/site-packages/notebook/services/sessions/sessionmanager.py", line 13, in <module>
     from pysqlite2 import dbapi2 as sqlite3
ModuleNotFoundError: No module named 'pysqlite2'

However, the module sqlite3 is present in my python3.6.3 directory. Which makes the problem rather vague.

Any suggestions for this problem to be fixed will be greatly appreciated

Answer

The error you're encountering involves two main issues:

  1. ModuleNotFoundError: No module named '_sqlite3'
  2. ModuleNotFoundError: No module named 'pysqlite2'

Let's address these in steps:

1. Missing _sqlite3 module

The error No module named '_sqlite3' typically happens if SQLite3 was not properly compiled with Python when you installed it. This can happen when sqlite3 is missing or not compiled into Python due to missing dependencies during Python installation.

To resolve this, try the following steps:

Step 1: Install the necessary system dependencies

Make sure the necessary development libraries for SQLite and Python are installed. On Ubuntu, you can install them by running:

sudo apt-get update
sudo apt-get install -y build-essential libsqlite3-dev

These libraries are necessary for Python to properly compile and use SQLite3.

Step 2: Reinstall Python

Once the dependencies are installed, you need to ensure that Python is compiled with support for SQLite. The simplest way to do this is to reinstall Python 3.6.3 after installing the necessary development libraries. You can either reinstall it from source or use apt if you're okay with the default version of Python:

sudo apt-get install --reinstall python3.6

Step 3: Verify SQLite3 support

After reinstalling Python, run the following Python command to ensure that the _sqlite3 module is available:

import sqlite3
print(sqlite3.sqlite_version)

If this works without errors, then the SQLite3 module is properly installed and should work with Jupyter Notebook.

2. ModuleNotFoundError: No module named 'pysqlite2'

This error is related to an attempt by notebook to use pysqlite2 instead of sqlite3. pysqlite2 was the previous module for SQLite in Python, but in newer Python versions, it is generally replaced by sqlite3.

Step 1: Install pysqlite2 (if you need it)

In some rare cases, certain versions of notebook might be using the old pysqlite2. If you need to install pysqlite2 (though it's generally not needed in modern Python), you can try installing it via pip:

pip install pysqlite

This will install pysqlite2, which may resolve that error.

Step 2: Downgrade notebook package

If pysqlite2 is being incorrectly used in your environment, you may want to try downgrading the notebook package to a version that doesn't require pysqlite2. Run:

pip install notebook==5.7.8

This version of notebook should use sqlite3 and avoid the pysqlite2 issue.

3. Check Python Path and Jupyter Environment

Sometimes Jupyter Notebook might not be using the correct Python environment, which can lead to issues with missing modules. You can ensure that Jupyter is using the correct environment by running:

which python3
which jupyter-notebook

Ensure that they are pointing to the correct locations. If Jupyter is using a different environment or installation, you might need to reinstall it in the correct environment:

python3 -m pip install --user jupyter

Alternatively, you can install Jupyter in a virtual environment to avoid conflicts:

python3 -m venv jupyter_env
source jupyter_env/bin/activate
pip install jupyter

Summary of Steps:

  1. Install missing dependencies: sudo apt-get install libsqlite3-dev
  2. Reinstall Python 3.6.3 with SQLite support: sudo apt-get install --reinstall python3.6
  3. Install pysqlite2 (if required): pip install pysqlite
  4. Ensure Jupyter uses the correct Python environment and version.

By following these steps, you should be able to resolve the SQLite-related errors and get Jupyter Notebook running properly. Let me know if you need further help!