我收集了一些功能,并保存为functions.py
格式。这些功能需要numpy
,glob
和其他软件包。我先导入软件包,例如import glob
,然后再导入import functions
,然后在运行给定函数rvs = get_RVdata(path)
时收到错误消息,提示未定义glob。
为什么在我定义的函数中无法识别这些标准软件包?
def get_RVdata(path):
'''
Retrieve from the header of each fits the calculated RV per spectrum
'''
rvs = []
e_rvs = []
bjd = []
#get ccfs
CCF_paths = path
CCF_files = glob.glob(CCF_paths) # contains path
#set variables
for path in np.sort(CCF_files):
with fits.open(path) as data:
rvs.append(data[0].header["ESO QC CCF RV"])
e_rvs.append(data[0].header["ESO QC CCF RV ERROR"])
bjd.append(data[0].header["ESO QC BJD"])
return np.array(bjd), np.array(rvs), np.array(e_rvs)
错误是这样的:
---> CCF_files = glob.glob(CCF_paths) # contains path
NameError: name 'glob' is not defined
为什么在函数中看不到glob?我已将其加载到function.py之外。我可以解决在function.py中添加import glob
的问题,但是我想在我的环境中导入。
答案 0 :(得分:2)
最好用示例来说明:
functions.py
中需要包含的示例:
import glob
def get_ccfs(paths):
#get ccfs
CCF_paths = paths
CCF_files = glob.glob(CCF_paths) # contains path
#store CCFS and rv domain
# this is just a sample return to conform to your original code
return CCF_files, "some other return"
other_file_that_calls_functions.py
的示例:
from functions import get_ccfs
paths = "./fun*"
a, b = get_ccfs(paths)
print(a)
print(b)
命令行调用的示例:
> python other_file_that_calls_functions.py
['.\\functions.py']
some other return