Python IDE complete code autocompletion

6817
8
Jump to solution
03-09-2015 08:37 AM
OskarKarlsson1
New Contributor

Hello

I am seeing that code completion in both PyScripter and PyCharm is not complete, compared to the code completion you get in the interpreter window. In PyScripter, if you add arcpy to the code completion IDE options, you will get all the options, but having this added, causes errors in the program, for some reason. Thats why I am testing with PyCharm. In PyCharm the issue is the same, in the editor window I am unable to see all the options I see in the interpreter window while typing. Anyone know why or how to fix?

Thank you all

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

You make a good point, i.e., each IDE handles things a bit different so some parts of my statements might not apply to others using different IDEs.  That said, most of what I say is generally enough to apply on some level to most IDEs.

Regarding the "import arcpy" question, it is an apples to oranges issue, or at least Granny Smith to Honeycrisp.  An IDE code editor can't do name completion on a Python site package/library/module/etc... that isn't imported.  The import statement is used by the code editor to expand code completion beyond the default or base Python packages, but that normally doesn't provide access to dynamic properties like ArcPy environment settings.

In PyScripter when you add a package like ArcPy to the code completion special packages, it actually executes code to see all the run-time items you see in the interactive console and then caches them for use in the code editor.  It isn't "seeing" those items like normal items, it had to execute code to find the items and then build a cache of them; hence, why you have to put arcpy in the special packages list to see them all.

With PyCharm, there is a "Collect run-time types information for code insight" option that will build similar caches.  With ArcPy, however, that feature doesn't seem to work very well.  Whether the problem lies in Esri, JeBrains, or both I do not know.

View solution in original post

8 Replies
JoshuaBixby
MVP Esteemed Contributor

Can you give a specific example? 

0 Kudos
OskarKarlsson1
New Contributor

Absolutely, for example when typing arcpy.env. I can see different options if I am in the editor or in the interpreter windows, within the saem IDE software, in this case PyCharm

Editor

2015-03-09 16_47_39.png

Interpreter window

2015-03-09 16_48_14-.png

Thanks

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I can give a partial answer now.  I have to check a couple of things before giving the other part of my answer.

Some of the difference is due to the interactive Python console hiding Python's dunder/magic/special methods from the user.  Python special methods are used for managing namespaces and implementing Python's approach to operator overloading.  Since the are "special," it is assumed the end user doesn't need to be interacting directly with them, so the interactive console doesn't list them.

Note, what you see in the PyCharm interactive console differs from what you see in the ArcGIS Desktop interactive console.  The PyCharm interactive console will show more of the special methods than the ArcGIS Desktop interactive console.

OskarKarlsson1
New Contributor

I see your point, but what its "hidden" doesnt seem special to me, maybe even the contrary, as for example arcpy.env.workspace is basic in any arcpy script, and its not shown. More than that, seems that the geoprocesses are the ones not showing.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

arcpy.env.workspace is not a Python special method, it is an ArcPy environment setting exposed as a property on ArcPy's env class.  When ArcPy is instantiated, the environment settings get defined and stored as a set in one of the environment object's internal or private attributes, _environments to be specific.  You can use the Python dir() built-in function to see those environment settings, but you also see lots of other class attributes as well.  The following code snippet shows only the environment settings:

import arcpy
for env in arcpy.env._environments:
    print env

The interactive console is retrieving those environment settings and making them available for code completion to the user.  The set of environment settings doesn't exist until an instance of ArcPy is created, so a user doesn't see them for code completion in the editor window.

I can't say I am doing complete justice with this explanation, but I find it hard to get much more in depth without getting into the weeds of Python class structure and how properties are defined and managed.

I think Esri would argue one should use the online documentation to understand ArcPy and not rely on code completion in a code editor or interactive window.  Is Esri's implementation of the ArcPy Env class frustrating because of how it impacts code completion in editor windows, I think so; but I also think this factor is low on the list when considering how to structure ArcPy.

OskarKarlsson1
New Contributor

I think I understand what you mean but this brings to my attention a couple things. If those environments are not created until the import arcpy is actually executed, how come that if you dont type "import arcpy" in the editor none of the arcpy function is available in code completion. Once you type that in the editor, many thing become available, but not all as we are discussing.  Also, in PyScripter if you add arcpy to the IDE options, code completion, it actually shows them in the Editor (too bad this causes a whole bunch of other issues).

I guess that ESRI could argue that, but what I am looking for here, is just a more efficient manner of typing, in order to avoid errors or typos, not a manner of not understanding how ArcPy works.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

You make a good point, i.e., each IDE handles things a bit different so some parts of my statements might not apply to others using different IDEs.  That said, most of what I say is generally enough to apply on some level to most IDEs.

Regarding the "import arcpy" question, it is an apples to oranges issue, or at least Granny Smith to Honeycrisp.  An IDE code editor can't do name completion on a Python site package/library/module/etc... that isn't imported.  The import statement is used by the code editor to expand code completion beyond the default or base Python packages, but that normally doesn't provide access to dynamic properties like ArcPy environment settings.

In PyScripter when you add a package like ArcPy to the code completion special packages, it actually executes code to see all the run-time items you see in the interactive console and then caches them for use in the code editor.  It isn't "seeing" those items like normal items, it had to execute code to find the items and then build a cache of them; hence, why you have to put arcpy in the special packages list to see them all.

With PyCharm, there is a "Collect run-time types information for code insight" option that will build similar caches.  With ArcPy, however, that feature doesn't seem to work very well.  Whether the problem lies in Esri, JeBrains, or both I do not know.

OskarKarlsson1
New Contributor

Thanks a lot for your input, it does clarify a lot. Now I see the difference of behavior between PyScripter and PyCharm. I have seen that PyCharm does create that cache you mention, as for example the env.workspace, appeared in the list after using for the first time. Again, thanks a lot for the time on researching and helping me with this matter. Best regards,

0 Kudos