Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
124 views
in Technique[技术] by (71.8m points)

python can not find the fits file when using sys.path.insert

I would like to access the output of another python program that extracts information from a fits file.

I usually do this in the following way:

import sys
sys.path.insert(1, '/software/xray/Python_scripts')
from program2 import results

However, in this case, I receive the following error:

FileNotFoundError: [Errno 2] No such file or directory: 'info.fits'

When I run the program2.py It runs without problem. So, I don't understand why when I call it from program1.py it does not recognize the fits file, therefore it doesn't give the results! Can anybody point me in the right direction?

Thanks.

question from:https://stackoverflow.com/questions/65918145/python-can-not-find-the-fits-file-when-using-sys-path-insert

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Seems like your import from program2 import results searching for a file named info.fits which is located most probably in '/software/xray/Python_scripts'.

Basically, sys.path.insert temporarily adds path to PATH, in order to make OS executing/importing script from that place. It doesn't mean that it becomes a file search path as well.

You need to do somethig like:

import os
cwd = os.getcwd()
os.chdir('/software/xray/Python_scripts')
from program2 import results
os.chdir(cwd)

It is certainly a crunch, I would suggest you to create a package out of your program2 module. https://realpython.com/python-modules-packages/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...