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.

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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)
Yeah depends on the unix Flavors she-bang/hash-bangs.in-ulm.de/~mascheck/various/shebang
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

6 Comments

Added OS info in description
You can use #!/usr/bin/env python to avoid hardcoding the location of the python interpreter.
@SitiSchu, wouldn't that end up hard coding your python interpreter to what is in /usr/bin/env?
@Kelvin note the space, it runs /usr/bin/env with python as argument. env will run the first python it finds in your $PATH. More info here: stackoverflow.com/a/2429517/7228176
on my Mac it requires /usr/bin/env python3 for this to work
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.