Select to view content in your preferred language

Import arcpy in 10.1

3023
8
02-26-2014 09:00 AM
SimonLarkin
Emerging Contributor
I've been shuffling along on multiple threads trying to find a solution to the "import arcpy" function. I'm not a programmer or do I understand python very well. I'm essentially trying to create a model which I can run in task manager. From my limited understanding, I have to:
- Create a model in Model Builder
- Export the code as Python script
- Schedule a task in Task Manager

I've read that using different version of Python will mess with how my Python interface interacts with Arc. I currently have Python 2.7 installed along with ArcGIS Desktop 10.1. I previously had Python 3.4 installed but I read something about Arc 10.1 not being compatible with the 64-bit Python 3.4.. I'm not sure if I have any residual files messing with Python but importing arcpy doesn't work. I get the following error:

[INDENT]Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import arcpy
ImportError: No module named arcpy[/INDENT]

Is there a simple way to solve this problem. (Maybe a step by step?) Simply put, I want to copy files from a geodatabase to a "backup" version on a weekly basis. Maybe I'm taking the wrong approach?

Any help would be greatly appreciated.
Cheers,
Simon
Tags (2)
0 Kudos
8 Replies
JoshuaChisholm
Frequent Contributor
Hello Simon!

A few Notes:

  1. First off, I think 'Task Manager' and 'Task Scheduler' are different things. It sounds like you're looking for 'Task Scheduler'.

  2. Secondly, unless you doing really fancy stuff with python, you don't need to download your own copy. ArcMap 10 (and 10.1) come with a copy. Since you have multiple copies of Python, the wrong one is getting used by default. This version being run is not the ArcGIS version and therefore can't find "arcpy". You should uninstall all copies of python except the one that came with ArcGIS (it should be located here: C:\Python27\ArcGIS10.1). Note that there may be a different copy of python installed here: C:\Python27.

  3. Third, are you copying the entire geodatabase? If so, you can just use the robocopy command (or another similar windows based command). You would do this by creating a ".bat" file instead of a ".py" file. This may be easier since it would it a simple windows command.


Let us know if you have any questions or need any help scripting. Good luck Simon!
0 Kudos
SimonLarkin
Emerging Contributor
Hello Simon!

A few Notes:

  1. First off, I think 'Task Manager' and 'Task Scheduler' are different things. It sounds like you're looking for 'Task Scheduler'.

  2. Secondly, unless you doing really fancy stuff with python, you don't need to download your own copy. ArcMap 10 (and 10.1) come with a copy. Since you have multiple copies of Python, the wrong one is getting used by default. This version being run is not the ArcGIS version and therefore can't find "arcpy". You should uninstall all copies of python except the one that came with ArcGIS (it should be located here: C:\Python27\ArcGIS10.1). Note that there may be a different copy of python installed here: C:\Python27.

  3. Third, are you copying the entire geodatabase? If so, you can just use the robocopy command (or another similar windows based command). You would do this by creating a ".bat" file instead of a ".py" file. This may be easier since it would it a simple windows command.


Let us know if you have any questions or need any help scripting. Good luck Simon!


Thanks Joshua!
I did mean Task Scheduler, you're right.
I would love Scripting help as I am almost illiterate when it comes to programming. I do want to copy an entire geodatabase into a different one to act as a "backup". I'm not sure whether Python or Windows Command is better but my geodatabases contain shapfiles in which I do not want to loose any attributes. I'm looking for a simple way to do this and automate it so I don't have to think about it anymore. I've deleted all other versions of Python but I'm not sure what comes next. I found the "C:\Python27\ArcGIS10.1" folder and I believe it to now be the only version of Python on my computer. I don't think my task is an impossible one but I'm stuck when it comes to finding a solution.
Your expertise is highly appreciated,
Simon
0 Kudos
JoshuaChisholm
Frequent Contributor
Hello Simon,

This won't be a problem at all.

I think I'm being a bit of a jerk on technicalities, but I want to make sure I'm understanding you correctly. I don't think a geodatabase can store shapefiles. You likely mean feature classes.

I have a few more quick questions too:

  1. Are you copying one GDB or a much of GDBs? If the latter, are they all in the same folder?

  2. For the backup, do you want one backup (from the most recent time the backup was ran)? Or do you want a each backup in a folder named after the date of the backup. The latter might be a more secure method, but would take up a lot more space.


Let me know and we'll be able to figure something out!
0 Kudos
MathewCoyle
Honored Contributor
Your pythonpath system variable is most likely set to your Python 3 version. Try following the steps in the following link, depending on how you want to resolve this.

https://stackoverflow.com/questions/7472436/add-a-directory-to-python-sys-path-so-that-its-included-...

I would recommend uninstalling and reinstalling your entire ArcGIS and Python installations.
0 Kudos
SimonLarkin
Emerging Contributor
Hello Simon,

This won't be a problem at all.

I think I'm being a bit of a jerk on technicalities, but I want to make sure I'm understanding you correctly. I don't think a geodatabase can store shapefiles. You likely mean feature classes.

I have a few more quick questions too:

  1. Are you copying one GDB or a much of GDBs? If the latter, are they all in the same folder?

  2. For the backup, do you want one backup (from the most recent time the backup was ran)? Or do you want a each backup in a folder named after the date of the backup. The latter might be a more secure method, but would take up a lot more space.


Let me know and we'll be able to figure something out!



Hi again,
So I've spoke to a colleague and here's what I understand. We have a bunch of feature classes (you were right again) on a "postgress" server that we want to backup into a file geodatabase. New copies can overwrite old ones. Does this help clarify?
0 Kudos
JoshuaChisholm
Frequent Contributor
Hello Simmon,

Ok, so given that you're going between two data types (SQL SDE and GDB), robocopy (windows command) is out. arcpy it is!

I tested this for a gdb to gdb transfer. I think it should for a SQL SDE too, but let me know if you run into problems.
print "Starting Backup..."
import arcpy

#arcpy.env.workspace = r"Database Connections\NameOfYourDataBase.sde"
backupFolder=r"C:\Path\To\backupFolder"
backupGDB="DataBackup.gdb"
if not arcpy.Exists(backupFolder+'\\'+backupGDB):
 arcpy.CreateFileGDB_management(backupFolder, backupGDB)

featureClasses=arcpy.ListFeatureClasses() 

for fc in featureClasses:
    print "Backuping up: "+fc
    if arcpy.Exists(backupFolder+'\\'+backupGDB+'\\'+fc):
        arcpy.Delete_management(backupFolder+'\\'+backupGDB+'\\'+fc)
    arcpy.FeatureClassToFeatureClass_conversion(fc, backupFolder+'\\'+backupGDB,fc)

print "Backup complete!"

Let me know if it works out!
0 Kudos
SimonLarkin
Emerging Contributor
Hi Joshua,
Sorry about the slow response, I've been swamped for the last two weeks. I can finally get back to this project. So, today I've discovered our geodatabase comes from an online source. I have a data Type: (query feature class), a server: (IP), a client: (posgresql), a connection properties: (same IP as server), a database: (postgis20), a user name, a feature type: (Simple), a Geometry Type: (Points and Lines and Polygons), Coordinates have Z values: (Yes), and coordinates have measures: (No). I'm told we have a "mapped drive" (which I'm not entirely sure what that means). I'm not sure how all this fits into the equation but I'm guessing it does. When I tried running the code you've written, I get "Starting Backup..." and nothing else.
I really do appreciate you taking the time to help me, I'd +1 you if this were Google+.
Cheers,
Simon
0 Kudos
JoshuaChisholm
Frequent Contributor
Hello Simon,

Thank you for the kind words. If we do sort this out you can give me a green check! My appologizes in advance, I'm a little rusty with SDE connections.

The first thing we need to do is to make sure you have a connection to the database. Open ArcCatalog and go to Database Connections. Hopefully you'll already be connected to the geodatabase. If you can not find the connection, click Add Database Connection. Either way, make sure the right data is coming through and take note of the name of the connection.

Secondly, check if that connection exists in this folder (it should): C:\Users\[username]\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog. I think it will be a '.sde' file, but please tell me exactly how it shows up.

Let me know how this goes and we'll continue from there.
0 Kudos