env() reference

5181
30
02-05-2020 10:13 AM
by Anonymous User
Not applicable

Hi, I'm trying to use Pycharm, as IDE for python, but it seemd that the function arcpy.env is missing from __init_.PY...

I'm using C:\Python27\ArcGISx6410.5 interpreter.

When I try to run it inside PyCharm I get :

C:\Python27\ArcGISx6410.5\python.exe U:/Prog/PYScript/Geosoft2ArcGIS.py
Traceback (most recent call last):
File "U:/Prog/PYScript/Geosoft2ArcGIS.py", line 12, in <module>
import arcpy
File "c:\arcgis\desktop10.7\ArcPy\arcpy\__init__.py", line 22, in <module>
from arcpy.geoprocessing import gp
ImportError: cannot import name gp

0 Kudos
30 Replies
by Anonymous User
Not applicable

OK, 

I was able to import them in the Python console.

As I type, as soon as I've finished typing the y... arcpy turns green (error), before hitting enter

0 Kudos
DanPatterson_Retired
MVP Emeritus

So you can't... or you can... because as soon as you type the 'y' in arcpy you get a green? error?

When you hit enter, does it say something to the effect that arcpy doesn't exist?

can you do a >>> dir(arcpy) without error?  I still suspect that you aren't using the python that was installed with arcmap and/or it can find the path to that site-package distribution.

0 Kudos
by Anonymous User
Not applicable

From Python console:

import arcpy
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.2\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "C:\ArcGIS\Desktop10.7\ArcPy\arcpy\__init__.py", line 22, in <module>
from arcpy.geoprocessing import gp
ImportError: cannot import name gp

dir(arcpy)
Traceback (most recent call last):
File "<input>", line 1, in <module>

0 Kudos
DanPatterson_Retired
MVP Emeritus

Yup... it is fouled up.  Per chance did you have pycharm installed before ArcMap?  If so, then it isn't "seeing" and the python that arcmap expects to use.

Try a

import sys

sys.path
['C:\\.... a big list hopefully of environment paths, one of them should bear some inclining of being associated with ArcMap

I don't use it, so if one of the ArcMap'pers would be so kind and replicate the above and post the salient paths for Yves, that would be helpful to him.

(just saying... ArcGIS Pro, conda, even cloned environments  )

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I agree with Dan that something must be off with system and/or Python paths.  When I check the sys.path in a PyCharm console on my machine, I get:

PyDev console: starting.

Python 2.7.16 (v2.7.16:413a49145e, Mar  4 2019, 01:30:55) [MSC v.1500 32 bit (Intel)] on win32
>>> import sys
>>> for path in sys.path:
...     print(path)
...     
C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pydev
C:\Users\foobar\PyCharmProjects\untitled
C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pycharm_display
C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\third_party\thriftpy
C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pydev
C:\WINDOWS\SYSTEM32\python27.zip
C:\Program Files (x86)\Python27\ArcGIS10.7\DLLs
C:\Program Files (x86)\Python27\ArcGIS10.7\lib
C:\Program Files (x86)\Python27\ArcGIS10.7\lib\plat-win
C:\Program Files (x86)\Python27\ArcGIS10.7\lib\lib-tk
C:\Program Files (x86)\Python27\ArcGIS10.7
C:\Program Files (x86)\Python27\ArcGIS10.7\lib\site-packages
C:\Program Files (x86)\ArcGIS\Desktop10.7\bin
C:\Program Files (x86)\ArcGIS\Desktop10.7\ArcPy
C:\Program Files (x86)\ArcGIS\Desktop10.7\ArcToolBox\Scripts
C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pycharm_matplotlib_backend
>>> 
0 Kudos
by Anonymous User
Not applicable

The ones in BOLD are missing from my path... go figure why !!!

for path in sys.path:
... print(path)
...

C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pydev
C:\Users\foobar\PyCharmProjects\untitled
C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pycharm_display
C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\third_party\thriftpy
C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pydev
C:\WINDOWS\SYSTEM32\python27.zip
C:\Program Files (x86)\Python27\ArcGIS10.7\DLLs
C:\Program Files (x86)\Python27\ArcGIS10.7\lib
C:\Program Files (x86)\Python27\ArcGIS10.7\lib\plat-win
C:\Program Files (x86)\Python27\ArcGIS10.7\lib\lib-tk
C:\Program Files (x86)\Python27\ArcGIS10.7
C:\Program Files (x86)\Python27\ArcGIS10.7\lib\site-packages
C:\Program Files (x86)\ArcGIS\Desktop10.7\bin
C:\Program Files (x86)\ArcGIS\Desktop10.7\ArcPy
C:\Program Files (x86)\ArcGIS\Desktop10.7\ArcToolBox\Scripts
C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pycharm_matplotlib_backend
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

You could try adding those paths in the script before importing arcpy, but I honestly don't think that will solve the issue.  At this point, I am not sure why it won't work.

0 Kudos
by Anonymous User
Not applicable

It gives me the same error on startup from the console.

This is PyScripter

*** Python 2.7.16 (v2.7.16:413a49145e, Mar 4 2019, 01:30:55) [MSC v.1500 32 bit (Intel)] on win32. ***
*** Remote Python engine is active ***
>>> import arcpy
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\ArcGIS\Desktop10.7\ArcPy\arcpy\__init__.py", line 22, in <module>
from arcpy.geoprocessing import gp
ImportError: cannot import name gp
>>>
*** Remote Interpreter Reinitialized ***
>>>
*** Remote Interpreter Reinitialized ***
Traceback (most recent call last):
File "<module1>", line 18, in <module>
File "C:\ArcGIS\Desktop10.7\ArcPy\arcpy\__init__.py", line 22, in <module>
from arcpy.geoprocessing import gp
ImportError: cannot import name gp
>>>

Or from this code and "run"

def main():
pass

if __name__ == '__main__':
main()
import arcpy

0 Kudos
DanPatterson_Retired
MVP Emeritus

If arcmap works, can you import arcpy in the Python console from "within" arcmap?

If you can, then you might want to consider removing pycharm completely and try again

by Anonymous User
Not applicable

Hi,

I solved it.

  • I've deleted the C:\Python27\ArcGIS10.7, and C:\ArcGIS\Desktop10.7\arcpy.
  • uninstall pyscript
  • Copy the files from a working computer from those folders C:\Python27\ArcGIS10.7, and C:\ArcGIS\Desktop10.7\arcpy.
  • repair ArcGIS
  • reinstall pyscript

It's now working !!!!