Python 我自己的函数看不到本地导入的包为什么?
我有一组保存为Python 我自己的函数看不到本地导入的包为什么?,python,Python,我有一组保存为functions.py格式的函数。这些函数需要numpy、glob和其他软件包。我导入包,例如,import glob然后导入函数,然后当我运行给定函数rvs=get_RVdata(path)时,我得到一个错误,表示glob未定义 为什么在我定义的函数中无法识别这些标准包 def get_RVdata(path): ''' Retrieve from the header of each fits the calculated RV per spectrum
functions.py
格式的函数。这些函数需要numpy
、glob
和其他软件包。我导入包,例如,import glob
然后导入函数,然后当我运行给定函数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
来解决这个问题,但我想在我的环境中导入。可能最好用以下示例来说明:
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"
from functions import get_ccfs
paths = "./fun*"
a, b = get_ccfs(paths)
print(a)
print(b)
调用函数.py的其他\u文件\u的示例如下:
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"
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
也许最好用例子来说明:
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"
from functions import get_ccfs
paths = "./fun*"
a, b = get_ccfs(paths)
print(a)
print(b)
调用函数.py的其他\u文件\u的示例如下:
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"
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
请共享获取ccfs(路径)函数代码。如果不想在函数
模块内导入全局
,可以将glob.glob
作为参数传递给get\u ccfs
。虽然使functions.py
导入需要运行的内容更为简洁。模块有自己的作用域,它们从该作用域中搜索引用。在单独的文件中添加导入将不起作用,因为函数只能访问自己的作用域。谢谢,因此模块有自己的作用域。但我如何强制继承?我最终将所有导入都放在了模块中,它工作得很好,但是如果我想导入glob并使它在我的函数中从另一个模块可见,有解决方法吗?一种不将glob作为参数传递的方法?请共享get_ccfs(路径)函数代码。如果不想在函数
模块内导入glob
,可以将glob.glob
作为参数传递给get_ccfs
。虽然使functions.py
导入需要运行的内容更为简洁。模块有自己的作用域,它们从该作用域中搜索引用。在单独的文件中添加导入将不起作用,因为函数只能访问自己的作用域。谢谢,因此模块有自己的作用域。但我如何强制继承?我最终将所有导入都放在了模块中,它工作得很好,但是如果我想导入glob并使它在我的函数中从另一个模块可见,有解决方法吗?一种不将glob作为参数传递就可以看到它的方法?