w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
import libraries into python scrapy shell.

When you do numpy.random.random(n) Python looks for numpy name in local variables, then if it was not found, it looks for it in global variables.

It's clear that if you haven't imported numpy there is no such name:

NameError: global name 'numpy' is not defined 

If you do

def foo(n) :import numpy ; return numpy.random.random(n)

you import numpy module and keep a reference to it in the local (inside function foo) variable numpy. So your call numpy.random.random(n) works fine.

If you import numpy into global namespace, it will also work:

import numpy
def foo(n):
    return numpy.random.random(n)

Ensure that you you did import numpy, because everything works fine here:

In [1]: import numpy

In [2]: numpy.random.random(2)
Out[2]: array([ 0.05183811,  0.18187864])

In [3]: def foo(n): return numpy.random.random(n)

In [4]: foo(2)
Out[4]: array([ 0.19754933,  0.88955471])

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.