Reputation: 1765
Im building a small application. In my main.py I have:
from lib.save_decoded_json import saveJsonToFile
# some code
saveJsonToFile("get_info", outStr)
# some code
in lib folder I have 2 files:
__init__.py
save_decoded_json.py
the last one looks like this:
import time
def saveJsonToFile(folderName, packerJsonData):
timestr = time.strftime("%Y%m%d-%H%M%S")
with open(folderName + "/" + timestr + ".json","w") as file:
for result in extract_json_objects(packerJsonData):
file.write(json.dumps(result, indent=4, sort_keys=True))
print('-------------------')
print(folderName + ' SAVED')
print('-------------------')
call to saveJsonToFile throws this error:
File "lib/save_decoded_json.py", line 2, in saveJsonToFile
NameError: name 'time' is not defined
Why submodule doesn't import time properly"
Upvotes: 0
Views: 144
Reputation: 643
You are only importing the function saveJsonToFile
not the whole script so you never imported time.
However, you can import time
inside your function. This will work:
def saveJsonToFile(folderName, packerJsonData):
import time
timestr = time.strftime("%Y%m%d-%H%M%S")
with open(folderName + "/" + timestr + ".json","w") as file:
for result in extract_json_objects(packerJsonData):
file.write(json.dumps(result, indent=4, sort_keys=True))
print('-------------------')
print(folderName + ' SAVED')
print('-------------------')
Upvotes: 1