how do i import a function and a dictionary that mutually imports each other. These two files are already in the same directory thus, there is no nid to import sys. Also, i this it is recursive that is why it is unable to import. How do i import a dictionary from each other's file without making it recursive and causing an error?
I did go to this website here but it did not answer my question nor did it provide any example code to guide me thus, i created this question with a test code to explain my issue.
let's say i have these two files: boxA and boxR, each has a dictionary keyA and keyR and functions named generatekeyA and generatekeyR
in boxA:
import json
from tinydb import TinyDB, Query
from boxR import keyR
def generatekeyA():
keyA = {}
serialnoA = 'Serial_noA'
secretidA = 'Secret_idA'
count = 0
while (count <5):
serial = generate key
id = generate id
keyA[serialnoA].append(serial)
keyA[secretidA].append(id)
with open("/home/pi/Desktop/json/output.json", 'w+'):
db = TinyDB('/home/pi/Desktop/json/output1.json')
table = db.table('A KEYS')
db.insert_multiple([{'Serial number A' : keyA[serialnoA]}])
db.insert_multiple([{'Secret id A' : keyA[secretidA]}])
db.insert_multiple([{'Secret id R' : keyR[secretidR]}])
generatekeyA()
in boxR:
import json
from tinydb import TinyDB, Query
from boxA import keyA
def generatekeyR():
keyR = {}
serialnoR = 'Serial_noR'
secretidR = 'Secret_idR'
count = 0
while (count <5):
serialR = generate key
idR = generate id
keyR[serialnoR].append(serialR)
keyR[secretidR].append(idR)
with open("/home/pi/Desktop/json/output2.json", 'w+'):
db = TinyDB('/home/pi/Desktop/json/output2.json')
table = db.table('R KEYS')
db.insert_multiple([{'Serial number R' : keyR[serialnoR]}])
db.insert_multiple([{'Secret id R' : keyR[secretidR]}])
db.insert_multiple([{'Secret id A' : keyA[secretidA]}])
generatekeyA()
Let me explain the codes above. I have 2 files that generate keys for me after which, i have to export the output into a json file. output.json file prints out keyA's own serial no and secret id and only keyR's secretid and viceversa into output2.json file. but the thing is that eventhough i research on recursive outputs, i still do not understand how to fix it because it does not provide any sample code as a guide. What is the best way to approach this such that i do not have to make much changes to the file(s)?
Error tells me that it could be a recursive error:
Traceback (most recent call last):
File "/home/pi/Desktop/boxA.py", line 7, in <module>
from boxR import keyR
File "/home/pi/Desktop/boxR.py", line 11, in <module>
from boxA import keyA
File "/home/pi/Desktop/boxA.py", line 7, in <module>
from boxR import keyR
ImportError: cannot import name 'keyA'