[SciPy-user] Begginer's Problem

Michael Hearne mhearne at usgs.gov
Fri Oct 26 15:17:12 EDT 2007


Keith - You're right, it is a namespace problem.  One of the big  
differences between Matlab and NumPy is that in Matlab, every  
function in your path is automatically in every namespace (the global  
one or any functions that you may write).  In Python, by default the  
only functions you get are the Python "built-ins" (abs(), apply(),  
bool(), etc.)  To access the classes or functions defined in any  
other modules, be they part of the standard library or part of a  
third-party module like NumPy or SciPy, you _have_ to import those  
modules somehow.

That being said, I think  there is a way to organize your code that  
would make sense:

#fileopener.py
import scipy as S
import PIL.Image as I
def imageopen(filename):
	im1 = I.open(filename)
	ar1 = S.ones((20,20))
	return (im1,ar1)

#main.py
from fileopener import imageopen
image,array = imageopen(filename)
... #do whatever it is you want to do with the data

--Mike
On Oct 26, 2007, at 1:03 PM, Keith Suda-Cederquist wrote:

> Hi,
>
> I started using SciPy recently (converted from Matlab) and am  
> running into a problem.
>
> I wrote my first script that uses PIL.Image module and several  
> basic SciPy modules.  Within this script I declared functions that  
> used functions in the PIL.Image and scipy modules, for example:
>
>
> #version1.py
> import scipy as S
> import PIL.Image as I
>
> def imageopen(filename):
>     im1=I.open(filename)
>     ar1=S.ones((20,20))
>     ....
>
> im2=imageopen(file2)
>
> This worked out fine.  Then I decided it would be nice to move all  
> the function definitions into a seperate file:
>
> #split1.py
> def imageopen(filename):
>     im1=I.open(filename)
>     ar1=S.ones((20,20))
>
> #split2.py
> import scipy as S
> import PIL.Image as I
> import split1
>
> im2=imageopen(file2)
>
> This new structure is giving me a lot of problems.  The usual error  
> I get is that global name 'I' or 'S' is not defined.  I've searched  
> quite a bit for a solution, but haven't figured it out yet.
>
> As far as I can tell the problem is related to the namespace  
> belonging to particular modules.
>
> I've been able to work around this by importing the scipy and  
> PIL.Image modules all over the place (i.e. at the begging of  
> split1.py and split2.py and inside the function).  However, this  
> doesn't seem like the best way to handle this.
>
> Any advice?
>
> Thanks in advance for your help.
>
> -Keith
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user




------------------------------------------------------
Michael Hearne
mhearne at usgs.gov
(303) 273-8620
USGS National Earthquake Information Center
1711 Illinois St. Golden CO 80401
Senior Software Engineer
Synergetics, Inc.
------------------------------------------------------


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20071026/ff402805/attachment.html>


More information about the SciPy-User mailing list