'I can't use a function defined in another file, as if it wasn't there
I'm creating a problem written un python in Jupyter notebook. I have a file named as "myfunctions1.py", where I have defined a function called ClearSky. In the very same directory I'm writing a new programe but it turns out that every time I try to use the name of the file as a module and I try to access the function I get an error.
def ClearSky(csvfile):
#Fem que obvie els tabuladors
df = pandas.read_csv(csvfile, delimiter = "\\t")
#Subsitueix els Nan per strings amb 000
df.fillna("000", inplace=True)
#Convertiré en un array tot el percal
NUBarray = df.to_numpy()
#Creem la llista dels clear sky on anirem afegint els dies que toquen
clearsky = []
#Les úniques columnes no nules han de ser les de dia/mes/any
#Que venen a ser la columna 0, 1 i 2, per això comencem per la columna 3
col = NUBarray.shape[1]
row = NUBarray.shape[0]
lastcol = col - 1
#Fem dos bucles, el primer anirà fila a fia que és el dia, el segon anirà columna a columna
#Per a tenir un dia clear sky les columnes han d'estar totes composades per 000
for n in range(0,row):
for k in range(3,col):
#Trenco el primer bucle si trobem una columna diferent a '000'
if NUBarray[n,k] != "000":
break
#Si no s'ha trencat fins la última columna i aquesta val '000'
#Afegim a la llista el dia, el més i l'any que són les columnes 0,1 i 2
else:
if k == lastcol:
clearsky.append(NUBarray[n,0])
clearsky.append(NUBarray[n,1])
clearsky.append(NUBarray[n,2])
#Aquesta part només afecta si hem trencat el primer bucle, li diem que vagi al següent dia
#Així no cal que mire totes les columnes, no ens interessa
continue
#Me torna una llista però vull que sigue unarray de 3 columnes
#Puc trobar el nombre de files dividint per tres
#Uso reshape per a canviar la dimensió
clearskyrows = int(len(clearsky)/3)
clearskyarray = array(clearsky)
#Retorno un array amb els dies de cel clar
return clearskyarray.reshape(clearskyrows,3)
What I do in another file:
from myfunctions1 import ClearSky
I'm getting the error:
ImportError Traceback (most recent call last)/var/folders/h3/_ws68mys35vfc_g11qml5cmr0000gn/T/ipykernel_10758/2584126548.py in <module>16 #Let's improt useful functions17 from numpy import array,reshape,stack---> 18 from myfunctions1 import ClearSky
ImportError: cannot import name 'ClearSky' from 'myfunctions1' (/Users/andreacerapalatsi/Practiquesempresa/myfunctions1.py)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
