简体   繁体   中英

jupyter notebook python version 2.7.13 (print invalid syntax error)

Hi I have a Jupyter Notebook, with python 2.7.13 and it worked fine all along. But starting today it shows strange behaviour.

when I do this in a fresh notebook Python 2:

print 'hello'

it returns this:

  File "<ipython-input-1-bfbe230352b8>", line 1
    print 'hello'
                ^
SyntaxError: invalid syntax

It was working ok till yesterday and today I restarted the computer and this happens.

Any reasons? How to fix?

I have checked anaconda, spyder, command prompt... everywhere else is ok with 2.7.13

print ('hello') works but I don't want this.. all my code is in Python 2.

在此输入图像描述

I have now tracked the problem to be caused by the extension. Once I rename this file C:\\Users\\X\\.jupyter\\nbconfig\\notebook.json to something else and reopen jupyter notebook, the print works as expected.

However, I do need the extensions at least the key ones I use regularly. I don't know which one to disable. I didn't know how to track the culprit without going through each one by one. Also, as far as I can remember, all the extensions I have been using for few weeks already without any issues.

These are the contents of the notebook.json file.

{
  "load_extensions": {
    "toc2/main": true, 
    "Runtools": true, 
    "collapsible_headings/main": true, 
    "codefolding/main": true, 
    "varInspector/main": true, 
    "runtools/main": true, 
    "runtools": true
  }, 
  "collapsible_headings": {
    "add_button": true, 
    "add_insert_header_buttons": true
  }
}

After trial and error with disabling one every time, restart and check.. I have now solved the problem by setting this line to false, it now works. The problem is with variable inspector extension.

"varInspector/main": false, 

Sometimes the jupyter required a restart and sometime it didn't. Just opening the .ipynb file was enough to test.

I have the same extension running in my office computer, which has no issues.

If someone could really put in the comments for the cause and a solution for permanent fix of this problem will be great.

Could you please try with reloading sys like below ?

import sys
reload(sys)
print 'hello'

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