ArcPy in Created Python Modules?

305
4
Jump to solution
11-15-2017 06:22 AM
MarcusSanders2
New Contributor II

I'm trying to use the arcpy module in functions that are within custom Python modules that I've created, but I keep getting this error:

NameError: global name 'arcpy' is not defined

How do I fix this? These modules will be used within ArcGIS, where ArcPy is already loaded.

I've tried with and without "import arcpy".

Example:

import arcpy

def refresher():
    ''' Refreshes map and TOC '''
    arcpy.RefreshActiveView()
    arcpy.RefreshTOC()
    return 'Refreshed map and TOC.'
0 Kudos
1 Solution

Accepted Solutions
MarcusSanders2
New Contributor II

ArcPy can be imported. I just realized my issue came from not restarting ArcMap after changing my script; even after reimporting my module (without restarting), it doesn't see the changes until I restart.

It works now, and looks like import arcpy is required.

Solution: make sure import arcpy is in the module, and restart ArcMap after any changes to the .py file

import arcpy

def refresher():
    ''' Refreshes map and TOC '''
    arcpy.RefreshActiveView()
    arcpy.RefreshTOC()
    return 'Refreshed map and TOC.'

View solution in original post

4 Replies
DanPatterson_Retired
MVP Esteemed Contributor

So you are saying that you can use the builtin python IDE and you have not problem with or without importing arcpy.

But you say it fails in your 'outside' python IDE, whether you import arcpy or not.

If the latter is the case, then either your installation is incorrect, your IDE knows nothing about the location of the arcpy module.

You also failed to indicate whether this is for ArcMap or ArcGIS PRO... ArcGIS is a flakey term in terms of identifying what you are working with, since there is the 'arcgis' module and myriad of things with that moniker.

You did flag python 2, so presumably you are working with arcmap, therefore your installation path for Python and Arcpy is in the 

C:\Pythonx.x\ArcGIS10.X\ .... folder where the x's are version numbers.

If you have multiple instances of python installed outside a conda distribution, uninstall them

Provide more details if none of this applies.

MarcusSanders2
New Contributor II

No, I can't use this within the builtin python IDE in ArcGIS. I'd assume that arcpy was already loaded and ready for use after opening the program, but that's not the case when I remove the "import arcpy" line. It doesn't work whether I import arcpy or not within my module.

Yes, I'm working in ArcMap 10.5 (py2), and no changes to conda.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

If you can't import arcpy anywhere, then your installation is corrupt or somehow being misdirected.  Big chance to uninstall python (from all locations) and do a 'Repair' from the install/remove in windows.

MarcusSanders2
New Contributor II

ArcPy can be imported. I just realized my issue came from not restarting ArcMap after changing my script; even after reimporting my module (without restarting), it doesn't see the changes until I restart.

It works now, and looks like import arcpy is required.

Solution: make sure import arcpy is in the module, and restart ArcMap after any changes to the .py file

import arcpy

def refresher():
    ''' Refreshes map and TOC '''
    arcpy.RefreshActiveView()
    arcpy.RefreshTOC()
    return 'Refreshed map and TOC.'

View solution in original post