Select to view content in your preferred language

TypeError: can only concatenate str (not "NoneType") to str

9349
28
Jump to solution
08-15-2022 08:02 AM
ABishop
MVP Regular Contributor

Hello,

I hope this is posted in the right place.  I am troubleshooting a script that I have been running for almost a year now which updates a hosted feature service in ArcGIS Online.  We recently upgraded our ArcMap and Server to 10.8.2 and ArcGIS Pro 3.0.1.  I am wondering if this update has something to do with it?  Anyhow, I have posted the python  code below and attached a pic of the TypeError I receive when I try to run the script from Python 3 IDLE.  Please help. 

import arcpy
import os, sys
from arcgis.gis import GIS

### Start setting variables
# Set the path to the project
prjPath = "I:\\users\\abishop\\PY\\FeatureServiceUpdates\\AgUseParcels\\AgUseParcels.aprx"

# Feature service/SD name in arcgis.com, user/password of the owner account
sd_fs_name = "Agricultural_Use_Parcels"
portal = "https://mcpafl.maps.arcgis.com/" # Can also reference a local portal
user = "***************"
password = "*************"

# Set sharing options
shrOrg = True
shrEveryone = False
shrGroups = ""

### End setting variables

# Local paths to create temporary content
relPath = sys.path[0]
sddraft = os.path.join(relPath, "AgUseParcels.sddraft")
sd = os.path.join(relPath, "AgUseParcels.sd")

# Create a new SDDraft and stage to SD
print("Creating SD file")
arcpy.env.overwriteOutput = True
prj = arcpy.mp.ArcGISProject(prjPath)
mp = prj.listMaps()[0]
arcpy.mp.CreateWebLayerSDDraft(mp, sddraft, sd_fs_name, 'MY_HOSTED_SERVICES', 'FEATURE_ACCESS','', True, True)
arcpy.StageService_server(sddraft, sd)

print("Connecting to {}".format(portal))
gis = GIS(portal, user, password)

# Find the SD, update it, publish /w overwrite and set sharing and metadata
print("Search for original SD on portal…")
sdItem = gis.content.search("{} AND owner:{}".format(sd_fs_name, user), item_type="Service Definition")[0]
print("Found SD: {}, ID: {} n Uploading and overwriting…".format(sdItem.title, sdItem.id))
sdItem.update(data=sd)
print("Overwriting existing feature service…")
fs = sdItem.publish(overwrite=True)

if shrOrg or shrEveryone or shrGroups:
    print("Setting sharing options…")
    fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)

print("Finished updating: {} – ID: {}".format(fs.title, fs.id))

#provide input to stop script - uncomment input below if you need to see the script final output in the command window
input('Press ENTER to exit') 
Amanda Bishop, GISP
0 Kudos
28 Replies
Ramon_de_Leon
Frequent Contributor

Thanks Brian, that worked like a charm....

It's weird that it works for other projects that I have (SD file not saved at root folder), but for this particular project I need to move the SD file into the root folder. But I'll take it 🙂

0 Kudos
BrianPangtay
Regular Contributor

Only services generated from an SD file that exceeds a file size of 23-24MB trigger the error. The other unaffected services must have SD files that are smaller. 

Ramon_de_Leon
Frequent Contributor

That certainly explains it, my SD file is 32 MB. Thanks again, this is good to know for future projects that I need to do.

0 Kudos
MichaelSnook
Frequent Contributor

This should definitely be marked as the solution to this issue.  Truncating/appending is only a work around to the root issue.  You saved me a ton of time trying to track this down.

Thanks!

0 Kudos
BrianPangtay
Regular Contributor

Truncate/Append is appropriate if the goal is just updating the content of the feature service. The other script method is needed if the schema and/or symbology of the data changed.

0 Kudos
KentuckyDGI
Emerging Contributor

Are there plans to fix this?

0 Kudos
davedoesgis
Frequent Contributor

I was trying to do an overwrite, which sounds like a similar operation. I was getting the exact same error. A few notes on this: 

  • I solved the issue using pip to upgrade the ArcGIS API for Python. See below for instructions. 
  • My code worked fine in Pro 2.9.x, which I think used `arcgis` version 1.x. It started breaking when I upgraded to Pro 3.0.2, which comes with `arcgis` v2.0.1. I don't know what version of the API the issue was fixed in, but as of recently, version 2.1.0 was the latest on PyPI, and it solved the problem. 
  • I used breakpoints in the Python code to trace it to a function that accepts a user name and folder name, returning the AGOL item ID of a folder. The function was called incorrectly with the ID, not the name, and it returned `None`. The line after calling that function, the code tried to append the result to a string, which raised an exception. (Of course, it also raises the question why they're calling a function to get a folder ID if they already have that ID 😉). 
  • I was also seeing the problem go away when I used a smaller dataset. That's a real head-scratcher, because it doesn't seem related to the folder issue, but maybe smaller datasets are handled with a different protocol or something. 
  • I don't think I tried putting my item in the root folder. 

 

To resolve this, I cloned a new Conda environment. To upgrade `arcgis`, follow these steps:

> activate <env_name>
> cd C:\Users\<username>\AppData\Local\ESRI\conda\envs\<env_name>\Scripts
> .\pip.exe install arcgis --upgrade 

Note: If you use a proxy server, pip will have additional parameters for that. 

 

To check the API version in your default Conda environment and a cloned env: 

(arcgispro-py3) C:\>python
Python 3.9.11 [MSC v.1931 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import arcgis
>>> arcgis.__version__
'2.0.1'
>>> exit()

(arcgispro-py3) C:\>activate <env_name>

(<env_name>) C:\>python
Python 3.9.11 [MSC v.1931 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import arcgis
>>> arcgis.__version__
'2.1.0'

 

 

0 Kudos
ArizonaGIS
Occasional Contributor

@ABishop, Were you able to figure out a solution, or was truncating/appending the only option? I have a feature service with about 45 IDs with a combination of layers and tables. I have been doing some testing, and it appears the mixing of layers/tables is causing the issue. Manually doing the update through AGOL works without anything problems.

It would be much easier if the overwrite function worked as intended with the ArcGIS Python API, but the world can't be perfect I guess.

0 Kudos
davedoesgis
Frequent Contributor

See here for how I solved it: https://community.esri.com/t5/geoprocessing-questions/typeerror-can-only-concatenate-str-not-quot/m-...

Not sure if that'll solve your problem, but that's how I did it. Took hours to troubleshoot and 5 minutes to fix. 

0 Kudos