Reputation: 4864
Suppose I have a python shell script of the usual shebang kind (let's suppose it's in a file called foo.py
:
#!/usr/bin/env python
print("Hello World")
with the twist that I need to run it in a given python environment Now, of course, I can write a script of the following kind:
#!/bin/sh
conda activate myenv
exec foo.py
But this is mildly unsatisfying aesthetically. Is there a way to package the environment into the script to avoid the extra level of scripting?
Upvotes: 1
Views: 3296
Reputation: 2401
Option 1: explicit interpreter path
You can explicitly find the path to the python interpreter in your environment and use that in the shebang:
source activate myenv
which python
Will output something like /Users/me/anaconda/envs/myenv/bin/python
. You can then write the python script's shebang using that full path:
#!/Users/me/anaconda/envs/myenv/bin/python
...
However, it's still kinda ugly.
Option 2: symlinks
#!/usr/bin/env python
just looks through $PATH
for something called "python
" and uses that to run the script. We can use this behavior to get nicer shebangs for our conda environments.
Here's a script to add symlinks in ~/bin
for each conda environment:
#!/usr/bin/env bash
conda_prefix="$HOME/anaconda" # Modify this line if your anaconda folder is somewhere else
mkdir -p "$HOME/bin" # Make ~/bin if it doesn't exist
for env_dir in "$conda_prefix/envs/"*; do
env_name=$(basename "$env_dir")
ln -s "$env_dir/bin/python" "$HOME/bin/$env_name"
echo "Made symlink for environment $env_name"
done
Once you've run that once (and you've added $HOME/bin
to your $PATH
in .profile
), you can reference conda envs directly in the shebang:
#!/usr/bin/env myenv
...
This will find myenv
in the $PATH
as $HOME/bin/myenv
, which is a symlink to $HOME/anaconda/envs/myenv/bin/python
thanks to our script above.
Upvotes: 3