Workplace Environments

2711
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
13 Replies
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
BleysAndromeda-Focht
Emerging Contributor

That worked perfectly. Thank you.
The bigger question for me now is why was I able to use "workspace" before without properly defining it before using it. I sure feel lame for missing the obvious answer here, thanks again.

0 Kudos
DanPatterson_Retired
MVP Emeritus

My guess? 

You defined 'workspace' as a variable it during the python session... maybe before shortening it to 'w'.

The name,  'workspace' renames in the python namespace until the session is closed... not the script ends, but python is shutdown.

If you used a python IDE, like spyder that stores your history, you can review all your definitions and changes when you need to track down answers to mysteries.

Otherwise, The true answer will come in a dream .  

0 Kudos
JoeBorgione
MVP Emeritus

I try to avoid naming variables that are included in function names.  For example:

string = 'abcde'  #no no
theString = 'abcde'  #better

workspace =  r'C:\blah\blah\blah\fgdb.gdb'  #nope
ws = r'C:\blah\blah\blah\fgdb.gdb'
arcpy.env.workspace = ws
That should just about do it....