python hangs on raw_input

7878
5
03-28-2013 05:25 AM
ChrisJ
by
New Contributor III
I cannot overstate now new I am to python, so bear with me...

haven't had this before; one particular bit of raw_input is causing my pyscripter to just hang and never come back; I need to stop it in order to carry on:

import easygui
import arcgisscripting
import re
import dbfpy
from dbfpy import dbf
import sqlite3
import os

# create the geoprocessor object
gp = arcgisscripting.create(9.3)

# hangs on this for some reason, WHY?
workingfolder = raw_input('working folder please')
print workingfolder
#gp.workspace = workingfolder

# so do this instead for now
#gp.workspace = "I:\PDtest"

#easygui.fileopenbox(msg=None, title=None, default="*.shp", filetypes=None)

layer1 = easygui.fileopenbox(msg=None, title=None, default="I:\PDtest\*.shp", filetypes=None)
layer2 = easygui.fileopenbox(msg=None, title=None, default="I:\PDtest\*.shp", filetypes=None)

I just want the user to drop in a working directory that can be used from then on for the easygui stuff, fine, but, after printing the workingfolder string (which it does), pyscripter never carries on and I need to stop it manually. the string doesn't matter.

thanks for your help guys...

cj
Tags (2)
0 Kudos
5 Replies
MikeMacRae
Occasional Contributor III
From my experience, the only time I use
raw_input
or
input
in python 3.x, is when I'm running python from the Shell and I want to input a value as the code is being read. So, it pauses on that line and waits for you to input something. Logically, you cannot enter a workspace in Shell because you cannot navigate to a workspace, you can only enter something like an integer or a string.

I'm not familiar with easygui, but it sounds like a graphic interface of some kind you're building around. Are you using the interface coupled with an input parameter to enter the workspace? or are you just running from pyscripter? If it's the latter, then it will hang until you enter a value which, as I mentioned, I don't think you can do it this way. If it's the former, then I would read up on input parameters and see how easygui picks them up from your script. As an example, in ArcGIS, using the default tool wizard gui, you would set some parameters at the top of your script to read them from the GUI:

import arcpy

arcpy.GetParameter(0)
arcpy.GetParameterAsText(1)


Because this is an ESRI forum, you can read up on how ESRI handles parameters and then apply that knowledge to your script:

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Understanding_script_tool_parameters/0...

Also, some simple explanation of raw_input is here. this might help understand a little better.:

https://sites.google.com/site/usfcomputerscience/basic-input-output
0 Kudos
markdenil
Occasional Contributor III
raw_input can be a simple way of accepting user input, BUT
you will likely want to test the input to see if it is a valid path and / or workspace, AND,
as was mentioned, raw_input only works on a command line.

It likely hangs because it doen't know where the next keystroke is coming from.

If you are using a GUI module (and I am not familair with easygui), then it surely has a user input widgit of some sort.
(isn't that what GUIs are all about?)
0 Kudos
ChrisJ
by
New Contributor III
thankyou both for your kind responses...

again I am astonishingly novice with python, and intermediate with arc (having come from a mapinfo background).  I'm using pyscripter and running these things from within pyscripter, which I'm hardly sure is the right thing to do but I have gotten some expected results.  I see that I can also specify a .py script in the toolbox, though I'm not sure what the benefits are either way.  also, I'm using arcgis 9.3 rather than 10, if that matters.

I've quickly tried some of the GetParametersAsText(#) from within pyscripter, which failed, but I've only just started, will give that a further tinker and see how it goes...

thanks again,

cj
0 Kudos
MikeMacRae
Occasional Contributor III
If you're working in 9.3, make sure you look at the help menu for 9.3. The arcpy module wasn't introduced until 10.0. You will need to use the geoprocessing module to get your parameters if you wish to use the ESRI tool GUI.

http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=GetParameterAsText_method

gp.GetParameterAsText(0)


instead of

arcpy.GetParameterAsText(0)


Pyscripter is an OK debugging program. That won't be your issue.

Using the .py in your GUI toolbox is expected. It just means your tool GUI uses a .py (python file) to read your script. That helps the GUI understand that it is a python written script. You just set the GUI to the .py file and then it reads though the script, picking up the parameters as inputs. The GUI is just a nice interface that prompts the user to choose the parameter type you want (i.e. your workspace location) and then your write the script to take that input and do something with it, depending on what you write it to do 🙂
0 Kudos
ChrisJ
by
New Contributor III
thanks blm for the idea; makes sense and I did have a geoprocessing module in use, ready to use. however I am still getting stuck:

gp = arcgisscripting.create(9.3)

inWorkspace = gp.GetParameterAsText(0)
layer1 = gp.GetParameterAsText(1)
layer2 = gp.GetParameterAsText(2)

print layer1
print layer2
print inWorkspace

gp.workspace = inWorkspace
# process
gp.PointDistance_analysis(layer1,layer2,"temp.dbf","#")

...this 'failed to execute, parameters are not valid'; printing each variable to see what happened produces nothing either. bear in mind I am quite a novice of course...

thanks again for all help offered...

cj
0 Kudos