Python Forum
nltk installs but cannot be imported [SOLVED]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nltk installs but cannot be imported [SOLVED]
#1
I installed nltk in my virtual environment, but importing does not work. Please advise.
Output:
(venv) mel@Raccoonland:~/Documents/Foreign Countries and Moving Abroad/Portugal/Language/O Organista$ [b]pip install nltk[/b] Collecting nltk Downloading nltk-3.9.1-py3-none-any.whl.metadata (2.9 kB) Collecting click (from nltk) Downloading click-8.2.1-py3-none-any.whl.metadata (2.5 kB) Collecting joblib (from nltk) Using cached joblib-1.5.1-py3-none-any.whl.metadata (5.6 kB) Collecting regex>=2021.8.3 (from nltk) Using cached regex-2024.11.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (40 kB) Collecting tqdm (from nltk) Using cached tqdm-4.67.1-py3-none-any.whl.metadata (57 kB) Downloading nltk-3.9.1-py3-none-any.whl (1.5 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.5/1.5 MB 5.8 MB/s eta 0:00:00 Using cached regex-2024.11.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (796 kB) Downloading click-8.2.1-py3-none-any.whl (102 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 102.2/102.2 kB 10.8 MB/s eta 0:00:00 Using cached joblib-1.5.1-py3-none-any.whl (307 kB) Using cached tqdm-4.67.1-py3-none-any.whl (78 kB) Installing collected packages: tqdm, regex, joblib, click, nltk [b]Successfully installed [/b]click-8.2.1 joblib-1.5.1 [b]nltk-3.9.1[/b] regex-2024.11.6 tqdm-4.67.1 (venv) mel@Raccoonland:~/Documents/Foreign Countries and Moving Abroad/Portugal/Language/O Organista$ ./add_EnglishPortuguese.sh Traceback (most recent call last): File "/home/mel/Documents/Foreign Countries and Moving Abroad/Portugal/Language/O Organista/./list_sentences.py", line 3, in <module> import nltk, sys ModuleNotFoundError: No module named 'nltk' Traceback (most recent call last): File "/home/mel/Documents/Foreign Countries and Moving Abroad/Portugal/Language/O Organista/./list_sentences.py", line 3, in <module> import nltk, sys ModuleNotFoundError: [b]No module named 'nltk'[/b]
Reply
#2
This was not working because the python file started with

#!/bin/python3
which doesn't work in the virtual environment.
Reply
#3
Thanks for pointing that out, sheepog. I ran into a similar issue before, the shebang line can cause some confusion with virtual environments. Changing it to #!/usr/bin/env python3 usually helps make sure the script uses the Python from the active venv. Glad you got it sorted!
Reply
#4
I'm pretty sure that if you eliminate the shebang entirely, when using a virtual environment, then the proper python will be used.
Correct me if I am wrong.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NLTK danandu 7 1,046 Oct-30-2025, 02:31 PM
Last Post: noisefloor
  get nltk data Pedroski55 7 11,475 Aug-12-2024, 06:16 AM
Last Post: Pedroski55
  pip installs in a different python version and directory than the default 3.11 ao_tex_mal 9 24,808 Jun-20-2023, 02:55 PM
Last Post: snippsat
  Can a module tell where it is being imported from? stevendaprano 3 2,820 Apr-12-2022, 12:46 AM
Last Post: stevendaprano
  Help with simple nltk Chatbot Extra 3 4,801 Jan-02-2022, 07:50 AM
Last Post: bepammoifoge
  module detecting if imported vs not Skaperen 1 2,643 Nov-19-2021, 07:43 AM
Last Post: Yoriz
  [newbie] Why is a module imported twice? Winfried 3 6,303 Apr-02-2021, 04:48 AM
Last Post: deanhystad
  Saving a download of stopwords (nltk) Drone4four 1 17,365 Nov-19-2020, 11:50 PM
Last Post: snippsat
  Installing nltk dependency Eshwar 0 3,245 Aug-30-2020, 06:10 PM
Last Post: Eshwar
  Module installs.. but it doesn't - need help updating module farroar 0 5,669 Oct-18-2019, 10:36 PM
Last Post: farroar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020