Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python doesn't see packages with Jupyter Notebook

I use Jupyter Notebook with a virtual environment. I have a dependency installed, but can't import it:

cell 1:
!pip3 install sent2vec

Requirement already satisfied: sent2vec in 
venv/lib/python3.7/site-packages (0.0.0)

cell 2:
import sent2vec

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-5-06231d291a17> in <module>
----> 1 import sent2vec

ModuleNotFoundError: No module named 'sent2vec'

How this can happen? How to fix this?

> pip3 list
Package      Version  
------------ ---------
certifi      2019.9.11
chardet      3.0.4    
Cython       0.29.14  
idna         2.8      
joblib       0.14.0   
langdetect   1.0.7    
nltk         3.4.4    
numpy        1.17.1   
pip          19.3.1   
requests     2.22.0   
scikit-learn 0.21.3   
scipy        1.3.2    
sent2vec     0.0.0    
setuptools   41.6.0   
six          1.13.0   
urllib3      1.25.7   
wheel        0.33.6
like image 870
evaleria Avatar asked Mar 03 '23 04:03

evaleria


1 Answers

You'll note that jupyter is not listed in your installed packages. That means you're running it from a different virtual environment. As I mentioned in the comment responding to your question, you can run which jupyter to find out where your Jupyter Notebook application is being run from (assuming you're on a *NIX system); in this case, it won't be from the python3.7 virtual environment that shows up in your first code block.

To resolve the issue, you simply need to run pip3 install jupyter, then retry running jupyter notebook.

Alternatively, you can add your virtual environment as a kernel so that it can be selected when you're running Jupyter from your original environment. To do this, you would run (assuming pip is connected to your original environment):

pip install ipykernel
ipython kernel install --user --name=<insert name of your venv>

You should then be able to select that venv as a kernel on new notebooks. (Source for info on venv activation in Jupyter).

like image 55
PMende Avatar answered Mar 12 '23 16:03

PMende