1

I am writing some code that will require data from downstream the package hierarchy. Consider the following project structure:

├── app/
│   ├── main.py
│   ├── lib.py
│   └── subpck/
│       └── module.py

And the following code:

# lib.py
some_instance = SomeClass()

# subpck.module
from ..lib import some_instance
some_instance.a = 'abc'

# main.py
from .lib import some_instance
print(some_instance.a)

The key point here is that main.py does not import subpck.module, so the latter's code is not run. However, I have successfully used runpy in main.py to run subpck.module and have gotten the desired results.

My question is this:

If I can somehow figure out how to encapsulate the use of runpy in SomeClass, is it safe to do?

I have been reading the runpy docs, but am nervous if I am missing something. I also haven't heard if this is a big "no-no" for code that may be used in production.

Any help is appreciated.

Algebra8
  • 1,115
  • 1
  • 10
  • 23

0 Answers0