VPython 7.0.0 in Python 2.7 -- import fails from Jupyter notebook

54 views
Skip to first unread message

Jerome Fung

unread,
Jun 9, 2017, 10:32:07 AM6/9/17
to vpytho...@googlegroups.com, Samantha Hoang
Hi everyone,

I have a system running Ubuntu 16.04 with Python 2.7.13 and Jupyter notebook 4.3.0 installed via Anaconda (version 4.4.0, 64-bit). I successfully installed vpython this morning from Anaconda via

conda install -c vpython vpython

However, when I try importing vpython from a Jupyter notebook with

from vpython import *

I get a "SyntaxError: invalid syntax", and the code that is flagged is line 78 in site-packages/vpython/with_notebook.py. 

That line of code is defining a print method for the MISC class; I'm perplexed because it all looks like legitimate Python 2.7 syntax. Is it something I'm doing? Thanks in advance.

Best,
Jerome Fung


--
Jerome Fung
Lecturer in Physics
Wellesley College
Science Center E214

Bruce Sherwood

unread,
Jun 9, 2017, 11:39:38 AM6/9/17
to VPython-users, Samantha Hoang
That is indeed very odd. I just installed the new VPython 7 in Ubuntu 16.04 with Anaconda3 (Python 3.6), and it works. As a test, I suggest commenting out the MISC class and the GSprint function, which shouldn't really matter. That code is associated with useing GSprint() to print below a VPython canvas caption instead of to the standard print output location, and this works in VPython 7 outside a notebook but has never worked properly in the notebook case.

Jerome Fung

unread,
Jun 9, 2017, 12:34:59 PM6/9/17
to VPython-users, sho...@wellesley.edu
Hi Bruce,

Commenting out the MISC class and the GSprint function does appear to fix the issue -- thanks.

If it's working well for you in Python 3.6, it seems likely that this is a Python 2.7-related issue. The long-term answer for me is probably just to use a Python 3 conda environment.

Best,
Jerome

Bruce Sherwood

unread,
Jun 9, 2017, 12:38:48 PM6/9/17
to VPython-users, Samantha Hoang
The vpython module on Python 3.6 (and 3.5) has the further advantage that one can also use it outside a notebook if and when desired.

But there remains the question of why legal code is not accepted by Python 2.7. Really strange.

John

unread,
Jun 9, 2017, 3:27:55 PM6/9/17
to VPython-users, sho...@wellesley.edu
I see the same problem on on windows machine using python 2.7

import sys
print(sys.version)

2.7.12 |Anaconda custom (64-bit)| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)]

from vpython import *

produces this error.

  File "C:\Users\John\Anaconda2\lib\site-packages\vpython\with_notebook.py", line 78
    def print(self, s):

Bruce Sherwood

unread,
Jun 9, 2017, 9:06:51 PM6/9/17
to VPython-users, Samantha Hoang
Thanks for the reports. I too was able to reproduce the error. It seems to me to be a failing of Python 2.7 that "print" is apparently so reserved a word that it cannot be used as the name of a class method. I've change the name (and references to it) to "prnt", which fixes the problem. I've asked Matt Craig to create 7.0.1 installers.

Reply all
Reply to author
Forward
0 new messages