Run Python 3 scripts

From any directory in macOS

Posted by paul on 2018.11.08

Quick access to a python 3.6 script on Mac

This tutorial explains how to enable running a python script written in python version 3.x (using version 3.6 in this tutorial) only with typing in the filename and hit Enter key in macOS Terminal, while in any present working directory.

The more clunky method would be something like

python3.6 ~/scripts/somescript.py

Or

cd ~/scripts
python3.6 somescript.py

Or (assuming you have a virtualenv named python3env with python 3.x)

workon python3env
python ~/scripts/somescript.py

You can avoid having to type in the extra commands by doing following:

  1. Keep python scripts in ~/bin directory.
  2. First line of script will have #!/path_/.virtualenvs/all-python36-tools/bin/python3.6

Note you will be typing in commands in Terminal.

Keeping scripts in bin folder

First, quick introduction to keeping scripts in folder ~/bin/.

On a Mac, you can allow running a shell script easily from any present working directory by doing below:

  1. Make directory named bin in your home directory. Note ~/ in the command.
  2. mkdir ~/bin
  3. Leave a shell script (ex: nowdate.sh) in folder ~/bin with following code.
  4. #!/bin/bash
    echo "Today is:"
    date
  5. Make nowdate.sh executable
  6. chmod 750 ~/bin/nowdate.sh
  7. While in any directory, start typing now and hit tab key twice. Now nowdate.sh should auto fill in, and you can just hit Enter key to run nowdate.sh.
  8. Now you can execute shell script nowdate.sh from ANY directory on the Mac. You don’t have to switch to directory ~/bin/ and run ~/.nowdate.sh. Or type in the direct path to the script.

Having the executable script files in ~/bin allows you to run the script from any directory on the Mac. You just need to make sure the script file does not start with reserved system commands such as mv or cp to avoid any overlap conflict.

Run python scripts from any directory

Next step is preparing to run a Python script (coded for python 3.6) from any directory.

Install Python 3.6 on Mac.

Mac comes with python 2.x (ex: 2.7) by default. To run python 3.6 in macOS, you need to install python 3.6 along side the default python version that comes with macOS. Instruction for this is available on internet.

virtualenv

With python, you can use virtualenv to create self contained virtual environments by following steps. Check online for detailed tutorial for working with virtualenv and mkvirtualenv.

  1. Install virtualenv.
  2. Install mkvirtualenv.
  3. Create a new virtualenv using Python 3.6. Value “all-python36-tools” can be named anything. The single quote in below command is actually a back tick, not a single quote.
  4. mkvirtualenv --python=`which python3.6` all-python36-tools
  5. After above command runs and creates virtualenv called all-python36-tools, you should have a file at the path below on your Mac. Note the exact path on your Mac will be different depending on your username and name for virtualenv.
  6. /Users/paul/.virtualenvs/all-python36-tools/bin/python3.6

Separate environments for separate projects

It’s best to create separate virtualenv environment for different projects. But for running multiple general purpose tools that are not too different, I keep just one virtual environment for all. Just need to make sure this virtual environment is not deleted accidentally.

Add #! and file path at top of python script

By adding #! and the file path as shown below at top of a python script, you can run it simply by typing in only the file name and hit Enter key.

#!/Users/paul/.virtualenvs/all-python36-tools/bin/python3.6

Example for creating a test python script with above line is below.

Default python version on Mac

In Terminal, when you run python, it runs python with default version. As of 2018.11.05 on Mac 10.10, it is 2.7.9. You can test it as shown below.

Open Terminal and run following

python -V

You should get following output.

Python 2.7.9

Create a python script in python 3.6 to test

Let’s create a python script as file ~/bin/nowdate.py with following code. Notee the syntax is for python version 3.6, not 2.7.

#!/Users/paul/.virtualenvs/all-python36-tools/bin/python3.6
import sys
import datetime

print(sys.version_info[:3])
now = datetime.datetime.now()
print(f'Current time: {now}')

Make the file executable.

chmod 750 ~/bin/nowdate.py 

Execute nowdate.py

In Terminal console on your Mac, from any directory, you can execute nowdate.py. Type now and hit tab twice. You will most likely see following:

nowdate.py      nowdate.sh

Execute nowdate.py, and you should see lines similar to following:

(3, 6, 5)
2018-11-07 01:09:52.290702

Line (3, 6, 5) means the script is running with Python 3.6.5.

And following is possible in python 3.5 and newer versions only, not in python 2.7.x.

print(f'Current time: {now}')

That is it. With the simple changes, you can run any python version 3.x script from any directory on your Mac.