0

I am coding on Python 2.7. I want a large list of words to have access to. Looking around, I have found that nltk has what I'm looking for. However, every time I try and install it I get a syntax error. I have tried doing the commands in the shell and in files. I have no true understanding of how pip, install, and download commands work. I am on a mac, which other threads have said could affect things. I have tried...

sudo pip2 install nltk

Which gives:

SyntaxError: invalid syntax

When importing, I get

import nltk

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    import nltk
  ModuleNotFoundError: No module named 'nltk'

nltk.download

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    nltk.download
NameError: name 'nltk' is not defined

and a few other suggestions from other threads, but nothing works. Please help.

alvas
  • 115,346
  • 109
  • 446
  • 738
  • 3
    It looks like you're trying to run `pip install` in the python interpreter? `exit()` the interpreter and try. Also, I wasn't sure there was a `pip2`, thought it was just `pip` for P2.7 and `pip3` otherwise? – roganjosh Feb 27 '18 at 21:25
  • On a mac, open terminal, and type `pip install nltk` (not in python). Then, in your python interpreter, try `import nltk` – sacuL Feb 27 '18 at 21:25
  • Have you ensured that you have pip installed? https://stackoverflow.com/questions/17271319/how-do-i-install-pip-on-macos-or-os-x – Brett Holman Feb 27 '18 at 21:26
  • Also, see http://docs.python-guide.org/en/latest/dev/virtualenvs/ – alvas Feb 28 '18 at 09:54
  • Possible duplicate of [Why does "pip install" inside Python raise a SyntaxError?](https://stackoverflow.com/questions/8548030/why-does-pip-install-inside-python-raise-a-syntaxerror) – phd Feb 28 '18 at 12:30

2 Answers2

1

pip is your python package manager. It is a command line tool, and not a python function, object or method. This means you can't call pip from within python (at least not by just typing pip ... into a python interpreter). pip should come along with your installation of python 2.7, so you don't need to install it, as long as you have python installed.

You need to call pip from your command line (on a mac, this would most likely be from terminal). So you need to open your terminal, type pip install nltk, which should install your package.

Then, you can start python by using the command python in terminal. You can then import nltk using import nltk.

Only once you've followed those steps, and successfully installed and imported the nltk package, can you use nltk.download() to download nltk data. nltk.download() in itself has nothing to do with installing the package.

I would recommend following a python tutorial, such as the one linked, in order to gain an understanding of how to use the python interpreter. This should explain how to install packages, and use basic python functionality.

sacuL
  • 49,704
  • 8
  • 81
  • 106
0

Most versions of Mac OS come with Python version 2.7, but not with pip. First verify that you have pip installed from the command line:

pip -V

If pip is not installed, follow the instructions here: How do I install pip on macOS or OS X?

Then, directly in the terminal type (not in the python interpreter)

pip install nltk

Then, open your python interpreter from the command line:

python

and in the python interpreter, try importing nltk

import nltk

Brett Holman
  • 743
  • 6
  • 18