0

I am trying to move variables between scripts. I have two scripts: script1.py and script2.py

In script1.py I am using sys.argv to create a variable called country. This variable is imported to script2.py which process the country variable. The problem is when I try to import this new variable back to script1.py I get the following error:

from script2 import rule
ImportError: cannot import name 'rule'

I am running the script1.py in the terminal

python script1.py us

script1.py

import sys
country = str(sys.argv[1])
from script2 import rule
print (rule)

script2.py

from script1 import country
rule = 'this is a rule' + country 

Thanks!

David
  • 487
  • 2
  • 6
  • 18
  • 5
    Possible duplicate of [ImportError: Cannot import name X](https://stackoverflow.com/questions/9252543/importerror-cannot-import-name-x) – cookiedough May 24 '17 at 15:12
  • 2
    This is a case of circular dependent imports. Check out: [This link](https://stackoverflow.com/questions/9252543/importerror-cannot-import-name-x) – cookiedough May 24 '17 at 15:14
  • Not sure this is 100% dupe. – Mad Physicist May 24 '17 at 15:19
  • You can generally avoid this issue by avoiding `from` imports (at least in your case). Access the members by `script1.country` and `script2.rule` and you should be fine. Also, avoid circular imports if you can. Not that you always can, but they always do complicate things. – Mad Physicist May 24 '17 at 15:21
  • I tried using `script1.country` and `script2.rule` and I get the following error: `AttributeError: module 'script2' has no attribute 'rule'` – David May 24 '17 at 15:30

1 Answers1

1

Circular dependent imports would raise ImportError.
Modify your scripts as follows:

script1.py

import sys
country = str(sys.argv[1])
from script2 import rule
print(rule(country))

script2.py (just one line)

rule = 'this is a rule{}'.format
williezh
  • 917
  • 5
  • 8