<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Accessing and cloning python environment with Pro SDK in ArcGIS Pro SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/accessing-and-cloning-python-environment-with-pro/m-p/1180374#M8232</link>
    <description>&lt;P&gt;I'm trying to create a configuration/ add-in for a department and because Pro invalidates the cloned python environments after updates, I need a way to ensure that a cloned environment is created and the required python packages (one being our in-house package) are installed. Since some packages do not install through the Pro's conda manager, I need to install through pip.&amp;nbsp; I cannot ask each user to execute pip commands after each update of Pro. Nor do I really want to touch each computer after each update.&lt;/P&gt;&lt;P&gt;I haven't been able to find any examples of accessing the cloned environments, or if the python/ conda environment is exposed to the ArcGIS Pro SDK. A similar question without an answer was asked &lt;A href="https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-fetch-current-clone-python-environment-in/td-p/769934" target="_self"&gt;here&lt;/A&gt;.&amp;nbsp; I created a script that checks for the packages I need to install and installs them if they are not, but it needs the cloned environment first.&lt;/P&gt;&lt;P&gt;Simply, I need to do this:&lt;/P&gt;&lt;P&gt;User starts Pro after an update or fresh install.&lt;/P&gt;&lt;P&gt;Be able to:&lt;/P&gt;&lt;P&gt;1. Check if the python env is cloned and clone it if isn't.&lt;/P&gt;&lt;P&gt;&amp;nbsp; 1.a. If it exists, check if python packages are installed and install if they are not.&lt;/P&gt;&lt;P&gt;2. Set active python environment to the clone that contains added packages.&lt;/P&gt;&lt;P&gt;3. Run python scripts from the addin buttons.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If this is not possible to do programmatically through the SDK, I'd welcome any options.&amp;nbsp; Thanks!&lt;/P&gt;</description>
    <pubDate>Mon, 06 Jun 2022 19:10:06 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2022-06-06T19:10:06Z</dc:date>
    <item>
      <title>Accessing and cloning python environment with Pro SDK</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/accessing-and-cloning-python-environment-with-pro/m-p/1180374#M8232</link>
      <description>&lt;P&gt;I'm trying to create a configuration/ add-in for a department and because Pro invalidates the cloned python environments after updates, I need a way to ensure that a cloned environment is created and the required python packages (one being our in-house package) are installed. Since some packages do not install through the Pro's conda manager, I need to install through pip.&amp;nbsp; I cannot ask each user to execute pip commands after each update of Pro. Nor do I really want to touch each computer after each update.&lt;/P&gt;&lt;P&gt;I haven't been able to find any examples of accessing the cloned environments, or if the python/ conda environment is exposed to the ArcGIS Pro SDK. A similar question without an answer was asked &lt;A href="https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-fetch-current-clone-python-environment-in/td-p/769934" target="_self"&gt;here&lt;/A&gt;.&amp;nbsp; I created a script that checks for the packages I need to install and installs them if they are not, but it needs the cloned environment first.&lt;/P&gt;&lt;P&gt;Simply, I need to do this:&lt;/P&gt;&lt;P&gt;User starts Pro after an update or fresh install.&lt;/P&gt;&lt;P&gt;Be able to:&lt;/P&gt;&lt;P&gt;1. Check if the python env is cloned and clone it if isn't.&lt;/P&gt;&lt;P&gt;&amp;nbsp; 1.a. If it exists, check if python packages are installed and install if they are not.&lt;/P&gt;&lt;P&gt;2. Set active python environment to the clone that contains added packages.&lt;/P&gt;&lt;P&gt;3. Run python scripts from the addin buttons.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If this is not possible to do programmatically through the SDK, I'd welcome any options.&amp;nbsp; Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jun 2022 19:10:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/accessing-and-cloning-python-environment-with-pro/m-p/1180374#M8232</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-06-06T19:10:06Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing and cloning python environment with Pro SDK</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/accessing-and-cloning-python-environment-with-pro/m-p/1180403#M8233</link>
      <description>&lt;P&gt;I manage my non-ESRI conda envs through a &lt;A href="https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#creating-an-environment-from-an-environment-yml-file" target="_self"&gt;yaml&lt;/A&gt; requirements file. That tends to simply things, because you can rebuild with a one liner. I have not tested this approach with conda envs in ArcGIS Pro, but would be curious if it works for you.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jun 2022 19:56:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/accessing-and-cloning-python-environment-with-pro/m-p/1180403#M8233</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-06-06T19:56:24Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing and cloning python environment with Pro SDK</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/accessing-and-cloning-python-environment-with-pro/m-p/1180570#M8237</link>
      <description>&lt;P&gt;Thanks, I discounted the yaml route because there wasn't a way (back then) that I could find to set the Pro project's environment to environments that were created outside of Pro's own conda manager. But since then, &lt;A href="https://community.esri.com/t5/python-documents/what-s-new-with-python-in-arcgis-pro-2-8-the/ta-p/1062087" target="_blank" rel="noopener"&gt;what's-new-with-python-in-arcgis-pro-2-8&lt;/A&gt; demonstrates that it is possible so I'll have to reconsider it as an option and point a button to a bat file.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The 6th step Hannes provided is what I needed:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Generate a list of additional packages installed in your current environment,&lt;BR /&gt;with the conda command:&amp;nbsp;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;gt;&amp;gt;&amp;gt; conda env export &amp;gt; env.yaml&lt;/FONT&gt;&lt;/LI&gt;&lt;LI&gt;(Optional) If you have additional dependencies installed through pip,&lt;BR /&gt;find those with pip freeze:&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;gt;&amp;gt;&amp;gt; pip freeze &amp;gt; requirements.txt&lt;/FONT&gt;&lt;/LI&gt;&lt;LI&gt;Create a new environment by cloning arcgispro-py3, and activate it:&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;gt;&amp;gt;&amp;gt; conda create --clone arcgispro-py3 --name my-env --pinned&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;gt;&amp;gt;&amp;gt; activate my-env&lt;/FONT&gt;&lt;/LI&gt;&lt;LI&gt;Add back missing conda packages from your current environment:&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;gt;&amp;gt;&amp;gt; conda env update -n my-env -f env.yaml&lt;/FONT&gt;&lt;/LI&gt;&lt;LI&gt;(Optional) Add back missing packages from pip:&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;gt;&amp;gt;&amp;gt; pip install -r requirements.txt&lt;/FONT&gt;&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;(Optional) Make it the default for the ArcGIS Pro application and the&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;"Python Command Prompt":&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;&amp;gt;&amp;gt;&amp;gt; proswap my-env&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/LI&gt;&lt;/OL&gt;</description>
      <pubDate>Tue, 07 Jun 2022 12:57:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/accessing-and-cloning-python-environment-with-pro/m-p/1180570#M8237</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-06-07T12:57:30Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing and cloning python environment with Pro SDK</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/accessing-and-cloning-python-environment-with-pro/m-p/1180727#M8238</link>
      <description>&lt;P&gt;Thanks for the good link and good workflow. I might have to give this a second shot in Pro as well.&lt;BR /&gt;&lt;BR /&gt;Worth noting that you can pip install from within the YAML file as well. This eliminates the needs for a requirements.txt and guarantees that the pip installed packages make it to the correct conda env (incase there is somehow more than one pip installed).&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 17:49:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/accessing-and-cloning-python-environment-with-pro/m-p/1180727#M8238</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-06-07T17:49:08Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing and cloning python environment with Pro SDK</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/accessing-and-cloning-python-environment-with-pro/m-p/1180773#M8239</link>
      <description>&lt;P&gt;After some loss of hair, I cobbled a bat file together that performs the steps of cloning the default environment, installs missing packages, and sets the pro's active python env to the new env.&amp;nbsp; I am no CMD genius so if this can be improved, please feel free to do so and post it.&amp;nbsp; I got a lot of 700 errors from jupyter-notebook at times while developing this.&amp;nbsp; Not sure what the exact fix was but it seemed to dissappear when I cloned the default env and updated it with cloned env.&amp;nbsp; Gotta quit while I'm ahead.&lt;/P&gt;&lt;P&gt;The called scripts (activate.bet &amp;amp; proswap.bat) are using full paths because it would stop if I tried to activate the new env to run commands.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: VARIABLES                                                                    :
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off

SETLOCAL
SET "ENV_NAME=assessors-env"
SET "ARCGIS_DEFAULT=arcgispro-py3"
SET "ARCGIS_CLONE=arcgispro-py3-clone"

SET "PRO_PATH=C:\Program Files\ArcGIS\Pro\bin\Python"
SET "CLONED_PATH=%LOCALAPPDATA%\ESRI\conda\envs"
REM Set the default script path for this iteration. Maybe fixes jupyter notebook install errors?
SET PATH=%PATH%;"C:\Program Files\ArcGIS\Pro\bin\Python\Scripts"

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: COMMANDS                                                                     :
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

REM start by activating the arcgis pro conda env
call "C:\Program Files\ArcGIS\Pro\bin\Python\Scripts\activate.bat" &amp;amp; (
	REM export the cloned arcgispro-py3-clone environment that contains the additional packages installed outside of arcgis pro
		conda env export -n %ARCGIS_CLONE% &amp;gt; "%CLONED_PATH%\%ARCGIS_CLONE%\environment.yml"	
		ECHO ^-^-^&amp;gt; %ARCGIS_CLONE% conda environment exported to "%CLONED_PATH%\%ARCGIS_CLONE%\environment.yml"
		
	REM clone the deafult arcgispro-py3 environment to the new environment name
		REM CALL conda config --add channels conda-forge
		REM CALL conda config --add channels esri
		conda create --clone %ARCGIS_DEFAULT% --name %ENV_NAME% --pinned
		ECHO ^-^-^&amp;gt; %ENV_NAME% environment created
		
	REM update the new environment with packages that are not installed from the environment.yml created earlier.
	    REM conda env list
		REM conda env update -n %ENV_NAME% -f "%PRO_PATH%\envs\%ARCGIS_DEFAULT%\environment.yml"
		conda env update -n %ENV_NAME% -f "%CLONED_PATH%\%ARCGIS_CLONE%\environment.yml"
		ECHO ^-^-^&amp;gt; %ENV_NAME% environment updated
	
	REM set the active environment in Pro to the new environment
		CALL "%PRO_PATH%\Scripts\proswap.bat" %ENV_NAME%
		ECHO ^-^-^&amp;gt; Pro env set
	)
pause
exit&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now to get it to run from C#.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 22:28:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/accessing-and-cloning-python-environment-with-pro/m-p/1180773#M8239</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-06-07T22:28:00Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing and cloning python environment with Pro SDK</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/accessing-and-cloning-python-environment-with-pro/m-p/1180825#M8241</link>
      <description>&lt;P&gt;This is the c# part placed in the button onclick event. - I'm still playing with how to capture the messages or just let them display in the console window because I dont think the SDK has an output window other than the geoprocessing one.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;await QueuedTask.Run(async () =&amp;gt;
            {
                try
                {
                    ProcessStartInfo processInfo = new ProcessStartInfo();
                    processInfo.FileName = @"\\gisdata\Transfer_Tech\ESRISoftware\Addins\python-env.bat";
                    processInfo.UseShellExecute = false;
                    processInfo.CreateNoWindow = false;
                    processInfo.UseShellExecute = false;
                    processInfo.RedirectStandardError = false;
                    processInfo.RedirectStandardOutput = false;

                    using (Process process = Process.Start(processInfo))
                    {
                        process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =&amp;gt;
                            MessageBox.Show("output&amp;gt;&amp;gt;" + e.Data);
                        process.BeginOutputReadLine();

                        process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =&amp;gt;
                            MessageBox.Show("error&amp;gt;&amp;gt;" + e.Data);
                        process.BeginErrorReadLine();

                        process.WaitForExit();

                        //MessageBox.Show("ExitCode: {0}", process.ExitCode);

                        process.Close();
                    }

                    // Show a messagebox with the results
                    MessageBox.Show("Python environment updated successfully.");

                }
                catch (Exception exc)
                {
                    // Catch any exception found and display in a message box
                    if (exc.Message != "StandardOut has not been redirected or the process hasn't started yet.")
                    {
                        MessageBox.Show($"Exception caught while trying to validate the python env: {exc.Message}.");
                    }
                    
                    return;
                }
            });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 22:26:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/accessing-and-cloning-python-environment-with-pro/m-p/1180825#M8241</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-06-07T22:26:49Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing and cloning python environment with Pro SDK</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/accessing-and-cloning-python-environment-with-pro/m-p/1190330#M8350</link>
      <description>&lt;P&gt;I created an idea for providing conda documentation and how can we deploy environments using Pro SDK or python along with our configs and add-ins in case anyone reaches this and wants to help out.&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/arcgis-pro-ideas/create-documentation-for-esri-s-conda/idi-p/1189824/highlight/true" target="_blank" rel="noopener"&gt;create documentation for esri's conda&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jul 2022 13:02:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/accessing-and-cloning-python-environment-with-pro/m-p/1190330#M8350</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-07-07T13:02:53Z</dc:date>
    </item>
  </channel>
</rss>

