I have two python files, one where I have written and store functions and another where they are used.
The ‘HW’ function imports and runs, however the dbxupld does not. Can anyone suggest how I could get this to work. Many thanks.
Python_script1.py
def HW():
print('Hello, World!')
def dbxupld(FileFROM,FileTO):
import dropbox
class TransferData:
def __init__(self, access_token):
self.access_token = access_token
def upload_file(self, file_from, file_to):
dbx = dropbox.Dropbox(self.access_token)
with open(file_from, 'rb') as f:
dbx.files_upload(f.read(), file_to)
def main():
access_token = "[access code hidden]"
transferData = TransferData(access_token)
file_from = FileFROM
file_to = FileTO
transferData.upload_file(file_from, file_to)
if __name__ == '__main__':
main()
Python_script2.py
from Python_script1 import HW
from Python_script1 import dbxupld
HW()
FileFROM = '/home/Setup stuff.pdf'
FileTO = '/upload_testing/Setup Stuff.pdf'
dbxupld(FileFROM,FileTO)
Out:
Hello, World!
But no dropbox upload
The function is being imported correctly. The problem lies in this part:
if __name__ == '__main__':
main()
Since you're importing Python_script1 inside Python_script2, the value of __name__
inside Python_script1 is Python_script1
and not __main__
. The condition is not met and main
never gets called. To make this work, remove the condition if __name__ == 'main'
.
For more information about __main__
and __name__
, refer https://docs.python.org/3/library/main.html