2

I am using Jupyter Notebook to run python code. I already did the following:

!pip install pyldavis 

I can successfully import pyLDAvis via the following codes:

import pyLDAvis
pyLDAvis.enable_notebook()

However, I cannot import pyLDAvis.sklearn via the following codes:

import pyLDAvis.sklearn

It returns:

ModuleNotFoundError Traceback (most recent call last) Cell In[52], line 1 ----> 1 import pyLDAvis.sklearn_models

ModuleNotFoundError: No module named 'pyLDAvis.sklearn_models'

Why is that and what should I do to deal with it?

Bella
  • 47
  • 4
  • Did you restart the kernel after the install? And for future reference, you are better off using the more modern version of your `pip install` command when running it in the notebook. It isn't the issue here since your main import works after. However, you'll have a better experience going forward and things will work in more places if you adopt current best practices and not outdated guidelines. That command would be `%pip install pyldavis` using the modern magic install command that insures installation occurs in the correct environment where the kernel is running that is underling the ... – Wayne Aug 04 '23 at 14:36
  • active notebook. See [here](https://stackoverflow.com/questions/28828917/error-importing-seaborn-module-in-python-importerror-cannot-import-name-utils/76347896#comment134631387_41925357) for more about the modern magic install commands. Use of the exclamation point in conjunction with the install commands can sometimes lead to issues. See the second paragraph [here](https://discourse.jupyter.org/t/location-of-libraries-or-extensions-installed-in-jupyterlab/16303/2?u=fomightez) for more about how the exclamation point use with installs can lead to issues sometimes. – Wayne Aug 04 '23 at 14:39
  • You should include details like version information of a package in your post. I think I found your same issue already posted about but I cannot be sure since you aren't providing the details. – Wayne Aug 04 '23 at 14:45
  • print(pyLDAvis.__version__): 3.4.0 ; print(sklearn.__version__): 1.3.0 – Bella Aug 04 '23 at 14:47
  • Yes, then the new approach I point out suits your version. – Wayne Aug 04 '23 at 14:50

1 Answers1

3

It looks like there has been a change in how the software handles this pattern.

This issue posted here in May of this year (2023) that looks to be the same as yours.

It links over to a solution that details how the use of the software has recently developed:

"pyLDAvis v 3.4.0 no longer has the file sklearn.py in the pip package." Replace any logic involving:

import pyLDAvis.sklearn
...
pyLDAvis.sklearn.prepare

"with"

import pyLDAvis.lda_model
...
pyLDAvis.lda_model.prepare

How this was found via troubleshooting:

I went to the page for the package in the Python Package Index (PyPI) and clicked on 'GitHub statistics:' on the left side under 'GitHub statistics:'. Then in the 'filters' slot I entered 'pyLDAvis.sklearn'.
The four that came up as open didn't look too similar to the OP, and so I clicked on the '7 closed' tag above the listing. The most recent one listed 'ModuleNotFoundError: No module named 'pyLDAvis.sklearn' looked to be a good match to this post, and so I examined it.

Wayne
  • 6,607
  • 8
  • 36
  • 93