0

I am trying to execute this code in pycharm

import nltk

sentence = """ we all are gonna die"""

tokens = nltk.word_tokenize(sentence)

print(tokens)

But I keep getting this error

/Users/yoshithKotla/pythonProject/bin/python /Users/yoshithKotla/Desktop/pythonProject/main.py

Traceback (most recent call last):

  File "/Users/yoshithKotla/Desktop/pythonProject/main.py", line 1, in <module>
ModuleNotFoundError: No module named 'nltk'

But I am able to run the same code using terminal

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Yoshith Kotla
  • 135
  • 1
  • 3
  • 13
  • 2
    Check what enviroment pycharm is running – Chris Doyle Apr 05 '21 at 15:19
  • 1
    PyCharm doesn't automatically include all packages for all projects, you have to go to the project settings and install the packages you want available: https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgrading-packages.html have you done that yet? – Random Davis Apr 05 '21 at 15:20
  • 1
    Note the line `/Users/yoshithKotla/pythonProject/bin/python /Users/yoshithKotla/Desktop/pythonProject/main.py`. This tells you that the python executable being used is located within the same directory as the project, and is probably a virtual environment. You have installed the module globally, but not in this virtual environment. – Oli Apr 05 '21 at 15:21
  • how to install the module in virtual environment – Yoshith Kotla Apr 05 '21 at 15:23
  • The question lacks debugging details. It doesn't give enough information about how `nltk` is installed. If you are using a venv, or another virtual environment, if you activated that environment and how you are executing the code. see [Install, uninstall, and upgrade packages](https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgrading-packages.html). Running on the terminal you have to [activate the venv](https://stackoverflow.com/a/24720790) – bad_coder Apr 06 '21 at 13:32

2 Answers2

0

Make sure the interpreter PyCharm uses is the same as you are using in your terminal. ( Run > Run... > Edit configurations > Python Interpreter )

Choubada
  • 15
  • 4
  • yes its python 3.9 on pycharm same as terminal – Yoshith Kotla Apr 05 '21 at 15:23
  • 1
    @YoshithKotla Same version does not imply that it's the same interpreter. You need to check the interpreter's path. PyCharm usually creates a new interpreter using a virtual environment (see `venv` module) for each project. There are separate from the system Python installation. – Brian61354270 Apr 05 '21 at 15:35
0

The nltk module is installed globally and the python project has no access to it. Changing that setting will solve the problem :)

Yoshith Kotla
  • 135
  • 1
  • 3
  • 13