arfc / transition-scenarios

A repository to hold transition scenarios with Cyclus.
Other
3 stars 12 forks source link

Change import commands #148

Open abachma2 opened 1 year ago

abachma2 commented 1 year ago

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 commented 2 months ago

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 commented 2 months ago

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.