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.

5
  • 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? Commented Feb 27, 2018 at 21:25
  • On a mac, open terminal, and type pip install nltk (not in python). Then, in your python interpreter, try import nltk Commented Feb 27, 2018 at 21:25
  • Have you ensured that you have pip installed? stackoverflow.com/questions/17271319/… Commented Feb 27, 2018 at 21:26
  • Also, see docs.python-guide.org/en/latest/dev/virtualenvs Commented Feb 28, 2018 at 9:54
  • Possible duplicate of Why does "pip install" inside Python raise a SyntaxError? Commented Feb 28, 2018 at 12:30

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That makes sense. Thank you very much. I am totally green and needed a breakdown. Thank you for including the tutorial as well.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.