Python Command Prompt not changing environments correctly

1665
6
Jump to solution
11-18-2021 02:17 PM
DarrenConly
New Contributor III

Greetings All,

I just updated to ArcGIS Pro 2.9, and the following happens in the Python Command Prompt (also see copy/paste from the Python Command Prompt Terminal):

  1. I start out in default environment. If I launch python, it starts as expected, no problem so far.
  2. I activate another environment with packages I normally use. Activation appears to happen without issue, but when I launch python, I get the warning This Python interpreter is in a conda environment, but the environment has
    not been activated. Libraries may fail to load. To activate this environment
    please see https://conda.io/activation
  3. With this instance of python running, if I try importing a package that's in my custom environment (in the example below the package is dbfread), it says the package does not exist.
  4. However, a workaround is to run cd Scripts in the cmd prompt to go one folder deeper from ~\Python\envs\arcgispro-py3 to ~\Python\envs\arcgispro-py3\Scripts. If I do this and launch python, the warning disappears and I can import packages unique to the environment.

All in all, this is a minor inconvenience, i.e., all I have to do is enter cd Scripts before changing environments, but I want to raise it as a quality issue as it should not be necessary.

Lastly, in case you're asking why I don't just use the GUI environment manager, I find the command interface to be less buggy and more transparent (e.g., I can see how things are progressing rather than worrying that something has frozen).

Everything I described is in command line excerpt below. Also, this problem existed with the last version of Pro as well.

 

 

 

 

(arcgispro-py3) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3>python
Python 3.7.11 [MSC v.1927 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()

(arcgispro-py3) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3>activate arcgispro-nov042021

(arcgispro-nov042021) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3>python
Python 3.7.11 [MSC v.1927 64 bit (AMD64)] :: Anaconda, Inc. on win32

Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated.  Libraries may fail to load.  To activate this environment
please see https://conda.io/activation

Type "help", "copyright", "credits" or "license" for more information.
>>> import dbfread
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'dbfread'
>>> quit()

(arcgispro-nov042021) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3>cd scripts

(arcgispro-nov042021) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Scripts>activate arcgispro-nov042021

(arcgispro-nov042021) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Scripts>python
Python 3.7.10 [MSC v.1927 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import dbfread
>>> quit()

(arcgispro-nov042021) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Scripts>

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

I don't use clones, but I am wondering if proswap.bat is worth a try.  If memory serves its useage is.  

>>> proswap arcgispro-nov042021

it is one of the scripts in the folder 

C:\....YourInstallFolder....\bin\Python\Scripts

which contains activate.bat, conda.exe, conda_env.exe, proenv.bat and propy.bat


... sort of retired...

View solution in original post

6 Replies
DanPatterson
MVP Esteemed Contributor

I don't use clones, but I am wondering if proswap.bat is worth a try.  If memory serves its useage is.  

>>> proswap arcgispro-nov042021

it is one of the scripts in the folder 

C:\....YourInstallFolder....\bin\Python\Scripts

which contains activate.bat, conda.exe, conda_env.exe, proenv.bat and propy.bat


... sort of retired...
DarrenConly
New Contributor III

Hi Dan,

 

Thanks for following up. Using "proswap" instead of "activate" did the trick!

Quick question though: If you don't use clones, do you just edit the default env as an administrator?

0 Kudos
DanPatterson
MVP Esteemed Contributor

yes, I have full rights and I am not adverse to reinstalling if need be,  I have a number of blog posts in my current and previous incarnation about installing spyder and other packages in the main environment.

But!!! don't install "stuff" unless you feel comfortable with conda and know how to rollback to a previous state (amongst other things)


... sort of retired...
HannesZiegler
Esri Contributor

Dan gave the correct answer, proswap <env> is the preferred way to activate environments for Pro from the command prompt, it simplifies stuff like the problem you ran into. I just wanted to explain why you were getting the warning in the first place.

Notice that on line 8 (in OP) after you've called activate arcgispro-nov042021 your directory is still pointing to the arcgispro-py3 environment. So while you've activated arcgispro-nov042021 you're still running Python.exe from arcgispro-py3, leading to the problems you described. Once you switched out of the directory with cd you were no longer using the wrong Python.exe (no Python.exe in that folder) so your problems went away.

The reason for that is the Command Search Sequence.

If you're ever in doubt, you can run sys.prefix to see where your Python.exe is called from in your session.

Try this to see it:

(arcgispro-py3) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3>activate arcgispro-nov042021

(arcgispro-nov042021) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3>python
Python 3.7.11 [MSC v.1927 64 bit (AMD64)] :: Anaconda, Inc. on win32

Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated.  Libraries may fail to load.  To activate this environment
please see https://conda.io/activation

Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.prefix
'C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3'
>>> sys.exit()

(arcgispro-nov042021) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3> cd ..

(arcgispro-nov042021) C:\Program Files\ArcGIS\Pro\bin\Python\envs> python
Python 3.7.11 [MSC v.1927 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.prefix
'C:\\<whatever_the_path_to_your_environment_is>\\arcgispro-nov042021

 

RupinderKBakhshi
New Contributor

Thanks for this response. I have a somewhat similar issue that I could not solve entirely by this suggestion. I was earlier using a cloned ArcGIS Pro environment to run Spyder but then my user license changed from a university license to an individual one and I am back to a default project environment under the Python Package Manager. But still, when I open the python command prompt from the windows start menu it opens pointing to the cloned environment (which no longer exists). I tried running what you suggested above but reopening the python command prompt still points to the cloned environment by default. Here is a screenshot 

RupinderKBakhshi_0-1655474850281.png

How can I start fresh with the new default environment? Thanks!

 

0 Kudos
HannesZiegler
Esri Contributor

So from my understanding, %localappdata%\esri\conda\envs\arcgispro-py3-clone is now an empty folder? In the screenshot, you're using activate. Using activate will switch the Windows environment path variable to point to arcgispro-py3, but it won't switch the current directory in your CMD out of arcgispro-py3-clone, so arcgispro-py3 wasn't used until you backed out (cd ..) of arcgispro-py3-clone due to the Command Search Sequence.

reopening the python command prompt still points to the cloned environment by default

The Python Command Prompt will open in the environment currently active in ArcGIS Pro (this is backed by a registry key), so you've probably not activated another environment for Pro. Using activate won't affect the environment that Pro (and therefore the Python Command Prompt) is set to.

Can you open ArcGIS Pro and see what the active environment there is? If it is still arcgispro-py3-clone, try to either switch the active environment to arcgispro-py3 from the Package Manager or use proswap instead of activate from the CMD, like this: 

 

proswap arcgispro-py3

 

If there are no other problems with your environments, the Python Command Prompt (and Pro) should now open with arcgispro-py3 as the active environment. 

 

0 Kudos