Python Script to Tool in Custom Toolbox

2604
13
01-31-2012 08:04 AM
BrittneyVenetucci
New Contributor
Hi all,

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:
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'

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)
           
            oldpath = ""
            newpath = r""
            #mxd.replaceWorkspaces(oldpath, "SDE_WORKSPACE", newpath, "SDE_WORKSPACE",True)
            print "ok"
            mxd.findAndReplaceWorkspacePaths(oldpath, newpath)
            #mxd.save()
            mxd.save()
      
           
        print '...', basename, extension, "finished"

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?

Thanks!

Brittney
Tags (2)
0 Kudos
13 Replies
MathewCoyle
Frequent Contributor
This line may cause issues.
PYTHONPATH = "C:\Python26\ArcGIS10.0\lib\ntpath.pyc"

Should be as below, if you ave to include it at all.
PYTHONPATH = r"C:\Python26\ArcGIS10.0\lib\ntpath.pyc"

You can see this link for help and repairing data sources.
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s30000004p000000

To format your post correctly, read this.
http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code
0 Kudos
BrittneyVenetucci
New Contributor
Ok I got the tool to work but it is not saving the mxds.. am I missing a variable?

Thanks,

Brittney
0 Kudos
MathewCoyle
Frequent Contributor
I would try this
mxd.saveACopy(file_name, {version})
0 Kudos
curtvprice
MVP Esteemed Contributor
PYTHONPATH is a Windows (or UNIX) environment variable that needs to be set before the script is run, if you need it. The purpose of that variable is to modify sys.path 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.

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:

import ntpath


However, you don't need to import it -- the os module is what you want (and what you are using).  I'm pretty sure os calls ntpath for you on windows, something else on Mac or Unix.

If you need to alter where to look at runtime, you can alter the sys.path as follows.

import sys
sys.path.append(r"C:\mytools\python\modules")


You can check out where imports will look by listing your sys.path:
>>> import sys
>>> sys.path


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.

http://docs.python.org/tutorial/modules.html
0 Kudos
BrittneyVenetucci
New Contributor

[/HR]
I would try this
mxd.saveACopy(file_name, {version})


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.)

Do I need to create a derived variable? If so, where/how would I call this out in my code?

Any help would be greatly appreciated.
0 Kudos
MathewCoyle
Frequent Contributor
What is the particular error you are getting? Or does it run through without error and just not make any changes to the mxd?

Also, posting your code with code blocks will make it easier for us to help.
0 Kudos
BrittneyVenetucci
New Contributor
What is the particular error you are getting? Or does it run through without error and just not make any changes to the mxd?

Also, posting your code with code blocks will make it easier for us to help.


I am not getting an error, rathre it is nto saving my mxds
0 Kudos
BrittneyVenetucci
New Contributor
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'


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)


oldpath = ""
newpath = r""
#mxd.replaceWorkspaces(oldpath, "SDE_WORKSPACE", newpath, "SDE_WORKSPACE",True)
print "ok"
mxd.findAndReplaceWorkspacePaths(oldpath, newpath)
#mxd.save()
mxd.save()


print '...', basename, extension, "finished"
0 Kudos
BrittneyVenetucci
New Contributor
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.

Thanks,

Brittney
0 Kudos