Access arcpy on Linux

4136
4
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
4 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
rkraujutis
New Contributor II

It's easy to reproduce and it's not related with VS Code. Steps to reproduce:

- run `docker run -it --rm continuumio/anaconda3 bash`
- create new environment by executing:
```bash
conda create -n arcgis -c esri python=3.7 anaconda arcgis-server-py3
```
- activate environment by executing:
```bash
conda activate arcgis
```
- start `python3`:
```bash
python3
```
- try import arcpy:
```python
import arcpy
```

Result is:
```
Python 3.7.16 (default, Jan 17 2023, 22:20:44)
[GCC 11.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import arcpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/conda/envs/arcgis/lib/python3.7/site-packages/arcpy/__init__.py", line 75, in <module>
    import _initagsenv
_initagsenv.error: Xvfb: failed initializing arcpy. Check if environment was activated correctly.
```

 

VidmasKondratas
Esri Contributor

I use VS Code to debug arcpy on Linux. It can be finicky and I sometimes get the "Xvfb: failed initializing arcpy. Check if environment was activated correctly." error when importing arcpy. The way I use VS Code to debug and run scripts is basically this:

1) I create Python unit tests to step into the arcpy script. I either run proper unit test or just step into and run main() if I want to run the entire script.

2)  In VS Code I open an integrated bash terminal and copy and paste the following lines of bash code into it

#!/bin/bash

export PATH="$PATH:/home/arcgis/anaconda3/bin"
export ARCGISHOME=/opt/esri/arcgis/server
source activate arcpy3

3) Then in VS Code I make sure the Python interpreter from my arcpy3 conda environment is selected as the Python interpreter.

4) I open the Python unit test file and in VS Code Debug I run the "Python: Current File (Integrated Terminal)" debug configuration to step into a breakpoint.

VidmasKondratas_0-1699462213588.png