When you run a script in ArcMap (Python window or tool) and you have a workspace set but a different layer with the same name exists in the TOC, which will be used?
I did a test with get_count, it takes the one in the TOC and ignore the workspace. When I delete the layer from the TOC I got the correct layer from the workspace.
Is this a bug??
That is what I would expect if you are running a script with arcmap open.
If there is any doubt, you should use full paths to files rather than relying on a workspace
I am not sure I agree. In windows when you set your PATH the order in that variable is the ONLY thing that setting the order of search for a programs. Even if a program exists in the current directory it will still look for it in the first element in PATH. If you set your workspace it mean that this is the place you want the data to come from, not the TOC.
Your observation is correct in your first question and it has been my experience TOC rules.
When using tools and defining input parameters, I always use the featurelayer option (or rasterlayer etc) so that the user has the option to accept the layers in the ToC or to navigate to a file on disk (with or without the same name), The selection is never populated with anything from any preset path. If I am running a standalone script with arcmap closed, then you have to set an arcpy.env.workspace if you plan to use any of arcpy's *list* features.
This doesn't have to do with the TOC, per se, it is a matter of layers and views being searched before data sources. When a full path is not specified, ArcPy searches feature layers and table views for a name match first. If a name match isn't found there, the workspace is searched for a data source with the matching name.
This same situation can be replicated in a stand alone script outside of ArcGIS Desktop:
>>> import arcpy >>> import os >>> >>> gdb = # path to gdb with test tables >>> arcpy.env.workspace = gdb >>> >>> # table "test_1" has 1,000,000 records while "test_ovr" has 11 records >>> arcpy.MakeTableView_management("test_ovr", "test_1") >>> arcpy.GetCount_management(os.path.join(gdb, "test_1")) <Result '1000000'> >>> arcpy.GetCount_management("test_1") <Result '11'> >>> arcpy.Delete_management("test_1") <Result 'true'> >>> arcpy.GetCount_management("test_1") <Result '1000000'> >>>