ImportError: cannot import name 'UMAP' from 'umap' (unknown location)
I tried tutorial at bertopic website and this is only my code
from sentence_transformers import SentenceTransformer
from umap import UMAP
from hdbscan import HDBSCAN
from sklearn.feature_extraction.text import CountVectorizer
from bertopic.vectorizers import ClassTfidfTransformer
from bertopic import BERTopic
Every packages already installed separately especially the 'umap-learn' But got an error
ImportError Traceback (most recent call last)
Cell In [20], line 1
----> 1 from umap import UMAP
2 from hdbscan import HDBSCAN
3 from sklearn.feature_extraction.text import CountVectorizer
ImportError: cannot import name 'UMAP' from 'umap' (unknown location)
Environment Info Pycharm 2021.2.3 Python 3.8
There might be an issue with the environment in which you work. I would suggest starting from a completely fresh environment and re-installing BERTopic there. Moreover, make sure that you have no files in your working directory with the name umap.py.
My workaround for this is as follows:
I used this hint: https://github.com/resemble-ai/Resemblyzer/issues/6#issuecomment-701387312
Use import umap.umap_ as UMAP instead of from umap import UMAP
Then I had to change the UMAP to UMAP.UMAP in my code and in all pieces of code that came with bertopic:
umap_model = UMAP.UMAP(n_neighbors=15, n_components=5, min_dist=0.0, metric='cosine')
Instead of:
umap_model = UMAP(n_neighbors=15, n_components=5, min_dist=0.0, metric='cosine')
Due to inactivity, I'll be closing this issue for now. Feel free to reach out if you want to continue this discussion or re-open the issue!
Hello, I'm sad to re-open this issue.
So everything worked fine with my code and then suddenly the hdbscan was not working anymore, than I re-instaled all packages and now I have a problem with umap.
I did what was suggested here and in other foruns and uninstalled and re-installed both umap-learn and bertopic . I can import umap as import umap or import umap.umap_ as UMAP , the problem is when I import bertopic. I tried:
import bertopic
and
import umap.umap_ as UMAP
import bertopic
and
import umap
import bertopic
and
import umap
from bertopic import BERTopic
and finally:
import umap.umap_ as UMAP
from bertopic import BERTopic
In all situations, the problem occurs when I'm importing bertopic: ImportError: cannot import name 'UMAP' from 'umap' (unknown location) . I also reboot the machine a several times. I don't think that issue is related to the environment, because I have been using the same environment before when the same code was working: Python 3.10.7 and Visual Code Studio 1.74.3 . The bertopic is version 0.13.0 and umap-learn version 0.5.3
I found a solution at least for my case: despite the fact that I uninstalled umap-learn , there was still a umap folder in the package directory ( c:\python310\lib\site-packages ). I manually deleted the umap folder and later I just installed the bertopic. It works now!
@voigtjessica Glad to hear that you fixed the issue and thank you for posting that here! This will definitely help others experiencing the same issue.
I found a solution at least for my case: despite the fact that I uninstalled
umap-learn, there was still a umap folder in the package directory ( c:\python310\lib\site-packages ). I manually deleted the umap folder and later I just installed the bertopic. It works now!
This worked for me with the Apple M1 Pro running on Ventura 13.1 (22C65).
I have stumbled upon the same error. When using import umap.umap_ as umap, this seems to work fine. But I cannot use BERTopic because every time I import anything from bertopic, it gives me the error that it cannot import name 'UMAP' from 'umap'. I tried reinstalling and manually deleting the umap/bertopic folder, but the issue seems to be with bertopic. The error also only just appeared today. Yesterday it worked without any issue.
@MaartenGr hi ! i am facing this same issue & cant seem to figure a way out :( I tried @voigtjessica's workaround deleting the umap folder from site-packages(in Library/Python/3.9/lib/python/site-packages/) but unlike @aleckendall , didnt work for me on macbook air m1(os: ventura 13.3.1 , python 3.9.6 )
I was using umap in an earlier part of my code (pip3 install umap-learn).
There also i was getting this ImportError.
Some googling there led me to
import umap.umap_ as umap
which solved the umap import in my code. But how do i solve the import happening in BERTopic
@FraWagner were you able to find any resolution so far ?
@MaartenGr any suggestions/pointers/advice ?
For anyone stuck like me, I had to manually replace all from umap import UMAP and replace it with from umap.umap_ import UMAP in the bertopic python files.
umap version: umap-learn==0.5.3 bertopic==0.15.0
python 3.9
I am having a different issue related to importing UMAP.
from umap import UMAP
It works fine if I use a command prompt but it gets stuck when I use Spyder IDLE. Did anyone face this issue?
I am using Python 3.10 in a clean and new environment.
I installed as follows: pip install bertopic pip install spyder
Thanks
@trivikram18 Most likely, you are either using different environments between Spyder and the command prompt or you have a file in your current working directory that is called umap.py.
Hi Maarten,
Good day!
I re-checked and I am not having 'umap.py' file in my working directory. Is there any other folder where I should check for this file?
A wrong environment usage should give me a module not found error. My environment name is 'p310_btopic'
I am executing the code in interactive mode (running a selection of code).
In the status bar of the following screenshot, it shows the correct conda environment.:
In the Python interpreter selection from Spyder settings (shown in the following screenshot), I tried both the options "Default" as well as "Use the following Python interpreter":
When I interrupted the process it shows the following error, which seems to be a permission error. I went ahead and deleted the pycache folder and re-tried but no luck.
I started thinking if I installed it in the wrong location "C:\ProgramData\miniconda3", but then I realized that I am using the default location given by miniconda.
I had this working fine previously. I recently had to upgrade my laptop storage from 256 GB to 512 GB and had to re-install all software and programs. This is the background.
Thanks.
@trivikram18 Since you are working with miniconda, I would advise starting from a completely fresh environment. After the environment was created, you can check which packages are installed, which should be devoid of BERTopic-related dependencies such as numpy, numba, umap-learn, etc. Then, you can install BERTopic and it should work in that new environment.
Generally, whether it is this package or another, clean environments solve most of the problems I encounter.
@MaartenGr
I followed your suggestion and did a clean install in a new environment.
There was a permission error which was coming up as mentioned in my previous conversation.
I realised that somehow spyder has to be run as administrator always. This eventaully solved the issue.
Further I think my user id in windows has to be changed to have admin rights.
Thanks.
just write from.umap_ import UMAP in umap\init.py, and everything is ok