Re: import problem
Alan Gauld via Tutor <[email protected]> Sun, 29 Mar 2026 00:41:08 +0000
| Newsgroups | gmane.comp.python.tutor |
|---|---|
| Message-ID | <[email protected]> |
On 29/03/2026 00:17, Alex Kleider via Tutor wrote:
> I'm having an import problem that baffles me!
> When I run a script (code/logic.py) I get an import error but if the
> same script is imported (by main.py,) no error occurs.
Can you remove all the try/except stuff
and run the two files. Then post the two
sets of output with the full tracebacks.
That might make it easier to see what's going on.
> [/home/alex/Git/Exp] $ cat code/logic.py
> #!/usr/bin/env python3
> # File: code/logic.py
> """
> !!!!!!! Won't run !!!!!!!
> Unable to import cli as ui
> but can be imported by main.py.
> """
> try:
> import data
> except ModuleNotFoundError:
> from code import data
> try:
> from code import cli as ui
> except ModuleNotFoundError:
> import cli as ui
> print("logic.py successfully imported cli as ui")
> print("logic.py: being imported (or run.)")
> [/home/alex/Git/Exp] $ cat main.py
> #!/usr/bin/env python3
> # File: main.py
> """
> Driver program.
> """
> try:
> from code import logic
> except ModuleNotFoundError:
> import logic
> try:
> from code import cli as ui
> except ModuleNotFoundError:
> import cli as ui
> if __name__ == "__main__":
> print("Running main.")
>
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
Tutor maillist - To unsubscribe send an email to [email protected]
To unsubscribe or change subscription options:
%(web_page_url)slistinfo/%(_internal_name)s