Home > front end >  How to activate specific Anaconda env in Bash shell
How to activate specific Anaconda env in Bash shell

Time:07-27

How can I activate a specific Anaconda Python environment in a Bash shell script (using Git Bash command line)?

I'm on a Windows computer. The only Python in my PATH is the ESRI ArcMap version. I cannot/will not add another version to the PATH because it's well known that this breaks ArcGIS 99% of the time. That's why I am using Anaconda to manage my other Python environments.

I am trying the following (based on How to source virtualenv activate in a Bash script and Running Python File from Command Line with Libraries in venv and receiving the error as shown. I'm new to Bash and usually use Anaconda command line prompt, so this is all new to me, thank you for help

file name "test.sh"

#! C:/Users/name/anaconda3/envs/py38env/python.exe
print("Hello")

Git Bash command prompt:

name@DESKTOP-xxxxxxx MINGW64 ~/Desktop
$ sh test.sh
test.sh: line 3: syntax error near unexpected token `"Hello"'
test.sh: line 3: `print("Hello")'

CodePudding user response:

By executing test.sh by

sh test.sh

you are explicitly running as a shell script, and print("Hello") is not a valid shell command. You should run it as a Python script. If Python is in your PATH, this would be done by

python test.sh

If it isn't, you can of course also provide the Path to your python installation explicitly:

/path/to/your/python test.sh
  • Related