Workplace Environments

2706
13
Jump to solution
04-27-2020 01:44 PM
BleysAndromeda-Focht
Emerging Contributor

I've hit a funny problem, I can't set/access my environment. I did a little research and found a similar problem (Not able to set workspace using standalone Python script for ArcGIS). I know that the script was working because it was it was fully functional for me on Friday, and today it can't find my workspace. I've gone ahead and included a shortened version of my script (and image of the notebook from Pro) that I was working with trying to troubleshoot this issue. It includes a commented out line, and some notes I added for clarity.

Thanks for any ideas, they are all welcome.

Import

import arcpy
from arcpy import env
from arcpy.sa import *
import os, numpy
import pandas as pd

Set environments - tried single and double backslashes, there are no spaces in my file name. Assigned file path to variable "w" just for troubleshooting this. That is not part of the code

w="C:\\GIS\\ArcHydro\\TBR\\AH_TBR\\AH_TBR.gdb"
arcpy.env.workspace="C:\\GIS\\ArcHydro\\TBR\\AH_TBR\\AH_TBR.gdb"

Confirmed that arcpy can read the chosen workspace - only from the filepath not from workspace

arcpy.Exists("C:\\GIS\\ArcHydro\\TBR\AH_TBR\\AH_TBR.gdb")

Confirm that I can print file path for my variable "w", yet workspace still doesn't exist.

print (w)
print (workspace)

Thank you!

Bleys

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

emulate...

workspace = arcpy.env.workspace = r"C:\Git_Dan\npgeom\Project_npg\npgeom.gdb"
print(workspace)
C:\Git_Dan\npgeom\Project_npg\npgeom.gdb

View solution in original post

13 Replies
DanPatterson_Retired
MVP Emeritus

workspace isn't defined... w is and arcpy.env.workspace is.  You can't do the 'workspace' shortcut

0 Kudos
BleysAndromeda-Focht
Emerging Contributor

So right you are. I only had the variable in there for troubleshooting this issue, so I tweaked my post to make that clear. Thanks for addressing that.

Prior to the weekend "workspace" was acting like it was defined. I had no trouble referring to files and setting the workspace:     

      "arcpy.env.workspace="C:\GIS\ArcHydro\TBR\AH_TBR\AH_TBR.gdb"

      DEM=workspace+"\DemX10"
      conveyances=workspace+"\Layers\Conveyances"
      walls_internal=workspace+"\Layers\internwalls"
      Ponds=workspace+"\Layers\Ponds"
      Pourpts=workspace+"\Layers\PourPoints"

I can include what is left of that code. My notebook inside Pro must have not saved correctly on Friday because I lost a lot of work. Is there anychance that a bad save could have had larger ramifications?

0 Kudos
DanPatterson_Retired
MVP Emeritus

No.  You must have defined 'workspace' earlier on.

arcpy.env.workspace = r"C:\Git_Dan\npgeom\Project_npg\npgeom.gdb"
workspace
Traceback (most recent call last):
File "<ipython-input-6-91a042f38ba6>", line 1, in <module>
 workspace
NameError: name 'workspace' is not defined
BleysAndromeda-Focht
Emerging Contributor

I am defining the workspace on my second line of code after calling arcpy with my first line of code defining the ArcHydro toolbox. I must be missing something, especially because the code ran with no issues on Friday.

0 Kudos
DanPatterson_Retired
MVP Emeritus

examine lines 74 - 76

Nowhere do you define a variable named "workspace"

Examine my example as well.

BleysAndromeda-Focht
Emerging Contributor

Thanks Dan. After sleeping on it I realize that I tricked myself into thinking that "workspace" was akin to a reserved variable in arcpy. Thus I wouldn't need to define the variable, just assign it via the arcpy.env.workspace and then use it freely the rest of the time.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Shall I mark this as "Assumed Answered" to close the thread or did one provide the solution?

0 Kudos
JoeBorgione
MVP Emeritus
#Import

import arcpy
from arcpy import env
from arcpy.sa import *
import os, numpy
import pandas as pd

 

'''Set environments - tried single and double backslashes, there are no spaces in my file name. Assigned file path to variable "w" just for troubleshooting this. That is not part of the code
'''

w="C:\\GIS\\ArcHydro\\TBR\\AH_TBR\\AH_TBR.gdb"
arcpy.env.workspace="C:\\GIS\\ArcHydro\\TBR\\AH_TBR\\AH_TBR.gdb"

 

'''Confirmed that arcpy can read the chosen workspace - only from the filepath not from workspace
'''

arcpy.Exists("C:\\GIS\\ArcHydro\\TBR\AH_TBR\\AH_TBR.gdb")

 
'''
Confirm that I can print file path for my variable "w", yet workspace still doesn't exist.
'''
print (w)
print (workspace)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I took the liberty of formatting your code so it's more easily readable.  Please do the same thing in the future.

As Dan mentions, no where in your code do you set a variable called workspace. Line 14 you set w but not workspace. 

You should get into the habit of using r strings which is raw text.  That way you can just copy and paste your paths and never ever have to worry about which slash to use or escape:

myFolder = r'C:\some\path\to\a\folder'‍‍
That should just about do it....
BleysAndromeda-Focht
Emerging Contributor

Thank you for reformatting my text for readability. I am really, really sorry I ever included the variable "w". It just made stuff confusing.

When my code was functioning last week, I was able to print (workspace) and it would print the path to the workspace, that is why I was using the print function; only to troubleshoot my own code.

The script was not finding my feature classes inside of the workspace when I attempted to rerun the code from last week, saying that workspace was not defined. Converting my file paths to raw text seemed to be the fix. Then Pro crashed on me and it is no longer working. The code dies on line 3, when I define my DEM to use. This was working fine for last week and so I am not quite sure would have changed.

#Import

import arcpy
from arcpy import env
from arcpy.sa import *
import os, numpy
import pandas as pd
AH=arcpy.ImportToolbox(r"c:\program files\arcgis\pro\Resources\ArcToolbox\toolboxes\Arc_Hydro_Tools_Pro.tbx")


#Set env & variables

arcpy.env.workspace=r"C:\GIS\ArcHydro\TBR\AH_TBR\AH_TBR.gdb"
folderspace=r"C:\\GIS\ArcHydro\\TBR\\AH_TBR\\"
DEM=workspace+r"\DemX10"
conveyances=workspace+r"\Layers\Conveyances"
walls_internal=workspace+r"\Layers\internwalls"
Ponds=workspace+r"\Layers\Ponds"
Pourpts=workspace+r"\Layers\PourPoints"
arcpy.env.snapRaster=DEM
#NAD 27 Alaska State Plane Zone 4 WKID Code: 26734
sr=arcpy.SpatialReference(26734)
arcpy.env.outputCoordinateSystem=sr
arcpy.env.overwriteOutput= True

#Convert FC to Rast
rast_convey=folderspace+"\\Layers\\convey_rast.tif"
arcpy.conversion.FeatureToRaster(conveyances,"TYPE", rast_convey)

#Fill Raster
Fill_1_out=folderspace+"\\Layers\\fil3.tif"
AH.FillSinks(DEM,Fill_1_out, None, Ponds)

‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos