我想访问另一个python程序的输出,该程序从fits文件中提取信息。
我通常这样做:
import sys
sys.path.insert(1, '/software/xray/Python_scripts')
from program2 import results
然而,在这种情况下,我收到以下错误:
FileNotFoundError: [Errno 2] No such file or directory: 'info.fits'
当我运行program2.py
时,它运行正常。所以,我不明白为什么当我从program1.py
调用它时,它不识别fits文件,因此它没有给出结果!有人能给我指个正确的方向吗?
谢谢。
似乎您的进口from program2 import results
搜索一个名为info.fits
的文件,该文件最有可能位于'/software/xray/Python_scripts'
。
基本上,sys.path.insert
临时为PATH
添加路径,以便操作系统从该位置执行/导入脚本。这并不意味着它也会成为一个文件搜索路径。
你需要这样做:
import os
cwd = os.getcwd()
os.chdir('/software/xray/Python_scripts')
from program2 import results
os.chdir(cwd)
这确实很紧张,我建议你从你的program2模块中创建一个包。https://realpython.com/python-modules-packages/