Not able to set workspace using standalone Python script for ArcGIS

2673
28
08-21-2018 11:51 PM
HarshalGharat
New Contributor II

I am facing a very weird issue in setting the workspace. When I setup the workspace inside python window in ArcMap/ArcCatalog, it just works fine. But the moment I do the same in a standalone python script, the things do not work at all. I tried other options like creating connection file in scratch folder and then setting it as a workspace but still no luck. I am facing this issue on ArcGIS Desktop 10.2.1 and my database is 64 bit Oracle.

I had ArcGIS Pro 2.1 installed on the same machine hence I also tried the same thing after uninstalling ArcGIS Pro but still no luck.

0 Kudos
28 Replies
NeilAyres
MVP Frequent Contributor

Can you browse the database in ArcCatalog once you have opened the connection file?

Just setting env.workspace = "my connection file.sde" has always worked for me.

What happens?

0 Kudos
HarshalGharat
New Contributor II

Hi Neil,

Yes, I can browse the database in ArcCatalog/ArcMap and walk around the featureclasses/datasets etc. But this is driving me crazy as env.workspace = "my connection file.sde" is no way working for me.

0 Kudos
NeilAyres
MVP Frequent Contributor

What happens when you set env.workspace = "my connection file.sde" in your python script?

Are you using os.path.join() to construct the path pointer to the connection file? Or just raw strings.

HarshalGharat
New Contributor II

I set up my workspace in the following manner

arcpy.env.workspace = r"Database Connections\Connection to sde.sde"

# Just for sake of testing, I am trying to print all the datasets

print arcpy.ListDatasets()

The above code snippet prints all the datasets list if I run it in python window withing ArcMap but If I try to run the same snippet in a script, it Print "None"

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

in a standalone script it will need more information that provided since the script has no information as to where the project is.  Can you use a full path to your database connection

Create Database Connection—Data Management toolbox | ArcGIS Desktop 

0 Kudos
HarshalGharat
New Contributor II

Hi Dan,

Thanks for the suggestion. I have tried that option as well and it didn't work.

0 Kudos
NeilAyres
MVP Frequent Contributor

This is not going to work:

arcpy.env.workspace = r"Database Connections\Connection to sde.sde"

The script has no idea where your user profile will be.

Copy the full path as Joe suggests.

Or copy the sde file to a local space and use that.

0 Kudos
HarshalGharat
New Contributor II

I have tried with full path as well, no luck!

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Let's see exactly what your are using.

can't wait to retire....
0 Kudos