0

I'm converting all my Python2 scripts to work with Python3.

From within a bash wrapper, scripts can be executed with Python3 with:

python3 myScript.py

However, I have an old wrapper which called a Python2 script via the shorthand, i.e

./myScript.py

How do I ensure that this shorthand will run the script with Python3 by default?

The server these scripts are running on is Ubuntu 14.04.

x3nr0s
  • 1,389
  • 1
  • 15
  • 34

2 Answers2

2

You can use the "hash-bang" #!/usr/bin/python3

redhatvicky
  • 1,876
  • 7
  • 8
  • I think you mean "she-bang", and the preferred way to do it would be `#!/usr/bin/env python` since not every distribution will have python in that location (for example NixOS) – SitiSchu Nov 25 '19 at 14:27
  • Yeah depends on the unix Flavors she-bang/hash-bangs.https://www.in-ulm.de/~mascheck/various/shebang/ – redhatvicky Nov 25 '19 at 14:31
0

more information regarding your operating system would be helpful. If you are on linux or mac you can add the following to the first line of your script:

#!/usr/local/bin/python

adjusting for the location of your python3. Then you could change the file to executable using:

chmod u+x script

After this you would be able to run your script with just

script