0

I have two documents, I want to import a variable from my main one into the other one but when I try this pops up

ImportError: cannot import name 'numb'

I have successfully transferred variables from the auxiliary one to the main one but cannot do it the other way round and do not know why.

This is the code, in case I am doing something wrong.

The main one (code.py):

import second
numb = [25,45,48,56,25]
choice = str(input("Choice"))
if choice == "a" :
    second.test()

The second one (second.py):

def test():
    import code

    from code import numb
lolol
  • 69
  • 11
  • `from second import *` or `from second import test` should do. But test should have actual code , not `import code` which is circular. – roadrunner66 Mar 05 '16 at 18:35
  • Making both `code` and `second` `import` each other makes no sense. –  Mar 05 '16 at 18:39
  • @roadrunner66 When you say _from second import test_, do you mean the second python document or second.py? – lolol Mar 05 '16 at 18:40

1 Answers1

1

This is as a result of Circular dependency explained in this article --> Importing Python Modules. You can read the marked answer to this question. That should help.

Community
  • 1
  • 1
i_emmanuel
  • 519
  • 6
  • 6
  • Ahhh, thanks. I understand how it happens now but how can I resolve the issue? The link said to move stuff around but if I move the imports around then it will affect the other code (Not included up there). – lolol Mar 05 '16 at 18:48
  • @XainR Circular dependencies are usually a result of poorly engineered code. I don't fully understand what you're trying to achieve in the code snippet you posted, but I'll suggest that instead of having to import numb in second.py, you could create an __init__.py file and declare numb there. That way, you can import numb in both modules. Use http://stackoverflow.com/questions/1383239/can-i-use-init-py-to-define-global-variables as a reference. – i_emmanuel Mar 05 '16 at 19:11