<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Running python from cmd window works well, how to run from toolbox??? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/running-python-from-cmd-window-works-well-how-to/m-p/717354#M55612</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Just to give you a better idea of my problem, the below script returns a list of Users currently connected to my SDE... this script is only useful if i run it from the CMD window, if I run it from Catalog as a script it displays the window shown in my first post.. and as you can see it does not return a list of connected users.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#-------------------------------------------------------------------------------
# Name:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Find current users connected to SDE
# Purpose:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; This script is specifically written to be able to return a
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list of all current users connected to the SDE_Spatial database.
#
# Author:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Ben Van Kesteren &amp;amp; Nathan Duncan
#
# Created:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14/11/2014
# Copyright:&amp;nbsp;&amp;nbsp; (c) Ben Van Kesteren 2014
#
#-------------------------------------------------------------------------------


import arcpy
import csv


def main():
&amp;nbsp;&amp;nbsp;&amp;nbsp; PCNameKey = pc_name_key()
&amp;nbsp;&amp;nbsp;&amp;nbsp; UserList = user_list()
&amp;nbsp;&amp;nbsp;&amp;nbsp; outputFinal = output(PCNameKey, UserList)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for name in outputFinal:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print name
&amp;nbsp; print name
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "\n" + str(outputFinal)+ "\n"
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
## Creates a dictionary from the listed CSV file
def pc_name_key():
&amp;nbsp; reader = csv.reader(open('H:\\GIS Admin\\PCNumbers.csv'))
&amp;nbsp; key_dict = {}
&amp;nbsp; for row in reader:
&amp;nbsp; key = row[0]
&amp;nbsp; if key in key_dict:
&amp;nbsp; pass
&amp;nbsp; key_dict[key] = row[1]
&amp;nbsp; return key_dict




def user_list():
&amp;nbsp; # Set the admistrative workspace connection
&amp;nbsp; arcpy.env.workspace = "H:\\ESRI SDE Connections\\GISADMIN@SDE_Spatial@Smithy.sde"
&amp;nbsp; user_list = arcpy.env.workspace


&amp;nbsp; # Create a list of users
&amp;nbsp; '''
&amp;nbsp; NOTE: When the arcpy.env.workspace environment is set, a workspace
&amp;nbsp; does not need to be provided to the function.
&amp;nbsp; '''
&amp;nbsp; users = arcpy.ListUsers(user_list)


&amp;nbsp; # Create a list of SDE connected usernames.
&amp;nbsp; id_users = [user.ClientName for user in users]
&amp;nbsp; current_users = []
&amp;nbsp; for user in id_users:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if user != str(u'gisserver'):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; user = user.upper() # set PC Numbers to uppercase
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; user = str(user)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if user in current_users:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; current_users.append(user)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else: pass


&amp;nbsp; # Count all users currently connected
&amp;nbsp; total_users = len(current_users)

&amp;nbsp; # Print the total number of users currently connected
&amp;nbsp; if total_users &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "\nTotal number of connected users " + str(total_users) + "\n"
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "\nNo Users Connected\n"
&amp;nbsp; return current_users




def output(PC_Key, name_list):
&amp;nbsp;&amp;nbsp;&amp;nbsp; output_list = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; for id in name_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if id in PC_Key:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output_list.append(PC_Key[id])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else: pass
&amp;nbsp;&amp;nbsp;&amp;nbsp; return output_list


## Boilerplate
if __name__ == '__main__':
&amp;nbsp; main()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 06:43:05 GMT</pubDate>
    <dc:creator>BenVan_Kesteren1</dc:creator>
    <dc:date>2021-12-12T06:43:05Z</dc:date>
    <item>
      <title>Running python from cmd window works well, how to run from toolbox???</title>
      <link>https://community.esri.com/t5/python-questions/running-python-from-cmd-window-works-well-how-to/m-p/717353#M55611</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi All, &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a python script that I have just written and I am able to run it quite simply by opening the CMD window, and typing &amp;gt;&amp;gt;&amp;gt;python myScript.py and it runs great, it shows me all my print commands as it runs through.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now I have it working I want to add it to my Toolbox so I can simply run the script once a week to do what I have set it to do.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have quite simply right clicked the toolbox, and selected ADD | Script... I then get it to point at my myScript.py file, and thats it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So then I go back to the script in my catalog, and double click it and it runs. But unfortunately I am unable to see any of the Print statements as its the esri window, it only lets me know when its complete.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Basically see the first screenshot of the text the script prints out, this is what I like...&lt;/P&gt;&lt;P&gt;&lt;IMG alt="2014-11-25_12-58-31_CWindowssystem32cmd.exe - python  Importing_CAD_Data.py.jpg" class="jive-image image-1" src="/legacyfs/online/34100_2014-11-25_12-58-31_CWindowssystem32cmd.exe - python  Importing_CAD_Data.py.jpg" style="width: 620px; height: 397px;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;See this second screen shot, this is the ESRI version, I am hoping to get all my print statements to show in this window... is it at all possible??&lt;/P&gt;&lt;P&gt;&lt;IMG alt="2014-11-25_13-08-06_View Connected Users.jpg" class="jive-image image-2" src="/legacyfs/online/34101_2014-11-25_13-08-06_View Connected Users.jpg" style="width: 620px; height: 287px;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks heaps for your time.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Nov 2014 02:08:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/running-python-from-cmd-window-works-well-how-to/m-p/717353#M55611</guid>
      <dc:creator>BenVan_Kesteren1</dc:creator>
      <dc:date>2014-11-25T02:08:52Z</dc:date>
    </item>
    <item>
      <title>Re: Running python from cmd window works well, how to run from toolbox???</title>
      <link>https://community.esri.com/t5/python-questions/running-python-from-cmd-window-works-well-how-to/m-p/717354#M55612</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Just to give you a better idea of my problem, the below script returns a list of Users currently connected to my SDE... this script is only useful if i run it from the CMD window, if I run it from Catalog as a script it displays the window shown in my first post.. and as you can see it does not return a list of connected users.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#-------------------------------------------------------------------------------
# Name:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Find current users connected to SDE
# Purpose:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; This script is specifically written to be able to return a
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list of all current users connected to the SDE_Spatial database.
#
# Author:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Ben Van Kesteren &amp;amp; Nathan Duncan
#
# Created:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14/11/2014
# Copyright:&amp;nbsp;&amp;nbsp; (c) Ben Van Kesteren 2014
#
#-------------------------------------------------------------------------------


import arcpy
import csv


def main():
&amp;nbsp;&amp;nbsp;&amp;nbsp; PCNameKey = pc_name_key()
&amp;nbsp;&amp;nbsp;&amp;nbsp; UserList = user_list()
&amp;nbsp;&amp;nbsp;&amp;nbsp; outputFinal = output(PCNameKey, UserList)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for name in outputFinal:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print name
&amp;nbsp; print name
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "\n" + str(outputFinal)+ "\n"
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
## Creates a dictionary from the listed CSV file
def pc_name_key():
&amp;nbsp; reader = csv.reader(open('H:\\GIS Admin\\PCNumbers.csv'))
&amp;nbsp; key_dict = {}
&amp;nbsp; for row in reader:
&amp;nbsp; key = row[0]
&amp;nbsp; if key in key_dict:
&amp;nbsp; pass
&amp;nbsp; key_dict[key] = row[1]
&amp;nbsp; return key_dict




def user_list():
&amp;nbsp; # Set the admistrative workspace connection
&amp;nbsp; arcpy.env.workspace = "H:\\ESRI SDE Connections\\GISADMIN@SDE_Spatial@Smithy.sde"
&amp;nbsp; user_list = arcpy.env.workspace


&amp;nbsp; # Create a list of users
&amp;nbsp; '''
&amp;nbsp; NOTE: When the arcpy.env.workspace environment is set, a workspace
&amp;nbsp; does not need to be provided to the function.
&amp;nbsp; '''
&amp;nbsp; users = arcpy.ListUsers(user_list)


&amp;nbsp; # Create a list of SDE connected usernames.
&amp;nbsp; id_users = [user.ClientName for user in users]
&amp;nbsp; current_users = []
&amp;nbsp; for user in id_users:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if user != str(u'gisserver'):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; user = user.upper() # set PC Numbers to uppercase
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; user = str(user)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if user in current_users:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; current_users.append(user)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else: pass


&amp;nbsp; # Count all users currently connected
&amp;nbsp; total_users = len(current_users)

&amp;nbsp; # Print the total number of users currently connected
&amp;nbsp; if total_users &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "\nTotal number of connected users " + str(total_users) + "\n"
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "\nNo Users Connected\n"
&amp;nbsp; return current_users




def output(PC_Key, name_list):
&amp;nbsp;&amp;nbsp;&amp;nbsp; output_list = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; for id in name_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if id in PC_Key:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output_list.append(PC_Key[id])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else: pass
&amp;nbsp;&amp;nbsp;&amp;nbsp; return output_list


## Boilerplate
if __name__ == '__main__':
&amp;nbsp; main()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:43:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/running-python-from-cmd-window-works-well-how-to/m-p/717354#M55612</guid>
      <dc:creator>BenVan_Kesteren1</dc:creator>
      <dc:date>2021-12-12T06:43:05Z</dc:date>
    </item>
    <item>
      <title>Re: Running python from cmd window works well, how to run from toolbox???</title>
      <link>https://community.esri.com/t5/python-questions/running-python-from-cmd-window-works-well-how-to/m-p/717355#M55613</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Check out the AddMessage function in arcpy: &lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//018v00000007000000" title="http://resources.arcgis.com/en/help/main/10.1/index.html#//018v00000007000000" rel="nofollow noopener noreferrer" target="_blank"&gt;ArcGIS Help 10.1&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Before you return your user count you could modify the print statements to this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;if total_users &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddWarning("Total number of connected users " + str(total_users))
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("No users connected")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:43:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/running-python-from-cmd-window-works-well-how-to/m-p/717355#M55613</guid>
      <dc:creator>OwenEarley</dc:creator>
      <dc:date>2021-12-12T06:43:07Z</dc:date>
    </item>
    <item>
      <title>Re: Running python from cmd window works well, how to run from toolbox???</title>
      <link>https://community.esri.com/t5/python-questions/running-python-from-cmd-window-works-well-how-to/m-p/717356#M55614</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Owen, thanks very much for that, has worked well for this little script.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So are you saying that print statements are not recognised by this ESRI window at all? I need to use AddMessage and AddWarning instead from now on?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks again.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Nov 2014 03:52:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/running-python-from-cmd-window-works-well-how-to/m-p/717356#M55614</guid>
      <dc:creator>BenVan_Kesteren1</dc:creator>
      <dc:date>2014-11-25T03:52:26Z</dc:date>
    </item>
    <item>
      <title>Re: Running python from cmd window works well, how to run from toolbox???</title>
      <link>https://community.esri.com/t5/python-questions/running-python-from-cmd-window-works-well-how-to/m-p/717357#M55615</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That's correct. Script tool messages can only appear in geoprocessing results if you use the arcpy methods to send those messages. On the plus side, these messages are not only printed, they are saved as results in the Results window for later review.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Nov 2014 04:00:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/running-python-from-cmd-window-works-well-how-to/m-p/717357#M55615</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2014-11-25T04:00:04Z</dc:date>
    </item>
    <item>
      <title>Re: Running python from cmd window works well, how to run from toolbox???</title>
      <link>https://community.esri.com/t5/python-questions/running-python-from-cmd-window-works-well-how-to/m-p/717358#M55616</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migration-blogpost/1070"&gt;Posting Code blocks in the new GeoNet&lt;/A&gt;‌&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The above link shows how to post nicely highlighted and numbered python code on the forum.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;While I'm here, I can't resist spreading the gospel on text formatting -- much better than adding strings together with + ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_1416891686894668 jive_text_macro" jivemacro_uid="_1416891686894668"&gt;&lt;P&gt;arcpy.AddWarning(&lt;SPAN class="string"&gt;"Total number of connected users {}".format(total_users))&lt;/SPAN&gt;&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.python.org/2/library/string.html#format-string-syntax" title="https://docs.python.org/2/library/string.html#format-string-syntax"&gt;7.1. string — Common string operations — Python 2.7.8 documentation&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Nov 2014 05:01:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/running-python-from-cmd-window-works-well-how-to/m-p/717358#M55616</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2014-11-25T05:01:50Z</dc:date>
    </item>
  </channel>
</rss>

