Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module "x" has no attribute "y", creating my own module .py

Tags:

python

module

I am trying to create my own module (mi_modulo.py) and move there all the functions that I have defined in my Jupyter Notebook script, so that it looks cleaner.

However, when I am trying to use these functions that I have already moved to the module, I am not able to use them all, and I get the following message: module 'mi_modulo' has no attribute 'train4_data_import'

I have installed Anaconda 3.0 and I am running Python 3.7.0 through Jupyter Notebooks. (Forgive me if the expressions sound awkward, I know a bit of Python, but I am not really into all the installation, software, IDE, etc details.)

## mi_modulo.py ##

def train4_data_import(file_name):

    df = pandas.read_excel(file_name)

    force = df["Signal 1"].values[13:]
    acceleration1 = df["Signal 2"].values[13:]
    acceleration2 = df["Signal 3"].values[13:]

    return force, acceleration1, acceleration2

def hola_mundo():
    print("whatever")

## script ##

import pandas
import mi_modulo as mi

mi.hola_mundo()

mi.train4_data_import("Tren4.xlsx")

And this is what I get: (I was going to show an image but I am not sure how to do that with this stackoverflow new form style)

whatever

AttributeError                            Traceback (most recent call last)
<ipython-input-18-69a38929f7e6> in <module>()
      3 mi.hola_mundo()
      4 
----> 5 mi.train4_data_import()

AttributeError: module 'mi_modulo' has no attribute 'train4_data_import'

I don't understand why it is able to read one function but not the other.

----------------------------- EDIT 1 ----------------------------

Doing what U9-Forward suggests:

import pandas
from mi_modulo import *

hola_mundo()

train4_data_import("Tren4.xlsx")

I get now the following error:

whatever


NameError                                 Traceback (most recent call last)
<ipython-input-25-e1885200beb7> in <module>()
      3 hola_mundo()
      4 
----> 5 train4_data_import("Tren4.xlsx")

NameError: name 'train4_data_import' is not defined
like image 488
sdiabr Avatar asked Jan 27 '23 23:01

sdiabr


1 Answers

In jupyter-notebook, sometimes you need to restart the kernel to import all the unsaved module you have. Also, you need to import all the dependency for the custom module within that module.

like image 117
Osman Mamun Avatar answered Feb 01 '23 17:02

Osman Mamun