Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Import * only imports certain things from package?

I've got a package called elements that contains some stuff like button, trifader, poster. In the Button class, I am doing from elements import *

This executes OK, and when I try to print(poster), also works OK and functions as expected. However, when I do print(trifader), NameError: name 'trifader' is not defined. Even though trifader and poster are in the same package, poster is defined, but trifader isn't? How weird. Is there any explanation for this?

The directory structure of the elements package is this:

Elements:
  __init__.py
  trifader.py
  button.py
  poster.py

Also, some other stuff that isn't really relevant.

Each .py file contains a class with the name of the .py, for example, trifader.py has a class called TriFader.

like image 351
Name McChange Avatar asked Apr 23 '26 06:04

Name McChange


1 Answers

If your __init__.py doesn't have __all__ defined (thus restricting what is imported using from X import *), then you probably have a circular import somewhere which is causing a module to be referenced before its' definitions have been evaluated.

like image 120
Nick Bastin Avatar answered Apr 25 '26 20:04

Nick Bastin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!