Cleaner module loading
DamienIrving opened this issue · 0 comments
DamienIrving commented
Up until now, the solution I've been using for importing modules is to require that my scripts are executed from the git repo that they're in (i.e. I use cwd = os.getcwd()
when defining the module directory).
It turns out that when you run a script at the command line sys.path[0]
will always be the directory that the script is located in, so using that is a better way to do it. e.g.
import sys
script_dir = sys.path[0]
repo_dir = '/'.join(script_dir.split('/')[:-1])
module_dir = repo_dir + '/modules'
sys.path.append(module_dir)
try:
import timeseries
import general_io as gio
import spatial_weights
except ImportError:
raise ImportError('Script and modules in wrong directories')