'NoneType' Object has no Attribute 'modules' AttributeError the Second Time Script Tool is Run

8327
17
06-15-2018 09:52 AM
PaulMcCord1
Emerging Contributor

I am developing a script tool that will be part of a suite of tools in an ArcToolbox. This script tool is intended to plot FAO food security indicators.

When I run this tool the first time from ArcToolbox (ie, I run the tool from ArcMap), it successfully completes without an issue (ie, the Pygal module (which I import) plots the food security indicator that the user selects for the country selected by the user). However, the second time that I run the tool, it fails with the following error:

Traceback (most recent call last):
File "C:\Users\Paul McCord\Google Drive\Toolbox\Tool_First\FoodSecurity\Content_Post052418\TestScripts\foodSecurity_v1.py", line 5, in <module>
import pygal as pg
File "C:\Python27\ArcGIS10.5\lib\site-packages\pygal\__init__.py", line 28, in <module>
import pkg_resources
File "C:\Python27\ArcGIS10.5\lib\site-packages\pkg_resources\__init__.py", line 46, in <module>
from pkg_resources.extern import six
File "C:\Python27\ArcGIS10.5\lib\site-packages\pkg_resources\extern\__init__.py", line 43, in load_module
mod = sys.modules[extant]
AttributeError: 'NoneType' object has no attribute 'modules'

Referring to a separate post that suggested removing the 'Try-Except' statement, I've tried removing this in order to resolve the issue, but it persists.

A similar GeoNet post exists (https://community.esri.com/thread/44095 ); however, in this post, the issue appears to arise from 'ImportToolbox.' I believe my issue arises from a module that I'm importing. Nevertheless, it does not appear that the issue was resolved in the earlier post.

Does anyone have experience with this issue while developing a Python script for a script tool?

Thanks,

Paul  

0 Kudos
17 Replies
NyigamBole
Occasional Contributor

Did you by any effort find the solution to it Paul?

 

0 Kudos
MichaelLowry
Emerging Contributor

Did you find a better solution than simply switching to Pro? I am still looking for a solution after reading dozens of pages about pkg_resources. My situation is here: arcpy - ArcGIS Server Geoprocessing Service failing on repeat call - Geographic Information Systems ... 

Note I had provided a hack solution there, but that no longer is sufficient for me because the issue arises with other packages.   

0 Kudos
PaulMcCord1
Emerging Contributor

Michael,

Unfortunately I never found a better solution than switching to Pro. The script was developed for a suite of desktop-based geoprocessing tools for users interested in resource sustainability. The only "compromise" I came up with was articulating in the user guide for these geoprocessing tools that ArcGIS may need to be restarted after running the tool. Not an ideal solution at all, but it was all I could do.

0 Kudos
DamijanBec
New Contributor

I have found a stupid workaround that worked in my case.

I could successfully run my Python script importing tablib, but only the first run was successful. Then nothing else worked but closing ArcMap and opening once again. Of course, only first run was successful. Until I have tried to import this library into the Python window first before running the script. All successive script runs were without any error.

Although I do not believe that it is relevant here are the specs:

ArcGIS 10.5.1 v. 7333 (no patches applied), Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32, tablib version 0.14.0

And the code that have triggered this error

import tablib‍
and the error
Traceback (most recent call last):
  File "D:\Data\Test\TestTabLib.py", line 9, in <module>
    import tablib
  File "C:\Python27\ArcGIS10.5\lib\site-packages\tablib\__init__.py", line 2, in <module>
    from pkg_resources import get_distribution, DistributionNotFound
  File "C:\Python27\ArcGIS10.5\lib\site-packages\pkg_resources\__init__.py", line 48, in <module>
    from pkg_resources.extern import six
  File "C:\Python27\ArcGIS10.5\lib\site-packages\pkg_resources\extern\__init__.py", line 44, in load_module
    mod = sys.modules[extant]
AttributeError: 'NoneType' object has no attribute 'modules'‍‍‍‍‍‍‍‍‍‍

LiYi1
by
Emerging Contributor

Hi Damijan, could you elaborate a bit more on what you meant by "import this library into the Python window first"? I have encountered the exact same problem with you and am desperate need a solution. Thanks so much.

0 Kudos
DamijanBec
New Contributor

Sorry for the delay in replying back to you. I do not encounter any issues if I follow the following procedure:

1. Inside ArcMap go to menu options Geoprocessing > Python (you will open the Python window) # You do this step only if you do not have Python window open. If you already have it then skip it and go to next step.

type

2. import tablib # Or whichever library is causing you this issue

3. Press Enter

4. Run your tool

You need to import this "offending" library first and only then try to run your tool. Otherwise this error will occur at the second run once again. And the only "solution" will be to close the ArcMap.

P.S. I do hope that this will really help you to successfully run your tool(s). So if this does not help you then you are on your own, as we cannot expect any fix coming from ESRI for that kind of "miniscule" issue. Unfortunately, as it is very obvious, that ArcGIS Desktop is going the way of dodo, like ArcInfo Workstation and ArcView before.

0 Kudos
NyigamBole
Occasional Contributor

I had been facing a similar issue and I accidentally stumbled on the solution. You can check out this link to the query I had raised. https://community.esri.com/t5/arcmap-questions/script-in-arcgis-runs-only-once-and-throws-error/m-p/... . Cheers!!

0 Kudos
NyigamBole
Occasional Contributor

Hey Paul! I accidentally stumbled on the solution to the problem. Heres the link to a query I raised in the website. https://community.esri.com/t5/arcmap-questions/script-in-arcgis-runs-only-once-and-throws-error/m-p/... Cheers!!😊

0 Kudos