Select to view content in your preferred language

AttributeError: 'module' object has no attribute 'GetParameterAsText'

5376
9
04-08-2019 05:34 AM
JohnKohlin
Emerging Contributor

import arcpy, os
folderPath = arcpy.GetParameterAsText(0)

leads to the error in the title. Seems like the path to arcpy is not correct somewhere. I have installed several versions of Python which I believe have caused the problem. I have reinstalled ArcGIS Desktop but the problem persist. How can I solve the problem?

Tags (2)
0 Kudos
9 Replies
DanPatterson_Retired
MVP Emeritus

What is the exact error message?

Why did you install other versions of Python?  the only one that should exist is the one(s)  associated with arcmap or ArcGIS pro.

0 Kudos
JohnKohlin
Emerging Contributor

Traceback (most recent call last):
File "H:\avanceradtest.py", line 3, in <module>
folderPath = arcpy.GetParameterAsText(0)
AttributeError: 'module' object has no attribute 'GetParameterAsText'

Complete beginner when it comes to Python. Mistake by me. 

0 Kudos
DylanHarwell
Frequent Contributor

As far as I know, uninstalling ArcGIS also uninstalls the instances of Python from the machine. What IDE are you using? If using PyScripter or IDLE, what happens when you type 'import arcpy' and hit enter in the interactive python interpreter?

0 Kudos
JohnKohlin
Emerging Contributor

I'm using IDLE. When importing arcpy I get no errors. 

0 Kudos
PavanYadav
Esri Contributor

Hi John,

1. I see you're getting an AttributeError. Can you share how you are importing arcpy package/modules in your script?

2. If you have several versions of python, you can go to: <drive>\Python27\ArcGIS<version e.g .10.6.1>
Right click 'python.exe' > run. Now, run your script here. This is ArcGIS Desktop python install default location.

This will help us determining if you're running into an install or a syntax issue.

Pavan Yadav
Product Engineer at Esri
AI for Imagery
Connect with me on LinkedIn!
Contact Esri Support Services
0 Kudos
JohnKohlin
Emerging Contributor
import arcpy, os
#Read input parameters from GP dialog
folderPath = arcpy.GetParameterAsText(0)
if folderPath=="":
  folderPath = r"H:\Python"
#Loop through each MXD file  
for filename in os.listdir(folderPath):  
  fullpath = os.path.join(folderPath, filename)  
  if os.path.isfile(fullpath):  
    if filename.lower().endswith(".mxd"):  
      #open rapportfile for MXD  
      outFilename=fullpath[:fullpath.rfind(".")]+".csv"  
      mes= '\nMXD: %s' % (fullpath)  
      print mes  
      arcpy.AddMessage(mes)  
      rapportfile=open(outFilename,"w")  
      header='MXD;WORKSPACE;FEATURECLASS'  
      rapportfile.write(header+'\n')  
      mxd = arcpy.mapping.MapDocument(fullpath)  
      for df in arcpy.mapping.ListDataFrames(mxd):  
        layerList = arcpy.mapping.ListLayers(mxd, "", df)  
        mes='MXD %s innehaller %s lager' % (filename, len(layerList))  
        arcpy.AddMessage(mes)  
        print mes  
        for lyr in layerList:  
          if lyr.supports("dataSource"):  
            workspace=lyr.workspacePath  
            fc=lyr.datasetName  
            print 'WorkspacePath: %s' % workspace  
            print 'FeatureClass:  %s' % fc  
            reg='%s;%s;%s' % (filename,workspace,fc)  
            #arcpy.AddMessage(reg)  
            rapportfile.write(reg+'\n')  
      mes='Papportbestand: %s\n' % (outFilename)  
      print mes  
      arcpy.AddMessage(mes)  
      rapportfile.close()  
      del mxd  

The script ran with no problems using python.exe as you mentioned in point 2. 

0 Kudos
DanPatterson_Retired
MVP Emeritus

in the following, "..." represents the installation path for arcgis Pro.

line 3 shows the version of python which is running, if it is in the ArcGISpro-py3 or your clone of it, then it should be python 3.x

line 7 does a similar thing, but shows the location of where arcpy is imported

Are yours similar?

import sys
import arcpy

sys.executable
'C:\\...\\bin\\Python\\envs\\arcgispro-py3\\pythonw.exe'

arcpy.__file__
'C:\\...\\Resources\\ArcPy\\arcpy\\__init__.py'
0 Kudos
JohnKohlin
Emerging Contributor
import arcpy
import sys


print sys.executable
print arcpy.__file__

C:\Python27\ArcGIS10.5\pythonw.exe
H:/arcpy.pyc
0 Kudos
DanPatterson_Retired
MVP Emeritus

installation problems.  what is the H drive? the pyc file isn't going to cut it because 'arcpy' just isn't a single file it is a package... at least in pro it is

0 Kudos