'How to share imports between modules?

My package looks like this:

enter image description here

These helpers, since they are all dealing with scipy, all have common imports:

from matplotlib import pyplot as plt
import numpy as np

I'm wondering if it is possible to extract them out, and put it somewhere else, so I can reduce the duplicate code within each module?



Solution 1:[1]

Alright, here is my tweak,

enter image description here

Create a gemfile under the package dir, like this

import numpy as np
from matplotlib import pyplot as plt
import matplotlib as mpl

Then, for other files, like app_helper.py

from .gemfile import *

This comes from here Can I use __init__.py to define global variables?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community