Unable to use arcpy within pipenv project

591
4
01-23-2023 11:58 AM
PhilipOrlando
New Contributor III

I am trying to set up a new python project that uses pipenv to manage my package dependencies. I'm getting the following error when trying to import arcpy within the virtual environment generated by pipenv:

 

 

 

arcpy must be run from inside the Conda environment, which ArcGIS
uses to manage the installation of Python.
You can access this environment by launching Python from the links inside the
"ArcGIS > ArcGIS Pro" Start Menu Group:
    "Python Interactive Terminal" starts a Python session.
    "Python Command Prompt" opens a Command Prompt window initialized with Conda.

Or by running the proenv.bat file in the terminal:
     C:\> C:\Program Files\ArcGIS\Pro\bin\python\scripts\proenv.bat

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import arcpy
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\__init__.py", line 70, in <module>
    raise ImportError("arcpy needs to run within an active ArcGIS Conda environment")
ImportError: arcpy needs to run within an active ArcGIS Conda environment

 

 

 

I have followed the pipenv documentation for using "other python distributions" and entered the following command to establish my new python environment:

 

 

 

pipenv --python="C:/Program Files/ArcGIS/Pro/bin/Python/envs/arcgispro-py3/python.exe" --site-packages

 

 

 

According to the documentation, pointing pipenv to the location of the arcgispro-py3 binary and adding the --site-packages flag should be enough to create a new python environment with the arcpy site package available. 

I'm hoping an Esri rep can help solve this issue. The only documentation I've seen from Esri is related to pipenv and the ArcGIS API for Python, but I need arcpy for my project.

0 Kudos
4 Replies
ShaunWalbridge
Esri Regular Contributor

If you upgrade to Pro 2.9 or newer, using ArcPy will raise a warning rather than an error and it should work OK with pipenv. That said, we only specify our dependencies for ArcPy and our Python environments via conda, so using pipenv may not work for dependencies which cross-reference things that we need for the core functionality, such as NumPy. If you are able to upgrade, you may also be interested in the new arcpy-base package that's available in Pro 3.1, it skips the Jupyter and other more complicated parts of our stack to provide a minimum 'import arcpy' package.

Cheers, Shaun

0 Kudos
undestu
New Contributor

Hi Shaun or anyone else in the know

Could I check if the interpretation of the sentence below is correct - if one is using arcpy with pipenv, one may not be able to have the right versions of the dependency packages required for arcpy to work over time since the packages were supposed to be managed in conda?

 

That said, we only specify our dependencies for ArcPy and our Python environments via conda, so using pipenv may not work for dependencies which cross-reference things that we need for the core functionality, such as NumPy.

0 Kudos
PhilipOrlando
New Contributor III

Thanks for the suggestion. There were some breaking changes that were introduced to the OriginDestinationCostMatrix class within Pro 2.9, so I'm currently stuck using Pro 2.8. Once I refactor all of my legacy code to use the new ClosestFacility class instead of ODCM, then I'll upgrade and try using pipenv again. 

0 Kudos
MelindaMorang
Esri Regular Contributor

Hello Philip.  Shaun asked me to weigh in here because I work on the arcpy.nax solver classes.  I'd like to try to help you with your issue with the OriginDestinationCostMatrix class that's preventing you from upgrading to newer software.

To my knowledge, we did not make any breaking changes between 2.8 and 2.9, unless you were inadvertently relying on some undocumented behavior or a bug we fixed.  Older scripts should continue working in newer software, at least, that is our goal.

Could you please give me some more details about the specific nature of your issue?  Hopefully I can advise on how to fix it without your having to rewrite the whole thing using ClosestFacility.

0 Kudos