Tom Weisner
Tom Weisner

Reputation: 13

Imported function not working

I am pretty new to Python and am trying to import a function I have made in a separate file. When I run the code I get "TypeError: signal() missing 1 required positional argument: 'handler'". I think it means the signal function is not being passed a single argument but I am pretty sure that is what the for loop does. Where am I going wrong? Also, the files are in the same folder, which is a part of the working directory. The code that calls the function is:

import numpy as np

t=np.linspace(-5,5,200)

import signal

y=[]
for i in t:
    y.append(signal.signal(i))

The function code is saved in a file called signal.py. The code is:

def signal(t):
    import numpy as np
    y=np.cos(t)*np.exp(-abs(t))
    return y

Upvotes: 0

Views: 8971

Answers (2)

Anuj
Anuj

Reputation: 1014

As suggested by chepner, you have a module name conflict with pythons inbuilt module signal

If the name is not important, then you could change the name.

If the name is important then you could create a package and place the file in that, Then import it.

For example, following will be your directory tree

signal_module/
├── __init__.py
└── signal.py
original_file.py

Then import the signal_module as follows

from signal_module import signal

The __init__.py file is import.

It can be empty, but it needs to be created for python to tree the directory as package.

As you said you are new to python, have a look at this answer to know more about the importance of __init__.py

Upvotes: 1

David Gourde
David Gourde

Reputation: 3914

It seems you are trying to import a signal from the standard library instead of your own file. Try to import it like this:

from .signal import signal

PS: Since you are new to Python, you should also make sure you have a an __init__.py file in the directory, like so:

/Parent
    __init__.py
    main.py
    signal.py

Upvotes: 1

Related Questions