Imported py file can't find modules it uses

278
April 15, 2017, at 05:41 AM

I have an util.py file. There's a function func in it:

import pandas as pd
import numpy as np
def delete_columns(data, columns):
    """
    Deletes all columns in data 
    """
    for column in columns:
        del data[column]

If I import it in my jupyter notebook and implement it - everything is OK.

But then I deside to make another util2 which calls util:

import numpy as np
from util import *
def clean(data):
    data = data[np.isfinite(data.lol)]
    delete_columns(data=data, columns= ['1','2','3'])

And NOW, when i call it from jupyter it finishes with:

----> 7 from user_functions import *
      8 def clean_1(data):
      9     data = data[np.isfinite(data.lol)]
NameError: name 'np' is not defined

What's wrong? Each file imports all modules!

Answer 1

you should import np in modules that's using it, change your util.py to this:

import numpy as np
def func(l):
    a = np.array(l)
    return a

UPDATE: make sure you have numpy for python3 installed (open python3 interpreter (make sure it's python3) and import numpy see if it works (also make sure jupiter is using python 3)

Rent Charter Buses Company
READ ALSO
Apply multiple functions at one time to Pandas groupby object

Apply multiple functions at one time to Pandas groupby object

Variations of this question have been asked (see this question) but I haven't found a good solution for would seem to be a common use-case of groupby in Pandas

554
Fix index column in pandas DataFrame

Fix index column in pandas DataFrame

I have a pandas DataFrame with the following data (output from Jupyter Notebook)

605
In which order do the rules get evaluated in the CrawlSpider?

In which order do the rules get evaluated in the CrawlSpider?

I have a question regarding the order in which the rules get evaluated in a CrawlSpiderIf I have the code below:

325