No module names ArcPy

12879
10
11-10-2016 09:26 AM
Ravi_Nigudkar
New Contributor II

I have two question:Is there a Python IDLE or editor outside of the Python window that has the autocompletion capabilities? Th

Is there a Python IDLE or editor outside of the Python window that has the autocompletion capabilities? 

Can someone help me with my error message?

File "C:\Python27\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 322, in RunScript
debugger.run(codeObject, __main__.__dict__, start_stepping=0)
File "C:\Python27\Lib\site-packages\pythonwin\pywin\debugger\__init__.py", line 60, in run
_GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
File "C:\Python27\Lib\site-packages\pythonwin\pywin\debugger\debugger.py", line 655, in run
exec cmd in globals, locals
File "C:\EsriPress\Python\Data\Exercise06\Results\Field_list.py", line 1, in <module>
import arcpy
ImportError: No module named arcpy -- This is where the problem is

This is the script I run.

import arcpy
from arcpy import env
env.overwriteOutput = True
env.overwriteOutput = " C:/esripress/python/data/exercise06"
fieldList = arcpy.ListFields("counties.shp")
for field in fieldList:
print field.name + " " + field.type

Tags (1)
0 Kudos
10 Replies
ClintonDow1
Occasional Contributor II

Hi Ravi,

Based on the File "C:\Python27\Lib..." in your stack trace it looks like you're using a version of Python that you installed rather than the one that is included with ArcGIS. There can be multiple instances of python.exe on one machine - since arcpy depends on some of our DLLs it only works with the ArcGIS copy. 

Try looking for a python.exe in "C:\Python27\ArcGIS10.4" (or whichever 10.x version number you're working with) and run your script using it instead. 

Ravi_Nigudkar
New Contributor II

Hi Clinton,

You are right. I am using PythonWin.

When I run the same script inside the Python Window of my 10.4.1 version of

ArcGIS it works.

I want to get the latest version of PythonWin and that has the same

capabilities of autocompletion.

Is this possible?

Let me know

Thanks for email.

Ravi

0 Kudos
ClintonDow1
Occasional Contributor II

edit: I have 10.5 installed, please change the paths below to the correct version that you are using

Yes it is possible. PythonWin uses a windows installer which checks your registry and installs the package in your specified Python2.7 location - Currently it seems yours is at 'C:\Python27\python.exe' and it should be 'C:\Python27\ArcGIS10.5\python.exe' instead. 

First, uninstall PythonWin because its using the wrong registry value, we will reinstall it in a minute. 

Then save this script as 'C:\Python27\ArcGIS10.5\reg.py':

#
# script to register Python 2.0 or later for use with 
# Python extensions that require Python registry settings
#
# written by Joakim Loew for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm
#
# modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html//www.mail-archive.com/distutils-s...//www.mail-archive.com/distutils-sig@python.org/msg10512.html//www.mail-archive.com/distutils-sig@python.org/msg10512.html

import sys

from _winreg import *

# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix

regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
    installpath, installpath, installpath
)

def RegisterPy():
    try:
        reg = OpenKey(HKEY_CURRENT_USER, regpath)
    except EnvironmentError as e:
        try:
            reg = CreateKey(HKEY_CURRENT_USER, regpath)
            SetValue(reg, installkey, REG_SZ, installpath)
            SetValue(reg, pythonkey, REG_SZ, pythonpath)
            CloseKey(reg)
        except:
            print "*** Unable to register!"
            return
        print "--- Python", version, "is now registered!"
        return
    if (QueryValue(reg, installkey) == installpath and
        QueryValue(reg, pythonkey) == pythonpath):
        CloseKey(reg)
        print "=== Python", version, "is already registered!"
        return
    CloseKey(reg)
    print "*** Unable to register!"
    print "*** You probably have another Python installation!"

if __name__ == "__main__":
    RegisterPy()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Then from a cmd prompt run: C:\> C:\Python27\ArcGIS10.5\python.exe reg.py

You should see a message '---Python 2.7 is now registered!'

To verify its the right version, from the cmd prompt run 'C:> python'

Then within the interpreter run

import sys
print sys.executable‍‍

and it should return 'C:\\Python27\\ArcGIS10.5\\python.exe'

If this is the path, you should now be able to reinstall PythonWin and verify in the install wizard that it is now pointing to 'C:\\Python27\\ArcGIS10.5\\python.exe' then you should be good to go!

DanPatterson_Retired
MVP Emeritus

Clinton, line # 2 states that it can be used to register python 2 or later... I presume that this means python 2.0 - 2.7.x since the print statements aren't compliant in the script.  Or did the authors mean that the registry fixes are only for the 2.x line of python... 

>>> print("Python version " + sys.version[:5])  # print requires round brackets, going forward
Python version 3.5.1

ps... sad to see 2.7 still in 10.5...

ClintonDow1
Occasional Contributor II

Good eye, yes I believe this snippet is old enough that it predates Python 3 so it should only work on a Python 2.x install in this form. 

0 Kudos
LukeWebb
Occasional Contributor III

You can and most likely do have multiple pythons on your computer! 

Therefore, your IDE is hooked up to the wrong python!

E.g. if you type into dos:

python C:\myfile.py

it actually looks in a windows registry, and runs:  (as an aexample, not your setup!)

C:\python27\python.exe   C:\myfile.py

You need to make sure whatever you use to run it, uses the correct python.exe (Installed when you setup arcgis). This setup stage, should also allow autocomplete to work using any python IDE.

0 Kudos
Ravi_Nigudkar
New Contributor II

I tried everything you suggested. It did not work. I was not able to locate myfile.py

0 Kudos
DanPatterson_Retired
MVP Emeritus

Luke was giving an example of what one could do IF they have a file called myfile.py located on the c: drive.  It was not something that you were supposed to try specifically, but to emulate with your own file on your own computer.  The operative word is 'emulate' or something similar

0 Kudos
XanderBakker
Esri Esteemed Contributor

There are quite a few IDE's for Python, many of which are free. I'm still using PyScripter. You can see a discussion in this poll I created some years ago: 

When you post your code you should have a look at: Posting code with Syntax Highlighting on GeoNet , since your indents are gone and that makes line 7 invalid.

Your code does have some errors on line 4 and should look like this:

import arcpy
from arcpy import env
env.overwriteOutput = True
env.workspace = "C:/esripress/python/data/exercise06"
fieldList = arcpy.ListFields("counties.shp")
for field in fieldList:
    print field.name + " " + field.type

On line 4 you probably mean to set the workspace (required from the ListFields to work correctly if you only specify the name of the shapefile and not the full path) and you have a space before the path (directly after the double quotes).

Check the code with the Python window in ArcMap to see if your install is OK.