Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested Imports
#1
I'm trying to create modules for my application that are themselves testable on their own. I would like to have the following directory structure:

\application.py
\support\module1.py
\support\module2.py

application.py depends on both module1.py and module2.py.
module1.py is also dependent directly on module2.py

My way to make this work so far is the following in module1.py

1
2
3
4
if __name__ == "__main__":
    import module2
else:   
    import support.module2
Without that, I get various "module not found" issues. I did a lot of reading on absolute and relative imports, but I can't identify my case among the many examples presented. My alternative plan was to leave everything flat for now (no directories) so that imports are nice and straightforward. But for a larger application, I know this would just make a big mess.

Ideas?
Reply
#2
use try except, here's an extreme example from the lxml manual:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
try:
    from lxml import etree
    print("running with lxml.etree")
except ImportError:
    try:
        # Python 2.5
        import xml.etree.cElementTree as etree
        print("running with cElementTree on Python 2.5+")
    except ImportError:
        try:
            # Python 2.5
            import xml.etree.ElementTree as etree
            print("running with ElementTree on Python 2.5+")
        except ImportError:
            try:
                # normal cElementTree install
                import cElementTree as etree
                print("running with cElementTree")
            except ImportError:
                    try:
                    # normal ElementTree install
                    import elementtree.ElementTree as etree
                    print("running with ElementTree")
                except ImportError:
                    print("Failed to import ElementTree from any known place")
Reply
#3
Thanks for the reply. Is try/except a better approach than using an if statement? In this case, I know exactly what statement will work under each condition so try except seems a little less direct.
Reply
#4
'if' statements will not stop error events from crashing system, try/except will
Reply
#5
(Nov-08-2018, 12:19 AM)Larz60+ Wrote: 'if' statements will not stop error events from crashing system, try/except will

True. That gives me something to think about.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Best programming practice for imports Curbie 8 1,630 Oct-16-2024, 02:20 AM
Last Post: Curbie
  Sudden Extremely Slow / Failed Python Imports bmccollum 1 1,123 Aug-20-2024, 02:09 PM
Last Post: DeaD_EyE
  Imports that work with Python 3.8 fail with 3.9 and 3.10 4slam 1 3,478 Mar-11-2022, 01:50 PM
Last Post: snippsat
  Imports in my first package cuppajoeman 1 2,495 Jun-28-2021, 09:06 AM
Last Post: snippsat
  script with imports works but pytest gives "ModuleNotFoundError"? Hpao 0 2,114 Jun-27-2021, 08:30 PM
Last Post: Hpao
  Help wanted with python imports petros21 3 3,518 Apr-07-2021, 07:16 PM
Last Post: snippsat
Question How to include Modules not found (conditional imports) in my setup.py when I want to cff 0 4,793 Mar-17-2021, 11:57 AM
Last Post: cff
  threading across imports Nickd12 2 2,981 Nov-09-2020, 01:59 AM
Last Post: Nickd12
  refreshing imports seandepagnier 4 3,804 Sep-20-2020, 11:51 PM
Last Post: seandepagnier
  Multimode imports fine as script but not after compiling into exe chesschaser 0 2,906 Aug-13-2020, 01:28 PM
Last Post: chesschaser

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020