Access arcpy on Linux

3069
2
08-28-2018 01:51 PM
QiushengWu2
New Contributor II

Hello,

I have installed ArcGIS Server 10.6.1 on Linux and followed the instruction here to create a conda environment named arcgis with Python 3.6. Using the shell, I can activate the conda env and import arcpy successfully. However, the conda env does not work with any IDE (e.g., PyCharm, VS Code) or Jupyter Notebook. See below the message I got when trying to import arcpy in Jupyter Notebook. Any help will be greatly appreciated! 

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-1-5467a3dc9fe3> in <module>()
----> 1 import arcpy 
/opt/anaconda/envs/arcgis/lib/python3.6/site-packages/arcpy/__init__.py in <module>()     
68         raise ImportError("arcpy needs to run within an active ArcGIS Conda environment")     
69 else:---> 
70     import _initagsenv     
71      
72 from arcpy.geoprocessing import gp 
error: WSLib: failed initializing arcpy. Check if environment was activated correctly.
---------------------------------------------------------------------------


arcpy only works in Console 
arcpy-linux
0 Kudos
2 Replies
ShaunWalbridge
Esri Regular Contributor

Hello Quisheng,

Great to hear you're trying out the native ArcPy project, I'd be happy to help you. It should work for anything that's running inside the conda environment, you'll just need to run the commands inside the activated environment. Here's IPython started this way:

(arcpy-native) arcgis@ubuntu:/$ ipython
Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 17:14:51)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import arcpy
arcpy
In [2]: arcpy.GetInstallInfo()['Version']
Out[2]: '10.6.1'

This should also work well for Jupyter Notebooks and tools like Spyder, which can be installed into the environment. The story for other external tools is still a little complicated, the native ArcPy interface has to initialize system environment variables in order to operate. The script that executes on startup is available in $CONDA_PREFIX/etc/conda/activate.d/arcgis-server-10.6.1-py3-env_vars.sh if you'd like to take a look. The simpler way to get this along with your IDE is to start your IDE from within the active environment, so e.g. type `code` after activating the environment to start Visual Studio Code [note: I haven't tested this, but it should inherit the calling environment on creation]. Alternatively, you could create a bash script that first sourced that file. This is necessary because tools that call into ArcPy need to initialize the server pieces internally which are used for their dependent calls. We are looking at simplifying this for a future release, but hopefully that's enough information to get you started.

Cheers,
Shaun

0 Kudos
ShaunWalbridge
Esri Regular Contributor

One more note: I tried a little more with Visual Studio Code, and it looks like at some point, they will fix this upstream by activating the environment prior to using it with Conda environments. Currently, they just call python.exe without activation, they have a few issues open on their repository about this, such as Debugger not activating Anaconda environments · Issue #2067 · Microsoft/vscode-python · GitHub .

If you want to try and make this work in Visual Studio Code today without any additional setup of environment variables, you could also set them up manually with VScode (see Configuring Python Environments in Visual Studio Code ) , but it's probably easiest to run code from within the active environment.

0 Kudos