Python||Error: ModuleNotFoundError: No module named 'nltk'

Broken thoughts:

        Why is there this post? Generally, I will not write a post record process for errors such as No module named 'nltk', because pip install can usually be solved. But (everyone is afraid of a but), this error is not simple, because there are a series of errors later, it took me two or three hours to solve.

        What makes me feel outrageous is that the installation package is installed in the corresponding path and it is necessary to manually unzip the package, which is something I never expected. And at that time, I was thinking about solving the bug, so I didn't think about recording it, so in order to write this post, I restored the bug at that time, re-operated it again, and the road to death was opened, so I have this post. It's born~

        Shake your hands to celebrate hhhh

Error One

        The NLTK library is often used when working with textual data. Natural Language Toolkit (NLTK), a natural language processing toolkit, one of the most commonly used Python libraries in the field of NLP.

      So use the import nltk statement to import the library as usual, and the result is an error ModuleNotFoundError: No module named 'nltk'.

         When I saw No module named 'xxx' when I saw the error statement, I thought it was not a big problem. I opened the Anaconda Prompt interface and entered pip install nltk. The result showed that Requirement already satisfied: nltk in e:\anaconda3\lib\site-packages (3.2. 4) , the translation means that the nltk library has been installed, but I still get an error when I run import nltk again, what is going on? (confused face.jpg)

         Since pip install nltk doesn't work, try using conda install nltk! Then I waited for a long time without any response, the blogger I gave up. . .

        Later, I had a brainstorm, and when I recalled the previous operation, I didn't seem to have entered my current operating environment? ! (Blind students, I found that Huadian (⊙x⊙;) opened Anaconda Prompt and entered the Base environment by default, so I used the activate eve command line to enter my own operating environment eve (the environment created by myself), and then used pip install nltk to install, sure enough It worked.

        Yoshiya ヽ (✿ ゚ ▽ ゚) ノ


Error Two

        When I successfully imported the nltk library, I started to read the text I needed, removed the corresponding punctuation, and when I ran the code that was split by spaces, I reported an error 〒▽〒

#1.导入相关库
import nltk #
from nltk.corpus import stopwords #停用词
#2.读取所需文本数据
text = smsdata_data[2]
print('The original text is: ', text)

#3.去除标点符号
text2 = " ".join("".join([" " if ch in string.punctuation else ch
                          for ch in text]).split())
print('After removing punctuations, the text is: ', text2)

#4.按照空格进行分割
tokens = [word for sent in nltk.sent_tokenize(text2) for word 
              in nltk.word_tokenize(sent)]
print('The tokenized words are: ', tokens)

        The content of the error message is that the elder has grown T_T, Resource punkt not found.

         According to the error message, you need to run the code nltk.download('punkt') to download related packages. But I tried many times and it didn't work, I cried and cried (Tiger Tears.jpg)

         What is the reason? It is because nltk.download('punkt') takes a long time (it is said on the Internet that it takes four hours), so the connection is usually forcibly closed

        What should I do then? You can download the NLTK data file (about 638MB) online at https://github.com/nltk/nltk_data (friends who can connect to the external network can try it out); it will not connect to the external network or not. What about network students? You can try the installation package NLTK installation package I uploaded. rar-Python document class resource-CSDN download

        The specific steps are: 1. After downloading the compressed package of NLTK data from the Internet, store it in any location you remember on the CDE disk; 2. Unzip the compressed package and rename it as nltk_data; 3. Use the statements import nltk and nltk.download() Run it, an NLTK Downloader will appear, change the offline download directory (Download Directory) at the bottom of the pop-up window to the directory where nltk_data was installed before, choose to download all, and wait slowly.

 


Error Three

        The ideal is beautiful, the reality is cruel. According to the specific steps mentioned above, I thought I could solve the problem smoothly. However, my NLTK Downloader interface is like this (so the above picture is not mine, just for the convenience of demonstration from the Internet, I Failed to open o(TヘTo)

         WinError 10054: An existing connection was forcibly closed by the remote host! This problem is very similar to the previous one, and the reason for blind guessing is that there is no Internet connection. But my installation package is installed in the corresponding directory, why is it still not working?

        I have been stuck on this problem for a long time. After searching the Internet for a while, I decided to use the statement from nltk.book import * to try it. So turn off NLTK Downloader, enter the statement in the running interface, the result... (Silence is Cambridge tonight

         Seeing the content of the error, it seems familiar, comrades (っ°Д °;)っ

        After a lot of searching (actually it took almost an hour), I finally got the solution: unzip the compressed files in the taggers and tokenizers folders.

 

         God knows it will cause a series of messes due to a No module named 'nltk' error, but finally solved it.

        Hello, everyone celebrates together, rushing to tell each other, manual dog head, goodbye ヽ(✿゚▽゚)ノ

     (End of this post)

Guess you like

Origin blog.csdn.net/Inochigohan/article/details/121277325