Select the Python version (2.x or 3.x) per a file basis in Pythonista 3

There are two Python versions available in Pythonista 3

In Pythonista3, you can select Python version to execute your code – tap and hold the right triangle next to the file name and select 2.7 or 3.6. Usually 3.6 which is default should be fine, but in case you need to run a code written in Python 2.x you’ll need this feature.

6DF76B93-53C3-4D19-B1AD-37FD314A1BD5.jpeg

Some commands on StaSh are written in Python 2.x

Some commands on StaSh, the shell-like powerful CLI add-on to Pythonista3, are written in Python 2. So, if you run it without specifying the version, you get the warning message below.

StaSh v0.7.2 on python 3.6.1
Warning: you are running StaSh in python3. Some commands may not work correctly in python3.
Please help us improving StaSh by reporting bugs on github.

In case you need to install Python 2 packages, you have to run StaSh in Python 2.7 and the warning message won’t appear.

StaSh v0.7.2 on python 2.7.12

Selecting the version everytime you launch StaSh is not smart, is it? 

Specify the Python version to execute your code

When you write a code, it is usually meant to be executed by a specific version, either 2.x or 3.x. Just like macOS or Linux, well, even simpler, you can specify the Python version at the first line – this method is called “Shebang”.

#! python2
# coding: utf-8
# Write your Python 2 codes below

Now you got the idea – yes, add the Shebang to a copy of the launch_stash.py and add it as a shortcut along with the original code so that you can quickly launch StaSh in your desired Python version.

There are lots of useful legacy codes/packages written in Python 2 you can find in pip, in the Internet or books. Don’t spend too much time to hand-code and convert into Python 3. Simply run as-is and/or have Pythonista3 convert it to version 3 – you can find Python 2 to 3 tool in the spanner menu: 

IMG_9338.jpeg

Enjoy!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.