简体   繁体   中英

Trouble Installing spaCy english model in python 2.7? And upgrading python to 3.5?

I am trying to install the spaCy english model on my mac after installing the program. Right now my machine has python 2.7. I have installed spaCy in the venv then followed that with "python -m spacy.en.download" to install the model as instructed on the website. When I try to do that I get the following in response:

$ python -m spacy.en.download

Traceback (most recent call last):

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/Users/katietemrowsky/Documents/Desktop/machine/.env/lib/python2.7/site-packages/spacy/en/download.py", line 1, in 
    import plac
  File "/Users/katietemrowsky/Documents/Desktop/machine/.env/lib/python2.7/site-packages/plac.py", line 38, in 
    from plac_tk import TkMonitor
  File "/Users/katietemrowsky/Documents/Desktop/machine/.env/lib/python2.7/site-packages/plac_tk.py", line 46
    print('Process %d killed by CTRL-C' % os.getpid(), file=sys.stderr)
                                                           ^
SyntaxError: invalid syntax

I then tried to install spaCy and the model on my computer outside of the venv which I would rather not do, but wanted to see if it would work. Again I got the same error.

Additionally I am wondering if the issue has something to do with running python 2.7? I upgraded my python on my computer to 3.5 but am not sure how to replace 2.7 with 3.5? Right now I can run both on the interpreter using $ python or $ python3 . How can I upgrade everything to 3.5?

Thank you in advance!

In short:

The latest version of spacy and plac doesn't have this issue anymore.

Upgrade your spacy version, it should automatically upgrade plac too:

pip install -U spacy

In long:

In the latest version of spacy , the import plac line is no longer in the spacy.en.download.py but the plac library is used in other places

plac is an argument parser like the native argparse or the popular docopt .

  File "/Users/katietemrowsky/Documents/Desktop/machine/.env/lib/python2.7/site-packages/plac_tk.py", line 46
    print('Process %d killed by CTRL-C' % os.getpid(), file=sys.stderr)
                                                           ^
SyntaxError: invalid syntax

The error you've occurred above is caused by the difference between the print_function syntax of Python2 and Python3, ie:

alvas@ubi:~$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print('foo bar', file=sys.stderr)
  File "<stdin>", line 1
    print('foo bar', file=sys.stderr)
                         ^
SyntaxError: invalid syntax
>>> exit()
alvas@ubi:~$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print('foo bar', file=sys.stderr)
foo bar

And this was introduced by this commit .

Since there is the from __future__ import print_function at https://github.com/micheles/plac/blob/46d8d393fbca8820e5cba5d1da808b65a1c879a3/plac_tk.py#L1

The print_function should have kicked in and allow the file= parameters in the print for Python2, eg

alvas@ubi:~$ python2
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import print_function
>>> import sys
>>> print('foo bar', file=sys.stderr)
foo bar

But the __future__ import from plac didn't kick in and that remains a mystery to me =( But that's another answer for another question at another time...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM