0

just new to python, and I believe this is not a big deal but because I am a freshman. Basically, this is a simple program plotting a FM signal in time domain. I write a module by myself.

def FreqMod (fc,fm,t_domain)

    pi=py.pi  
    if fc>fm:
       delta=fc-fm
    else:
        delta=fm-fc
    return py.cos(2*pi*fc*t_domain+ (delta/fm)*py.sin(2*pi*fm*t_domain))


def AmpMod(fc,fm,t_domain):

    pi=py.pi
    return py.cos(2*pi*fc*t_domain)*py.cos(2*pi*fm*t_domain)

And import it in another program

import numpy as py
import mylib 
import matplotlib.pyplot as plt


pi=py.pi
y=mylib.FreqMod(5,1000,t=py.arange(0,2*pi,pi/4000))
plt.plot(y)

The lib file is located as the same directory as the program. But I Got this later:

Traceback (most recent call last):

  File "...(The directory)...", line 14, in <module>
    y=mylib.FreqMod(5,1000,t=py.arange(0,2*pi,pi/4000))

AttributeError: module 'mylib' has no attribute 'FreqMod'

It seems like I didn't import the module successfully. I have compared it with examples in how to write and import a module, but yet can't figure out why. This really confuse me as a beginner in python.

Lambert_X
  • 1
  • 1
  • Does this answer your question? [import function from a file in the same folder](https://stackoverflow.com/questions/43865291/import-function-from-a-file-in-the-same-folder) – Tomerikoo Sep 27 '20 at 14:37
  • Where do you use the `t_domain` parameter inside your function? and Where do you define the `global t` ? If you pass a `t` parameter to your function, it will not know what it is. Fix those issues and I think you should have no trouble importing `mylib` – chapelo Sep 27 '20 at 14:42
  • Hi. Yeah I found that there are some error related to the variables and I have fixed that. However the attribute error is still there. – Lambert_X Sep 27 '20 at 15:26
  • As shown now I have filtered the problem with variables. – Lambert_X Sep 27 '20 at 17:28
  • It seems like a problem with the path. I have solved it. But anyway thanks! – Lambert_X Sep 27 '20 at 19:12

0 Answers0