Estou tentando fazer um pacote Python para TUI, mas quando tento importar meu pacote e usar módulos dele, esse erro está ocorrendo
Traceback (most recent call last):
File "path to test.py", line 3, in <module>
mypro.draw.box('+', None, None, 50, 25)
^^^^^^^^^
AttributeError: module 'my_project' has no attribute 'draw'
test.py se parece com isso
import my_project as mypro
mypro.draw.box('+', None, None, 50, 25)
estou recebendo esse erro apenas quando importo meu pacote diretamente porque quando tento isso
from my_project import draw as my_draw
my_draw.box('+', None, None, 50, 25)
agora tudo funciona bem
estrutura do projeto
my_project
│ LICENSE.txt
│ pyproject.toml
│ README.md
│ __init__.py
│
├───build
|
├───draw
│ │ shapes.py
│ │ __init__.py
│ │
│ └───__pycache__
│
├───my_project.egg-info
│
├───my_math
│ │ types.py
│ │ __init__.py
│ │
│ └───__pycache__
│
├───styling
│ │ models.py
│ │ palette.py
│ │ types.py
│ │ __init__.py
│ │
│ └───__pycache__
│
└───__pycache__
formas.py
from ..styling import rgb, color_nameX, color_name, color_like
from sys import stdout as out
def box(fill: str, foreground: color_like | None, background: color_like | None, width: int, height: int) -> None:
if foreground: fore = foreground; fore.graund = "fore"
else: fore = False
if background: back = background; back.graund = "back"
else: back = False
for h in range(height):
if fore: out.write(str(fore))
if back: out.write(str(back))
out.write(fill[0]*width)
if fore: out.write("\x1b[39m")
if back: out.write("\x1b[49m")
out.write("\n")
Localizei o código test.py no mesmo nível da pasta my_project. Encontrei isso, além da seguinte entrada sugerida por @bsraskr.
meu_projeto/__init__.py
Você também precisa:
meu_projeto/desenhar/__init__.py