Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import not Working

I have two files say a.py and b.py.

in a.py , we do

import xxx
from b import *

in b.py we have a function which requires module xxx. Now when the function in b.py is called from a.py it cant find the module xxx.

Why is that and what can be the solution here? i cant do import xxx in b.py for some reason.

MCV:

a.py

import xxx
from b import *
fun()

b.py

def fun():
    xxx.dosomething()

Error:

Global name xxx not defined

like image 914
vks Avatar asked Jan 29 '23 23:01

vks


1 Answers

In python all modules have their own global namespaces, and A namespace containing all the built-in names is created, and module don't share it with other only built in Namespace are common and available for all modules, when you import a module it added into module global namespace, not into built namespace

enter image description here

The import statement does two things:

one, if the requested module does not yet exist, executes the code in the imported file

two makes it available as a module. Subsequent import statements will skip the first step.

and the Main point is that the code in a module will be executed exactly once, no matter how many times it is imported from various other modules.

SOURCE

like image 191
Kallz Avatar answered Feb 02 '23 08:02

Kallz