GarethC GarethC -4 years ago 80
Python Question

Spyder not recognising imported terms

I'm using Spyder 3.1.2 (Python 2.7.13). As part of the startup, the IPython console is set to "Automatically load Pylab and NumPy modules".

When i want to use the method NumPy.zeros(shape), I can just call it in the IPython console as zeros:

In [12]: zeros(5)
Out[12]: array([ 0., 0., 0., 0., 0.])


My problem comes when trying to call the same zeros function from a separate python file.

If I create a function UseZeros() in a file Test1.py

"""
filename: Test1.py
"""

def UseZeros():
return zeros(4)


And I call it from a separate file, Test2.py:

"""
filename: Test2.py
"""

import Test1

testArr1 = zeros(5)
testArr2 = Test1.UseZeros()


In this script, testArr1 = zeros(5) works fine, but when calling Test1.UseZeros(), I get an error stating the global name 'zeros' is not defined.


File "C:/Users/Gareth/Test2.py", line 8, in
testArr2 = Test1.UseZeros()

File "Test1.py", line 7, in UseZeros
return zeros(4)

NameError: global name 'zeros' is not defined


Is somebody able to help my understand why when calling this function, the IPython console no longer recognizes the method?

The only reference I can find to a similar problem was fixed in a previous release, and was different to what I'm experiencing.

The reason I need this to work is that I'm importing functions used by somebody else who has made extensive use of commands such as zeros, ones, sin, cos etc., and I don't want to have to redefine everything.

Thanks

Answer Source

(Spyder developer here) The option to Automatically load Pylab and NumPy modules is provided to facilitate interactive work in the console but it's not meant to write code in the Editor because that code won't run outside Spyder.

I'm afraid to tell you, but what your colleague has done is a very bad practice and, in my humble opinion, I think the best you can do is to instruct him/her to fix this problem.

There's nothing we can do on the Spyder side to fix/improve this situation because we're not willing to promote bad practices, sorry.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download