<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python Script to Tool in Custom Toolbox in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695982#M53961</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I attempted to run both scripts and they both failed. The first script said folderPath is not defined and the second secript said that there was an invalid mxdname. Any ideas?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For the creation of the tool, are their any output variables that should be created?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 06 Feb 2012 13:51:21 GMT</pubDate>
    <dc:creator>BrittneyVenetucci</dc:creator>
    <dc:date>2012-02-06T13:51:21Z</dc:date>
    <item>
      <title>Python Script to Tool in Custom Toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695970#M53949</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am trying to create a tool from a script that repairs all broken data sources in an mxd. I have posted the code as follows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy, os&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;PYTHONPATH = "C:\Python26\ArcGIS10.0\lib\ntpath.pyc"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;folderpath = arcpy.GetParameterAsText(0)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;oldpath = arcpy.GetParameterAsText(1)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;newpath = arcpy.GetParameterAsText(2)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print 'running'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;i=0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#create a folder within a local folder to store the mxds&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;folderPath = r""&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;for filename in os.listdir(folderPath):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fullpath = os.path.join(folderPath, filename)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if os.path.isfile(fullpath):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; basename, extension = os.path.splitext(fullpath)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if extension.lower() == ".mxd":&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(fullpath)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; oldpath = ""&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newpath = r""&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #mxd.replaceWorkspaces(oldpath, "SDE_WORKSPACE", newpath, "SDE_WORKSPACE",True)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "ok"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.findAndReplaceWorkspacePaths(oldpath, newpath)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #mxd.save()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.save()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print '...', basename, extension, "finished"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However, when I run the tool it does not work. Any ideas as to what I have to set my input parameters as or if there is something wrong in my code?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Brittney&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jan 2012 16:04:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695970#M53949</guid>
      <dc:creator>BrittneyVenetucci</dc:creator>
      <dc:date>2012-01-31T16:04:07Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Tool in Custom Toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695971#M53950</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This line may cause issues.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;PYTHONPATH = "C:\Python26\ArcGIS10.0\lib\ntpath.pyc"&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Should be as below, if you ave to include it at all.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;PYTHONPATH = &lt;STRONG&gt;r&lt;/STRONG&gt;"C:\Python26\ArcGIS10.0\lib\ntpath.pyc"&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;You can see this link for help and repairing data sources.&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s30000004p000000"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s30000004p000000&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To format your post correctly, read this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code"&gt;http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jan 2012 16:27:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695971#M53950</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-01-31T16:27:30Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Tool in Custom Toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695972#M53951</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ok I got the tool to work but it is not saving the mxds.. am I missing a variable?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Brittney&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jan 2012 17:43:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695972#M53951</guid>
      <dc:creator>BrittneyVenetucci</dc:creator>
      <dc:date>2012-01-31T17:43:31Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Tool in Custom Toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695973#M53952</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I would try this&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;mxd.saveACopy(file_name, {version})&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jan 2012 17:59:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695973#M53952</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-01-31T17:59:28Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Tool in Custom Toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695974#M53953</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;PYTHONPATH is a Windows (or UNIX) environment variable that needs to be set &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;before&lt;/SPAN&gt;&lt;SPAN&gt; the script is run, if you need it. The purpose of that variable is to modify &lt;/SPAN&gt;&lt;STRONG&gt;sys.path&lt;/STRONG&gt;&lt;SPAN&gt; when Python launches. Sys.path is a list of folders that tells Python where to look for imports, ie "import mymodule" will look for a file "mymodule.py" or "mymodule.pyc" through the sys.path, and use the first one it finds.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It looks like you are trying to invoke the ntpath module. This module is already in the sys.path, so you could get to it with:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import ntpath&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However, you don't need to import it -- the os module is what you want (and what you are using).&amp;nbsp; I'm pretty sure os calls ntpath for you on windows, something else on Mac or Unix.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you need to alter where to look &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;at runtime&lt;/SPAN&gt;&lt;SPAN&gt;, you can alter the sys.path as follows. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import sys
sys.path.append(r"C:\mytools\python\modules")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can check out where imports will look by listing your sys.path:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;STRONG&gt;import sys&lt;/STRONG&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;STRONG&gt;sys.path&lt;/STRONG&gt;&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note, by default, "" is the first item in sys.path. So if you include a custom module in the same folder as the script, it will always use that version.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://docs.python.org/tutorial/modules.html" rel="nofollow noopener noreferrer" target="_blank"&gt;http://docs.python.org/tutorial/modules.html&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:17:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695974#M53953</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-12T05:17:59Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Tool in Custom Toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695975#M53954</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;&lt;HR /&gt;[/HR]&lt;/SPAN&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I would try this&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;mxd.saveACopy(file_name, {version})&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I had this originally in my code, but I simply just want to update the existing mxd's. (Since I will be running the tool multiple times to repair multiple sde databases within a single mxd.) &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Do I need to create a derived variable? If so, where/how would I call this out in my code?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help would be greatly appreciated.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jan 2012 20:22:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695975#M53954</guid>
      <dc:creator>BrittneyVenetucci</dc:creator>
      <dc:date>2012-01-31T20:22:59Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Tool in Custom Toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695976#M53955</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;What is the particular error you are getting? Or does it run through without error and just not make any changes to the mxd?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, posting your code with code blocks will make it easier for us to help.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jan 2012 21:47:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695976#M53955</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-01-31T21:47:39Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Tool in Custom Toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695977#M53956</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;What is the particular error you are getting? Or does it run through without error and just not make any changes to the mxd?&lt;BR /&gt;&lt;BR /&gt;Also, posting your code with code blocks will make it easier for us to help.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am not getting an error, rathre it is nto saving my mxds&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Feb 2012 12:06:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695977#M53956</guid>
      <dc:creator>BrittneyVenetucci</dc:creator>
      <dc:date>2012-02-02T12:06:56Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Tool in Custom Toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695978#M53957</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy, os
PYTHONPATH = "C:\Python26\ArcGIS10.0\lib\ntpath.pyc"
folderpath = arcpy.GetParameterAsText(0)
oldpath = arcpy.GetParameterAsText(1)
newpath = arcpy.GetParameterAsText(2)
print 'running'
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
i=0
#create a folder within a local folder to store the mxds
folderPath = r""

for filename in os.listdir(folderPath):

fullpath = os.path.join(folderPath, filename)

if os.path.isfile(fullpath):

basename, extension = os.path.splitext(fullpath)

if extension.lower() == ".mxd":

mxd = arcpy.mapping.MapDocument(fullpath)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
oldpath = ""
newpath = r""
#mxd.replaceWorkspaces(oldpath, "SDE_WORKSPACE", newpath, "SDE_WORKSPACE",True)
print "ok"
mxd.findAndReplaceWorkspacePaths(oldpath, newpath)
#mxd.save()
mxd.save()
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;print '...', basename, extension, "finished"&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:18:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695978#M53957</guid>
      <dc:creator>BrittneyVenetucci</dc:creator>
      <dc:date>2021-12-12T05:18:02Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Tool in Custom Toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695979#M53958</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have reposted my code. Does anyone have any ideas on why it is not wiriting over my mxd's? It works as a standalone script, but not as a tool in a toolbox.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Brittney&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Feb 2012 20:09:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695979#M53958</guid>
      <dc:creator>BrittneyVenetucci</dc:creator>
      <dc:date>2012-02-03T20:09:27Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Tool in Custom Toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695980#M53959</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;From the code you posted the only thing that I really notice is that it appears that you are changing your input variables before you use them.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;folderPath = r""&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;oldpath = ""&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;newpath = r""&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I took those out and changed the extention.lower() to extention.endswith (looked like you were checking if the file was an mxd?). I think it should work now.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;

import arcpy, os

#Parameters (workspaces)
folderpath = arcpy.GetParameterAsText(0)
oldpath = arcpy.GetParameterAsText(1)
newpath = arcpy.GetParameterAsText(2)

#Loop through folder
for filename in os.listdir(folderPath):
&amp;nbsp; fullpath = os.path.join(folderPath, filename)
&amp;nbsp; if os.path.isfile(fullpath):
&amp;nbsp;&amp;nbsp;&amp;nbsp; basename, extension = os.path.splitext(fullpath)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if extention.endswith("mxd"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(fullpath)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.findAndReplaceWorkspacePaths(oldpath, newpath)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.save() 

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If not...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;It might be easier to test the basic workflow then add in your loop.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You could try making a tool out of this...it will only do one mxd at a time but it should work. If you leave the input for oldPath empty it should update the workspaces for all the layers to the new path.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy

#Inputs
mxdIn = arcpy.GetParameterAsText(0) #ArcMapDocument
oldPath = arcpy.GetParameterAsText(1) #Workspace
newPath = arcpy.GetParameterAsText(2) #Workspace

#Set mxd
mxd = arcpy.mapping.MapDocument(mxdIn)

#Change workspace paths of layers
mxd.findAndReplaceWorkspacePaths(oldPath, newPath)

#Save mxd (overwrites mxd)
mxd.save()
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:18:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695980#M53959</guid>
      <dc:creator>BenjaminGale</dc:creator>
      <dc:date>2021-12-12T05:18:04Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Tool in Custom Toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695981#M53960</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;PYTHONPATH is a Windows (or UNIX) environment variable that needs to be set &lt;SPAN style="font-style:italic;"&gt;before&lt;/SPAN&gt; the script is run, if you need it. The purpose of that variable is to modify &lt;STRONG&gt;sys.path&lt;/STRONG&gt; when Python launches. Sys.path is a list of folders that tells Python where to look for imports, ie "import mymodule" will look for a file "mymodule.py" or "mymodule.pyc" through the sys.path, and use the first one it finds.&lt;BR /&gt;&lt;BR /&gt;It looks like you are trying to invoke the ntpath module. This module is already in the sys.path, so you could get to it with:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import ntpath&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;However, you don't need to import it -- the os module is what you want (and what you are using).&amp;nbsp; I'm pretty sure os calls ntpath for you on windows, something else on Mac or Unix.&lt;BR /&gt;&lt;BR /&gt;If you need to alter where to look &lt;SPAN style="font-style:italic;"&gt;at runtime&lt;/SPAN&gt;, you can alter the sys.path as follows. &lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import sys
sys.path.append(r"C:\mytools\python\modules")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;You can check out where imports will look by listing your sys.path:&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;STRONG&gt;import sys&lt;/STRONG&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;STRONG&gt;sys.path&lt;/STRONG&gt;&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;Note, by default, "" is the first item in sys.path. So if you include a custom module in the same folder as the script, it will always use that version.&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://docs.python.org/tutorial/modules.html" rel="nofollow noopener noreferrer" target="_blank"&gt;http://docs.python.org/tutorial/modules.html&lt;/A&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I attempted to run both scripts and they both failed. The first script said folderPath is not defined and the second secript said that there was an invalid mxdname. Any ideas?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For the creation of the tool, are their any output variables that should be created?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:18:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695981#M53960</guid>
      <dc:creator>BrittneyVenetucci</dc:creator>
      <dc:date>2021-12-12T05:18:07Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Tool in Custom Toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695982#M53961</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I attempted to run both scripts and they both failed. The first script said folderPath is not defined and the second secript said that there was an invalid mxdname. Any ideas?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For the creation of the tool, are their any output variables that should be created?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 06 Feb 2012 13:51:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695982#M53961</guid>
      <dc:creator>BrittneyVenetucci</dc:creator>
      <dc:date>2012-02-06T13:51:21Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Tool in Custom Toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695983#M53962</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Strange the second should have worked.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I made it into a tool and tested it on a mxd that I broke the data sources in and it appeared to work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'll put the tool I made below in case it will work for you. Just extract the toolbox and script into the same folder and specify an mxd and new path. Leaving the old path blank should update all the layers.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If it still doesn't work there may be a step I am over looking or am not aware of.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;What is the scenario for the new paths compared to the old ones? Is the data for the layers arranged or named differently?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EDIT:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I should note running the tool with the target mxd open appears to crash ArcMap.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 06 Feb 2012 18:09:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-tool-in-custom-toolbox/m-p/695983#M53962</guid>
      <dc:creator>BenjaminGale</dc:creator>
      <dc:date>2012-02-06T18:09:31Z</dc:date>
    </item>
  </channel>
</rss>

