5

I have anaconda python distribution in ~/anaconda/bin/python but what shebang can I use to call this version of python instead of the old one that is default on the server?

# /opt/python/bin/python2.7 is the one i've been trying

O.rka
  • 161
  • 1
  • 1
  • 5

2 Answers2

7

To add to the already accepted answer (and reply to 0xc0de's comment), I think we can use:

#!/usr/bin/env python

Then we need to activate the Conda environment, or source the custom Python path that we want to use.

By doing so, #!/usr/bin/env will find the right Python version, and also will make sure that the script will run correctly inside the virtual environment.

IgNite
  • 171
  • 1
  • 2
5

The rest of the first line after the shebang is parsed as an interpreter directive.

If you want your script to be interpreted / run by an specific binary you need to point to that binary after the shebang.

In this case you need to write something like this:

#!/home/yourusername/anaconda/bin/python

Henrik Pingel
  • 9,380
  • 2
  • 28
  • 39
  • 1
    Isn't there a way to make the shebang text environment name agnostic? – 0xc0de Jun 12 '19 at 06:48
  • What do you mean by environment in this context? You could overwrite the `path` on the os level but that might break os python packages and I wouldn't recommend it. – Henrik Pingel Jun 12 '19 at 07:19