Re: import problem

Mats Wichmann <[email protected]> Sun, 29 Mar 2026 07:21:49 -0600
Newsgroups gmane.comp.python.tutor
Message-ID <[email protected]>
On 3/28/26 18: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.
> I'd be very grateful for an explanation as to why.

Path confusion.  If you run "python code/logic.py"  (Question: why are 
you trying to do this? You don't seem - in your fragments at least - to 
have intended it as independently runnable?) the search path starts with 
"code" and doesn't include the top-level directory. So you can't:

>      from code import cli as ui

There's no code/code/cli so this fails.

You can:

>      import cli as ui

which is your fallback... that should work, maybe try changing the 
exception you catch to ImportError rather than the more precise 
subclass. And,  in fact, a quick glance at the docs indicates that 
Python raises ImportError (specifically) when you use a from-list.

https://docs.python.org/3/library/exceptions.html#ImportError



_______________________________________________
Tutor maillist  -  To unsubscribe send an email to [email protected]
To unsubscribe or change subscription options:
%(web_page_url)slistinfo/%(_internal_name)s