VPython Common Errors and Troubleshooting: Difference between revisions
No edit summary |
|||
Line 89: | Line 89: | ||
If you've taken linear algebra before, you're probably aware that multiplying vectors or matrices is very different from simply multiplying out scalars, and in fact, in this case, is undefined. The correct code here might have been: | If you've taken linear algebra before, you're probably aware that multiplying vectors or matrices is very different from simply multiplying out scalars, and in fact, in this case, is undefined. The correct code here might have been: | ||
E = oofpez * (qproton / (rmag**2)) * | E = oofpez * (qproton / (rmag**2)) * that | ||
Some other operations to look out for: | |||
* Adding or subtracting a scalar from a vector or vice-versa | |||
* Divining a scalar by a vector | |||
* Multiplying or dividing two vectors | |||
In VPython, there's no syntax to indicate that a variable is a vector. The easiest way to tell is look at where it was defined. For example, if we created the variable <code>position</code> with any of these lines of code, it would hold a vector: | |||
position = vector(5, 4, 3) | |||
position = (5, 4, 3) | |||
position = arrow.pos // arrow.pos is a vector property of the arrow object | |||
position = arrow.pos * 5 // vector multiplied by a scalar is a vector | |||
When tracing these errors, always look and make sure your variables hold the type of data you expect. When all else fails, you can use <code>print(variable name)</code> to examine the value. |
Revision as of 17:21, 3 December 2015
(Lucas Christian, page 1 of 2)
This reference lists out some common errors I've seen students encounter while working in VPython. Programming languages can often generate obscure and confusing error messages, and VPython is no exception.
System/Environment Errors
If your system is not set up correctly, or the environment you are running VPython code is incorrect, a number of mysterious errors can result.
VPython does not start or immediately freezes
Make sure you are running your code in VIDLE (not the identical-looking IDLE).
With the most current releases of VPython, code compiles and launches fine in IDLE, but the graphics window comes up grey and hangs. This is because of a bug in the way regular IDLE executes code, it has difficulty with programs that keep running in a loop, as VPython does so you can drag and zoom the display.
Note that if you double-click a .py file in Windows Explorer or Finder, it may open up in the regular IDLE instead of VIDLE. To check if your code is open in VIDLE, click the "Help" menu and look for the "VPython" menu item. Regular IDLE does not have a "VPython" item in the Help menu.
ImportError: No module named *
Traceback (most recent call last): File "/my/documents/folder/vectors.py", line 1 from future import division ImportError: No module named future
Python has only a small number of built-in functions. The rest are stored in "modules" that you can load when you need them. The from (blank) import (blank)
lines tell Python to import the named functions from a specific module. When you import a module, Python searches various folders on your computer to find the requested module. If it cannot find a module with that name, you get an ImportError.
Cause #1: You made a typo in the module name.
The basic boilerplate for VPython programs written in this class:
from __future__ import division from visual import *
Often missed are the two underscores ( _ _
) not separated by a space before and after future
. Check that this is correct, or copy-and-paste from above.
Cause #2: VPython is not installed correctly.
It is possible the module actually does not exist on your system. The VPython installer should install all the needed modules into the module folder for Python 2.7. If you have a different version of Python installed, it will not be able to find the modules. Make sure to follow the instructions on the VPython website, and install Python 2.7 from python.org as suggested.
NameError: name 'gdisplay' is not defined
This is often indicative of a missing import statement. Make sure your code begins with the boilerplate code above.
If you are using the graph functions, you will also need:
from visual.graph import *
"The function ‘CGContextErase’ is obsolete"
Users on Mac OS X may see this message printed whenever they run a VPython program:
The function ‘CGContextErase’ is obsolete and will be removed in an upcoming update. Unfortunately, this application, or a library it uses, is using this obsolete function, and is thereby contributing to an overall degradation of system performance.
This message can be ignored and will have no impact on the running of your program.
Syntax Errors
(work in progress; will cover common syntax errors)
Runtime Errors
Even if Python understands the syntax of your program, it can still contain errors that pop up when it tries to run.
TypeError: unsupported operand type(s) for *
Consider this error and the line of code that produced it:
Traceback (most recent call last): File "/my/documents/folder/lab2.py", line 32 E = oofpez * (qproton / (r**2)) * rhat TypeError: unsupported operand type(s) for ** or pow(): 'vector' and 'int'
The error indicates that pow()
, i.e. the exponent function, cannot operate on a vector
and an int
(an int is short for integer, a.k.a. a number/scalar).
r = vector(5, 4, 3) - particle.pos rmag = sqrt(r.x**2 + r.y**2 + r.z**2) rhat = r / rmag E = oofpez * (qproton / (r**2)) * rhat <-- ERROR
Looking at the code, we see that we typed r ** 2
, i.e. squaring the vector r, or as we'd write it out on paper:
[math]\displaystyle{ \vec{r}^2 = \vec{r} * \vec{r} = \lt 5, 4, 3\gt * \lt 5, 4, 3\gt }[/math]
If you've taken linear algebra before, you're probably aware that multiplying vectors or matrices is very different from simply multiplying out scalars, and in fact, in this case, is undefined. The correct code here might have been:
E = oofpez * (qproton / (rmag**2)) * that
Some other operations to look out for:
- Adding or subtracting a scalar from a vector or vice-versa
- Divining a scalar by a vector
- Multiplying or dividing two vectors
In VPython, there's no syntax to indicate that a variable is a vector. The easiest way to tell is look at where it was defined. For example, if we created the variable position
with any of these lines of code, it would hold a vector:
position = vector(5, 4, 3) position = (5, 4, 3) position = arrow.pos // arrow.pos is a vector property of the arrow object position = arrow.pos * 5 // vector multiplied by a scalar is a vector
When tracing these errors, always look and make sure your variables hold the type of data you expect. When all else fails, you can use print(variable name)
to examine the value.