Relative import multiple levels ? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Relative import multiple levels ? (/thread-8000.html) |
Relative import multiple levels ? - Windspar - Feb-02-2018 packages - gradient -- hsl_by_value.py - objects -- styles --- box_style.py How would I do? Like to do it with relative imports. box_style.py from ..gradient import hsl_by_value I think I figure it out. So ... takes you to model name ? from ...gradient import hsl_by_value RE: Relative import multiple levels ? - metulburr - Feb-02-2018 restate your structure. I dont understand what the dashes mean. From what i assume that means is? packages/ gradient/ hsl_by_value.py objects/ styles/ box_style.py RE: Relative import multiple levels ? - snippsat - Feb-02-2018 This is how i would have done it. my_pack\ |-- __init__.py gradient\ |-- __init__.py |-- gradient.pyTop __init__.py is the file the file i change.The use of .something or usage of __all__ in this __init__.py will prevent long imports for users.A example of something that could have been done better from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas__init__.py: from .gradient import gradientgradient.py: def hsl_value(): return 'rgb(255, 0, 0)'So using my_pack look like this: λ ptpython >>> import my_pack >>> my_pack.gradient.hsl_value() 'rgb(255, 0, 0)' >>> # Or >>> from my_pack import gradient >>> gradient.hsl_value() 'rgb(255, 0, 0)' RE: Relative import multiple levels ? - Windspar - Feb-02-2018 (Feb-02-2018, 08:23 PM)metulburr Wrote: restate your structure. I dont understand what the dashes mean. From what i assume that means is? Yep. That what my structure looks like. Thanks. The ... solved my problem. |