471,349 Members | 1,236 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

why script work in console window of PyCrust but not work when put it into function?

7
Hi,

I am a Python beginner and using PyCrust to process data for my research work.
I found I not able to import a .py file directly. But I have to type in every lines every time. Or I can save the script in function. Then call out the function in PyCrust. But it's not convenient. And it causes global variable problem.

The case is even I import the numpy lib in the function file.py, but it shows error: “NameError: global name 'append' is not defined” when I call out the function in Pycrust. Could anyone tell me why it shows "append" is not defined even I "import numpy" already?

And how to open the script directly in PyCrust?

Besides, how can I import and export the array with PyCrust ? (As I want to export the array from PyCrust and then run it in the script with SciTE)

Thanks for help.

P.S. The versions I use are:
enthon-python2.4-1.0.0
numpy-1.0.3.1.win32-py2.4


Kanis
Oct 8 '07 #1
6 3238
bartonc
6,596 Expert 4TB
Hi,

I am a Python beginner and using PyCrust to process data for my research work.
I found I not able to import a .py file directly. But I have to type in every lines every time. Or I can save the script in function. Then call out the function in PyCrust. But it's not convenient. And it causes global variable problem.

The case is even I import the numpy lib in the function file.py, but it shows error: “NameError: global name 'append' is not defined” when I call out the function in Pycrust. Could anyone tell me why it shows "append" is not defined even I "import numpy" already?

And how to open the script directly in PyCrust?

Besides, how can I import and export the array with PyCrust ? (As I want to export the array from PyCrust and then run it in the script with SciTE)

Thanks for help.

P.S. The versions I use are:
enthon-python2.4-1.0.0
numpy-1.0.3.1.win32-py2.4


Kanis
Please try using IDLE instead of PyCrust. IDLE will let you save your scripts and run them from the Run menu.
Oct 8 '07 #2
Kanis
7
Please try using IDLE instead of PyCrust. IDLE will let you save your scripts and run them from the Run menu.
Because the interface I using is embedded with PyCrust only. And the data is read in that interface. So if I use IDLE to process the data, I think I have to know how to export the array data first. Do u know how to import and export the array in Python?
Oct 8 '07 #3
bartonc
6,596 Expert 4TB
Because the interface I using is embedded with PyCrust only. And the data is read in that interface. So if I use IDLE to process the data, I think I have to know how to export the array data first. Do u know how to import and export the array in Python?
In that case, PyAlaMode will let you save your files.

Expand|Select|Wrap|Line Numbers
  1. >>> import numpy
  2. >>> a1darray = numpy.zeros(10)
  3. >>> a1darray
  4. array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
  5. >>> 
Oct 8 '07 #4
Kanis
7
In that case, PyAlaMode will let you save your files.

Expand|Select|Wrap|Line Numbers
  1. >>> import numpy
  2. >>> a1darray = numpy.zeros(10)
  3. >>> a1darray
  4. array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
  5. >>> 
The interface I use can create a data array called "dataobj" already, then if I want to process the data, I open the embedded "PyCrust" console and the "dataobj" is already defined there. But if I run the Python directly (I mean not use the PyCrust inside the interface), the "dataobj" array is undefined. So, I would like to export the "dataobj" array, let say export it to a file, and then I can read it again when I use the IDLE or other Python Shell or Matlab. How can I export the array into a file?
Oct 8 '07 #5
Kanis
7
In the console (PyCrust), the script works:

>>> def x():
... r = array([1,2,3])
... return r
...
>>> x()
array([1, 2, 3])
------------------------------------------
while I save it as a function named "f()"

import numpy
def f():
r = array([1,2,4])
return r


and call out the function in console, error arouse:

>>> from test_array import f
>>> f()
Traceback (most recent call last):
File "<input>", line 1, in ?
File "C:\Documents and Settings\wckan\My Documents\Python_script\test_array.py", line 3, in f
r = array([1,2,4])
NameError: global name 'array' is not defined

HOW CAN I SOLVED THIS PROBLEM?
Oct 8 '07 #6
bartonc
6,596 Expert 4TB
In the console (PyCrust), the script works:

>>> def x():
... r = array([1,2,3])
... return r
...
>>> x()
array([1, 2, 3])
------------------------------------------
while I save it as a function named "f()"

import numpy
def f():
r = array([1,2,4])
return r


and call out the function in console, error arouse:

>>> from test_array import f
>>> f()
Traceback (most recent call last):
File "<input>", line 1, in ?
File "C:\Documents and Settings\wckan\My Documents\Python_script\test_array.py", line 3, in f
r = array([1,2,4])
NameError: global name 'array' is not defined

HOW CAN I SOLVED THIS PROBLEM?
Expand|Select|Wrap|Line Numbers
  1.  r = numpy.array([1,2,4])
Oct 8 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Daniel Joyce | last post: by
8 posts views Thread by Arun Seetharam | last post: by
3 posts views Thread by niconedz | last post: by
1 post views Thread by Anthony | last post: by
10 posts views Thread by Stephany Young | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.