Ladenkov Vladislav
Ladenkov Vladislav

Reputation: 1297

Imported py file can't find modules it uses

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!

Upvotes: 1

Views: 159

Answers (1)

aliva
aliva

Reputation: 5720

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)

Upvotes: 1

Related Questions