Reputation: 1
I've got a really particular case and I've spent hours on it but can't seem to figure it out. I've got a program that downloads an excel file, makes modifications to the file and then uploads it to sharepoint. The file is also saved on the local computer and that local file is what's uploaded to sharepoint under a different name.
When I run my code inside PyCharm's IDE it works perfectly fine and everything goes smoothly. Previously, when I compiled the file using pyinstaller everything worked fine, however all of a sudden (starting this morning) ONLY the .exe file that is created by pyinstaller returns an error. Everything works as expected until it comes time for the file to be uploaded to sharepoint, I then get this error:
NoneType object has no attribute 'text'
This is my code for the sharepoint upload function:
def sharepoint_uploader(upload_dir):
try:
global date_from, date_to, username, password
baseurl = '###'
basesite = '###'
siteurl = baseurl + basesite
localpath = upload_dir
remotepath = f"###_{date_from}_{date_to}.xlsx"
ctx_auth = AuthenticationContext(siteurl)
ctx_auth.acquire_token_for_user(username, password)
ctx = ClientContext(siteurl, ctx_auth)
with open(localpath, 'rb') as content_file:
file_content = content_file.read()
remotedir, name = os.path.split(remotepath)
file = ctx.web.get_folder_by_server_relative_path(remotedir).upload_file(name, file_content).execute_query()
print(f"Uploaded File to sharepoint with name: {name}")
except Exception as e:
print(f'Sharepoint upload failed with error: "{e}"')
(### is a valid link / name but I've removed it for privacy reasons).
I use the following command to compile it into an exe:
pyinstaller --onefile --add-binary "msedgedriver.exe;./" tdsAutomation.py
I've tried to create a new virtual environment, install all the packages again and recreate the executable but I still encounter the exact same error. Initially I thought this may be a link issue but again everything works inside PyCharm including the debugging option. After everything I could try I ended up putting in a bunch of print statements (can't really debug an exe like I can in pycharm) around each major part of the code, compiled into an exe, ran it and isolated the issue to be with the line:
file = ctx.web.get_folder_by_server_relative_path(remotedir).upload_file(name, file_content).execute_query()
I checked all variables and put in print statements to confirm none of the variables were returning blank / none. None were. At this point I'm lost, confused and don't know what to do. Any help would be really appreciated.
Things I've tried -
Code runs fine in PyCharm: The code works as expected when executed inside the PyCharm IDE.
Recreated virtual environment: Created a new virtual environment, reinstalled all packages, and tested again.
Recompiled executable: Recompiled the .exe file using PyInstaller after recreating the virtual environment.
Added print statements: Added print statements to isolate the issue, confirming that variables are not blank or None.
Isolated issue to SharePoint upload line: Identified that the error occurs specifically at the line responsible for uploading the file to SharePoint.
Checked all variables: Verified that all variables used in the SharePoint upload function are correctly set and not empty.
Tried logging errors: Attempted to log detailed error information to understand the failure better.
Upvotes: 0
Views: 46