-1

I am stuck in resolving a problem using python. Problem is I have to pass a variable value of module(python_code1.py) to a different module(python_code2.py). Based on the variable value, need to do some calculation in the module python_code2.py and then need to capture output value in the same module(python_code1.py) for further calculations.

Below is the snapshot of my code logic :

python_code2.py

import python_code1

data = python_code1.json_data
'''
Lines of code
'''
output = "some variable attribues"

python_code1.py

import python_code2

json_data =  {"val1": "abc3","val1": "abc3","val1": "abc3"}

input_data = python_code2.output

''''
Lines of code using input_data variable

'''''

when I execute python python_code1.py, this is giving error:

AttributeError: module 'python_code2' has no attribute 'output'

I feel like I am not doing it in write way, but considering my code complexity and lines of code, I have to use these 2 module method.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ankit
  • 61
  • 5
  • 2
    That's what function arguments and return values are for. – Stop harming Monica Aug 29 '19 at 14:10
  • https://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python looks like a duplicate, and gives a workaround (call the attributes within a function that gets executed at runtime). – Leporello Aug 29 '19 at 14:11
  • Possible duplicate of [Circular (or cyclic) imports in Python](https://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python) – Trapli Sep 04 '19 at 09:32

1 Answers1

0

Putting your code at the top-level is fine for quick throw away scripts, but that's about all. The proper way to organize your code for anything non-trivial is to define functions, so you can pass values as arguments and get results as return value.

If there's only on script using those functions, you can keep them in the script itself.

If you have multiple scripts needing to use the same functions, move those functions to a module and import this module from your scripts.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118