<?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: Script in ArcGIS runs only once and throws error the next time in ArcMap Questions</title>
    <link>https://community.esri.com/t5/arcmap-questions/script-in-arcgis-runs-only-once-and-throws-error/m-p/1269449#M4348</link>
    <description>&lt;P&gt;If you have access to ArcGIS Online then you could run this as a &lt;A href="https://doc.arcgis.com/en/arcgis-online/create-maps/prepare-a-notebook-for-automated-execution.htm" target="_self"&gt;scheduled notebook&lt;/A&gt;. Through this you could have modules imported first, then the script ran after.&lt;/P&gt;&lt;P&gt;Alternatively ArcGIS Pro also can use Notebooks, but you wouldn't be able to schedule these.&lt;/P&gt;&lt;P&gt;Hopefully that helps, if you have any questions about using Notebooks then please let me know.&lt;/P&gt;</description>
    <pubDate>Mon, 20 Mar 2023 15:37:30 GMT</pubDate>
    <dc:creator>David_McRitchie</dc:creator>
    <dc:date>2023-03-20T15:37:30Z</dc:date>
    <item>
      <title>Script in ArcGIS runs only once and throws error the next time</title>
      <link>https://community.esri.com/t5/arcmap-questions/script-in-arcgis-runs-only-once-and-throws-error/m-p/1268901#M4346</link>
      <description>&lt;P&gt;I am currently using ArcGIS 10.5. I am trying to build a script that would extract time series data from my netCDF file. Below is a sample code. As is required in ArcGIS, I have prepared a script tool that takes in input parameters and gives outputs.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os
from netCDF4 import Dataset
import numpy as np

# get the current directory relative to root folder
absolute_path = os.path.dirname(__file__)

# overwrite if previously created files are there
arcpy.AddMessage("Overwriting if previously created files are present in directory")
arcpy.env.overwriteOutput = True

## Process: getting variables
path_netcdf = arcpy.GetParameterAsText(1)
if path_netcdf == '#' or not path_netcdf:
    path_netcdf = os.path.join(absolute_path, "..\\..\\nc_test_files2\\ssp245.nc")

for i in range(3):
    # Storing the lat and lon data into the variables
    try:
        data = Dataset(path_netcdf, 'r')  # The 'r' in the end is a command to read the files
        lon = data.variables['lon'][:]  # This lists the longitude values
        lat = data.variables['lat'][:]  # This lists the latitude values
        arcpy.AddMessage(lon)
        print('working')

    except Exception as e:
        arcpy.AddMessage("Error occurred while reading NetCDF data: {0}".format(str(e)))
    finally:
        data.close()

arcpy.AddMessage("done")
&lt;/LI-CODE&gt;&lt;P&gt;Now, the script tool seems to run perfectly for the first time. However, after that if I want to use the tool again, it throws an error.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="netCDFerror.png" style="width: 901px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/65548iEA3EEE6B350883FC/image-size/large?v=v2&amp;amp;px=999" role="button" title="netCDFerror.png" alt="netCDFerror.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Digging deeper led me to the conclusion that the issue starts when line number #22 is run.&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/315360"&gt;@HendrikBernert1&lt;/a&gt; raised a similar issue in which he found out a workaround&amp;nbsp;&lt;A href="https://community.esri.com/t5/python-questions/arcpy-script-using-sqlalchemy-and-pandas-only-runs/td-p/275938/page/3" target="_blank"&gt;https://community.esri.com/t5/python-questions/arcpy-script-using-sqlalchemy-and-pandas-only-runs/td-p/275938/page/3&lt;/A&gt;&amp;nbsp;. The workaround is to import the modules in the ArcGIS python console before running the script... once that is done, the script runs normally end number of times. However, that is not an actual solution to the script issue.&lt;/P&gt;&lt;P&gt;Is there a way to properly run the script or automate the importing of modules before running the script?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2023 11:37:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/script-in-arcgis-runs-only-once-and-throws-error/m-p/1268901#M4346</guid>
      <dc:creator>NyigamBole</dc:creator>
      <dc:date>2023-03-17T11:37:22Z</dc:date>
    </item>
    <item>
      <title>Re: Script in ArcGIS runs only once and throws error the next time</title>
      <link>https://community.esri.com/t5/arcmap-questions/script-in-arcgis-runs-only-once-and-throws-error/m-p/1269449#M4348</link>
      <description>&lt;P&gt;If you have access to ArcGIS Online then you could run this as a &lt;A href="https://doc.arcgis.com/en/arcgis-online/create-maps/prepare-a-notebook-for-automated-execution.htm" target="_self"&gt;scheduled notebook&lt;/A&gt;. Through this you could have modules imported first, then the script ran after.&lt;/P&gt;&lt;P&gt;Alternatively ArcGIS Pro also can use Notebooks, but you wouldn't be able to schedule these.&lt;/P&gt;&lt;P&gt;Hopefully that helps, if you have any questions about using Notebooks then please let me know.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2023 15:37:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/script-in-arcgis-runs-only-once-and-throws-error/m-p/1269449#M4348</guid>
      <dc:creator>David_McRitchie</dc:creator>
      <dc:date>2023-03-20T15:37:30Z</dc:date>
    </item>
    <item>
      <title>Re: Script in ArcGIS runs only once and throws error the next time</title>
      <link>https://community.esri.com/t5/arcmap-questions/script-in-arcgis-runs-only-once-and-throws-error/m-p/1276069#M4362</link>
      <description>&lt;P&gt;Thank You David! I think I accidentally stumbled on a solution to the issue. I can now use the script as many times as I want without having to import the modules in the ArcGIS Python console every time I open an instance of ArcGIS. I wanted to make my script more interactive, and so I had to explore the use of the validation tab in the script properties (fig.1).&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="script properties.png" style="width: 329px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/67380i3BD916760A62BF21/image-size/large?v=v2&amp;amp;px=999" role="button" title="script properties.png" alt="script properties.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="NyigamBole_0-1680758354430.png" style="width: 449px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/67381i4DF0AC914DEC0EB3/image-dimensions/449x560?v=v2" width="449" height="560" role="button" title="NyigamBole_0-1680758354430.png" alt="NyigamBole_0-1680758354430.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;In the validation tab as shown in the picture, by default, it only imports the 'arcpy' module by default. The solution is to import all the modules used in the script, or even better, find the modules which is causing the issue (in my case it was 'netCDF4' module) and import it in the validation tab. This solved my issue of 'nonetype error' and the script running only once in ArcGIS.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Apr 2023 05:23:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/script-in-arcgis-runs-only-once-and-throws-error/m-p/1276069#M4362</guid>
      <dc:creator>NyigamBole</dc:creator>
      <dc:date>2023-04-06T05:23:38Z</dc:date>
    </item>
  </channel>
</rss>

