So I have a following headache (basic I guess):
external_file.py:
def db_upload():
# some upload_to_db_code
# db_upload is a simple add to_db function from an API
def api_fetch_func():
# some code
# api_fetch_func is a function which gets information from an API
Scenario 1: Import outside the function which uses imported functions
from external_file import db_upload, api_fetch_func
def fetching_api_adding_db():
db_upload(api_fetch_func())
x = 1
print(x)
Problem - even though db_upload and api_fetch_func are NOT CALLED inside external file, python still read them which takes time and I have to wait before x variable is printed in console
So even I put db_upload(api_fetch_func()) inside a function which is not called ( fetching_api_adding_db() as you can see ) and both db_upload and api_fetch_func is not called too, python still reads them.
Scenario 2 - import inside the function which uses imported functions
def fetching_api_adding_db():
from external_file import db_upload, api_fetch_func
db_upload(api_fetch_func())
x = 1
print(x)
Job done - I don't have to wait for python to read imported functions but I haven't seen anywhere people using imports inside functions
Any other ways to handle this? Is scenario 2 a "pythonic" and correct way of doing that? Why pyton read not called functions from external file?