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

Change import commands #148

Open
abachma2 opened this issue Feb 7, 2023 · 2 comments
Open

Change import commands #148

abachma2 opened this issue Feb 7, 2023 · 2 comments

Comments

@abachma2
Copy link
Contributor

abachma2 commented Feb 7, 2023

Right now, many of the module imports in scripts in this repository use sys.path.insert() which is bad form. To close this PR, update all of the needed files to import the modules directly without using the sys.path.insert() method.

@nsryan2
Copy link
Member

nsryan2 commented Jul 9, 2024

There is a feature called importlib.util that is slightly different?

I'm not totally clear on the reasons you have for why sys.path.insert() is bad form, but I've used importlib.util on a couple projects. Basically:

import importlib.util

# Specify the path relative to the current notebook's directory
<name> = importlib.util.spec_from_file_location('<package name>', 'relative/path/to/script.py')

In this repo, from scenarios, you could import analysis.py with:

import importlib.util

# Specify the path relative to the current notebook's directory
analysis = importlib.util.spec_from_file_location('analysis', '../scripts/analysis.py')

I'm sure there are other ways to do this, but this is one method I know of. Would something like this address your issue @abachma2?

@abachma2
Copy link
Contributor Author

abachma2 commented Jul 9, 2024

If the directory is set up correctly, then you should be able to use import package_name instead of having to add the path to the system. That's what I'm saying is bad form. It's a relatively small thing, but if there is a way to easily clean it all up then it might be worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants