Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaner module loading #16

Open
DamienIrving opened this issue Jan 22, 2021 · 0 comments
Open

Cleaner module loading #16

DamienIrving opened this issue Jan 22, 2021 · 0 comments

Comments

@DamienIrving
Copy link
Owner

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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant