Re: "import random" Odd Behavior on OSX install VPython v.3.1

Bruce Sherwood <[email protected]>
Newsgroups gmane.comp.python.visualpython.user
Message-ID <[email protected]>
No, import visual.graph isn't what you should use. It sounds like what
you want is probably import vis.graph, or possibly import vis.graph as
graph, so that you can refer to a graphing object such as gcurve as
graph.gcurve. I've again appended below the information on the first
page of the VPython documentation. When you import visual.graph,
you're importing a package that executes from visual import *, as the
documentation below points out. That in turn brings in a random
function, so visual.graph.random exists.

You'll probably also need to do things such as the following,
depending on your needs:

from vis import color # for vis.color.cyan; or import vis and refer to vis.color
from numpy import arange # or import numpy and refer to numpy.arange
from math import cos, exp # or import math and refer to math.cos and math.exp

If you're just trying to solve the problem of making sure you get the
random that you really want, you could just import that particular
function first, with its own name:

from wherever import random as MyRandom

As to the difference between 'import visual.graph' and 'from
visual.graph import *', in the first case you refer to gdots as
visual.graph.gdots, whereas in the latter case you refer to gdots
simply as gdots.

Bruce Sherwood

-------------------------------------

As a convenience to novice programmers to provide everything that is
needed to get started, the statement "from visual import *" imports
all of the Visual features and executes "from math import *" and "from
numpy import *". It also arranges that for routines common to both
math and numpy such as sqrt, the much faster math routine is used when
possible (when the argument is a scalar rather than an array).

If you want to import the visual objects selectively, import them from
the vis module. Two simple examples:

import vis
vis.box(color=vis.color.orange,material=vis.materials.wood)

from vis import (box, color, materials)
box(color=color.orange, material=materials.wood)

There are clean modules vis.controls, vis.filedialog, and vis.graph
equivalent to the modules visual.controls, visual.filedialog, and
visual.graph. The latter versions execute "from visual import *" and
are retained because some programs expect that behavior when importing
one of these modules.

The documentation is written assuming that "from visual import *" is used.

On Fri, May 27, 2011 at 1:40 PM, Eric Dick <[email protected]> wrote:
> The problem was with the next line of code.  I should have used
> 'import visual.graph' rather than 'from visual.graph import *'
> Can anyone explain what the difference between the two calls is?  Also why
> does visual.graph have visual.graph.random?
> The Problem
> _______________________________
> import random
> from visual.graph import *  #bad idea
> # imports random from numpy directory
> # visual.graph. does have a .random
> _______________________________
> Solution:
> import visual.graph

------------------------------------------------------------------------------
vRanger cuts backup time in half-while increasing security.
With the market-leading solution for virtual backup and recovery, 
you get blazing-fast, flexible, and affordable data protection.
Download your free trial now. 
http://p.sf.net/sfu/quest-d2dcopy1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.