Import package doesn't work

Hello,

I am new in python and I don’t undestand well the next topic.

I have a project with one file di test (pytest):

import pytest as pt
import src.business_logic.mis_funciones

def test_multi():
assert src.business_logic.mis_funciones.multiplicacion(2, 5) == 10

This is working, but If I change for that:
import pytest as pt
from src.business_logic.mis_funciones import multiplicacion as dt

def test_multi():
assert dt.multiplicacion(2, 5) == 10

I got this error:
def test_multi():

  assert dt.multiplicacion(2, 5) == 10
   AttributeError: 'function' object has no attribute 'multiplicacion'

VS Code intelligence recognizes my path. Why I can’t use from src.business_logic.mis_funciones import multiplicacion as dt which would be the same that import src.business_logic.mis_funciones?

Thanks a lot

In order to preserve formatting, please select any code or traceback that you post and then click the </> button.

You have src.business_logic.mis_funciones.multiplicacion(2, 5). You’re calling multiplicacion, so it’s clearly a function.

Then you have from src.business_logic.mis_funciones import multiplicacion as dt. You’re importing the function multiplicacion but giving it the name dt.

When you write assert dt.multiplicacion(2, 5) == 10, you’re asking it to get the attribute multiplicacion of the function dt, but that function doesn’t have such an attribute.

As dt is the function, you should call it directly with assert dt(2, 5) == 10.

1 Like

You can. But this means that dt is the name for the function that you imported. In the code, you try to use dt.multiplicacion, but that makes no sense. It means to look inside the thing named dt, to find its thing named multiplicacion. But it doesn’t work like that. dt is already the thing you want, and it doesn’t have anything named multiplicacion inside of it.

Thnaks a lot a both, now it is clear for me.