Python import file with dependencies in the same folder -
i have file ref.py depends on text file ex.txt
, in same directory \home\ref_dir
. works when run file ref.py
, if try import ref.py
file work.py in different directory \home\work_dir
, following
import sys sys.path.append('\home\ref_dir') import ref
but error, program cannot find ex.txt
how can solve issue without using absolute paths. ideas?
use os module access current absolute path of module you're in, , dirname that
you want open ex.txt in file this.
import os open('%s/ex.txt' % os.path.dirname(os.path.abspath(__file__)) ex: print ex.read()