Simplest way to Import a shared python script relative to the current script -
Simplest way to Import a shared python script relative to the current script -
i trying import 1 python script another. have few mutual functions defined in 1 script , lots of other scripts want import functions. no classes, functions.
the importing script needs import relative path e.g. ../../sharedscripts/python/common.py
i have few functions def f1(...)
defined call.
i found imp module seemed right thing utilize unable figure out exact syntax work example.
can suggest right code utilize or simplest approach if imp not right module?
solution answers below able working...
projectkey = 'thor' # load shared script relative script sys.path.append(os.path.dirname(__file__) + '/../../sharedscripts/python') import jira jira.checkjiracommitmessage(sys.argv[1], sys.argv[2], projectkey)
where had empty __init__.py
, jira.py
in sharedscripts/python
directory plain function definitions.
why not adding ../../sharedscripts/python/
python path? utilize common.py
other module:
import mutual common.f1()
you can alternate python path through scheme variable pythonpath
or manipulating straight python: sys.path.append("../../sharedscripts/python/")
please notice wiser work absolute pathes... (the current directory of app change)
to absolute path can phone call utilize function os.path.abspath
: os.path.abspath('../../sharedscripts/python/')
python import
Comments
Post a Comment