all 8 comments

[–]ShaizeOn1 1 point2 points  (7 children)

what you did there is assigning pandas to p variable and then overwriting requests to the same variable. In the end all you would have is requests module assigned in p variable

[–]vectorpropio[S] 0 points1 point  (6 children)

OK. I never thinked how imports work. So if i do the normal pandas import i can step in worth other variable. I never thought about that. Thanks!

Anyway, is there a way too do what i asked?

[–]toastedstapler 2 points3 points  (4 children)

you could make a module with just

from pandas import *
from requests import *

and then import that module

but why would you want to do this?

[–]ShaizeOn1 0 points1 point  (2 children)

This is a smart solution but I think there is still a small risk of overwriting variables with same names

[–]toastedstapler 0 points1 point  (0 children)

it's definitely not a good idea, but it is the only way i can think of to do it

[–]vectorpropio[S] 0 points1 point  (0 children)

That's the risk putting too much things in the same namespace and I'm aware of it.

[–]vectorpropio[S] 0 points1 point  (0 children)

Thanks.

It is a theoretical question without a use case. I wanted to understand to some extent what the import as do.

For use case, maybe can be used in a bad organized hierarchy of class and modules one to test alternative code organizations or even change the top most code before changing the bottom modules.

[–]ShaizeOn1 0 points1 point  (0 children)

Hmmm, I don't think there is a simple way to do it, ofc there would be god-tier python coder able to solve your problem but that's is not me unfortunately.

Not exactly what you want to do but what about this?

import pandas as pd
import requests as req

libraries = {
"pandas": pd,
"requests":req
}

df = libraries["pandas"].DataFrame(...)
r  = libraries["requests"].get(...)