Arcpy scripts keep giving an error "TypeError: expected string or bytes-like object"

1189
8
Jump to solution
10-11-2021 02:48 AM
Sahaja
by
New Contributor III

I am new to arcpy and most of python too. My new org has arcpy scripts (written by ex-employee) that would convert TAB files to shp and also create buffer zones in an shp. It was working great until we upgraded ArcGIS Pro to 2.8.3. With basic skills in python, I'm expected to debug this script. This is the script we are using -

#Import libraries
import geopandas as gpd

#Set directory as the same path as where this file is saved/pasted
abspath=os.path.abspath
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)

#Create a subfolder within the directory, folder called siteboundary
ProjectFolder=dname


#Locate the tab file and set as variable
#The script is locating a file that ends with "Site Boundary.TAB"
files=os.listdir(ProjectFolder)
Tabfile=[i for i in files if i.endswith('Site Boundary.TAB')]
def listToString(s): 
    
    # initialize an empty string
    str1 = " " 
    
    # return string  
    return (str1.join(s))
TabfilePath=ProjectFolder+'\\'+listToString(Tabfile)



#This creates the site boundary shapefile
Tabdata=gpd.read_file(TabfilePath,driver="MapInfo File")
ShapefilePath=ProjectFolder+r"\siteboundary.shp"
Tabdata.to_file(ShapefilePath)


#This creates the site's multiring buffer

#set arcpy environment
arcpyenv=ProjectFolder

#Allow arcpy to overwrite
arcpy.env.workspace=arcpyenv
arcpy.env.overwrite=True
arcpy.env.overwriteOutput = True

#Set multi ring buffer parameters 
distances=[200,500,1000,2000]
bufferunit="meters"
arcpy.analysis.MultipleRingBuffer('siteboundary.shp','sitebuffer.shp',distances,bufferunit,"distance","NONE")


#Print end message when done
arcpy.AddMessage("Conversion done,your file is located in"+arcpyenv)
print("Conversion done,your file is located in "+arcpyenv)'```


 [Here's the image of the error][1] 



  [1]: https://i.stack.imgur.com/z89Vs.png

We get the same error in all the PCs and also or all the scripts (json to shp and buffer and just buffer creation scripts). We are using scripts to semi-automate processes as we have to do plenty in a day.

I tried solving this issue from other sources' solutions, but none of them are relatable. For more details, after updating the ArcGIS Pro I had to clone the environment to run the scripts, I assume something might have happened there, but when I checked the installed libraries it had both arcpy 2.8 and geopandas 0.8.1 that are essential to run these scripts.

Another thing is, the scripts run only in one system where the ArcGIS is not updated and in the master of the machine's profile and none other. I would be very grateful if anyone could help me with this.

TIA

0 Kudos
1 Solution

Accepted Solutions
Sahaja
by
New Contributor III

I found that there was a problem with installing geopandas within ArcGIS Pro Python package management and also through anaconda prompt. So, I found this workaround on esri https://support.esri.com/en/technical-article/000024177. where you install geopandas through Python command prompt with this command line conda install geopandas libtiff=4.0.10. But this way, the notebooks in ArcGIS Pro will not work. So you have to execute this command on the same python command prompt conda install -c anaconda jupyter_client=5.3.1

Running these two codes in cloned environment resolved all the issues.

View solution in original post

8 Replies
MichaelVolz
Esteemed Contributor

What version of Pro are you upgrading from?

I would start putting in print statements to ensure that variables are being populated as expected.

0 Kudos
David_Brooks
MVP Regular Contributor

@Sahaja have you updated from an old Pro version or from ArcMap?

Python 3.6 handles binary differently to 2.7, so maybe this is a 2.7 script that needs updating?

In fact, you have Print statements in there, which you don't do at 3.6 anymore, so definitely an ArcMap script.


David
..Maps with no limits..
Sahaja
by
New Contributor III

Updated from 2.8.0 to 2.8.2 in one PC, in the other it was still in 2.8.0. 

But I found that the error is given when importing geopandas. the installed geopandas is 0.8.1

0 Kudos
JoeBorgione
MVP Esteemed Contributor

What are the errors you are getting?  Screen caps would be good...

That should just about do it....
0 Kudos
Sahaja
by
New Contributor III

Error.png

That's the error. I found that it's the problem in importing geopandas.

0 Kudos
Sahaja
by
New Contributor III

I found that there was a problem with installing geopandas within ArcGIS Pro Python package management and also through anaconda prompt. So, I found this workaround on esri https://support.esri.com/en/technical-article/000024177. where you install geopandas through Python command prompt with this command line conda install geopandas libtiff=4.0.10. But this way, the notebooks in ArcGIS Pro will not work. So you have to execute this command on the same python command prompt conda install -c anaconda jupyter_client=5.3.1

Running these two codes in cloned environment resolved all the issues.

JoeBorgione
MVP Esteemed Contributor

Good for you for sticking with it.  I have to ask, what is it that geopandas offers that is worth that much trouble?

That should just about do it....
0 Kudos
Sahaja
by
New Contributor III

An ex-employee wrote a few scripts that would convert site area in json or tab or any file to shp and also create a buffer of 200m,500m,1km,and 2km in one shp with one script. This makes our lives easy in reducing the robotic efforts every time we have to do a new report in the company.

Thank you for trying to figure out the issue with me

0 Kudos