简体   繁体   中英

Python call a imported function from a different .py file, but the imported function cannot see the other imported moduel in main file

I have a function defined in a different file nhpp_next_arrival.py , which contains a function. The important thing to notice is that I am using numpy package.

def nhpp_next_arrival(t,Lambda, h_lam):
    # current starting time t
    # rate handle h_lam
    # Lambda a majorizing function/constant

    U = np.random.uniform()
    V = np.random.uniform()

    t = t - np.log(U)/Lambda


    while V > h_lam(t)/Lambda:

        t = t - np.log(U)/Lambda
        U = np.random.uniform()
        V = np.random.uniform()

    return t

I imported this function in a different file as the following

import numpy as np
from nhpp import *

#Call nhpp_next_arrival
t_arrival = nhpp_next_arrival(t=t, Lambda=max_arrival, h_lam=h_arr_total)

Then I got the following error message.

U = np.random.uniform() NameError: name 'np' is not defined

Thank you!

You might be confused with a C #include (or something similar).

Your code is using numpy.random at nhpp_next_arrival.py , so you should have at its top

import numpy as np

Even though you imported it before the import to this file, when the interpreter sees

from nhpp import *

it doesn't import the packages into the namespace of that module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM