Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

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

Answer*

Draft saved
Draft discarded
Cancel
4
  • 1
    what's the source for all this Commented Jul 20, 2017 at 6:08
  • This is the case. Say, a.py import library X and monkey patches X. Then, b.py imports a.py. At this point, X is not directly visible to b.py. After that b.py imports X. Python won't import the same thing twice, so it goes on and uses the X patched in a.py for b.py instead of importing a new copy of X for b.py. That is why b.py only gets the patched X, but not the original X. Commented Jul 20, 2017 at 6:18
  • @vks You shouldn't accept an answer which shows no effort at answering your questions. Blind copy paste is tantamount to plagiarism and must be frowned upon. Commented Aug 18, 2017 at 14:09
  • @cᴏʟᴅsᴘᴇᴇᴅ while you may be correct,I also had an aspect to look upon wherein I was stuck with this and somebody got me outta it.... So had no option but to accept this :) Commented Aug 24, 2017 at 5:19

lang-py

AltStyle によって変換されたページ (->オリジナル) /