0

While running a file entitled shuf-new.py in the linux environment, the following command works when I use python 2:

./shuf.py -e bob

However, when I change the first line of my code to #!/usr/bin/python3, I get the following error:

-bash: ./shuf-new.py: /usr/bin/python3: bad interpreter: No such file or directory

I'm not sure how to go about solving this.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • Are you sure you have installed python3? Try running `which python3`, if that finds it, replace `/usr/bin/python3` with that or with `/usr/bin/env python3` – L3viathan Apr 28 '18 at 20:45
  • `#!/usr/bin/env python3` is the standard recommended shebang on Unix systems. If you want to forcibly specify a particular Python interpreter rather than the same one you get at the command line by just typing `python3`, then you should skip `env` and use the path to that interpreter. But you don't want that here, because you don't even know what the path to any specific interpreter is (and don't need to know, or care). – abarnert Apr 28 '18 at 20:54
  • 1
    Please avoid asking the same question multiple times. Possible duplicate of [How to convert turn a python file into a shell script](https://stackoverflow.com/questions/50079867/how-to-convert-turn-a-python-file-into-a-shell-script) – jww Apr 28 '18 at 21:43

1 Answers1

3

While adding the shebang #!/usr/bin/python3 is a way of executing your code with Python 3, it's not the only way, and there's no guarantee it will definitely work because Python 3 may be installed in a directory other than /usr/bin.

If Python 3 is installed on your machine (double-check that by trying to run python3 in the shell), you can always run Python 3 code with python3 your_file.py.

If you want to use the shebang approach, use #!/usr/bin/env python3 or find the exact location of the python3 executable using which python3.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • More importantly, some distributions already refer to python3 as the "main, go-to-guy" version of python, so when you call `$ python foo.py` it will call a python3, and if you want python2, you need to issue `$ python2 foo.py` (this is and has been for quite some time the case e.g. with Arch) – Oliver Baumann Apr 28 '18 at 20:55