I'm a Python newb so please bear with me if I'm asking a stupid question.

I have a RPi3 running Jessie lite. I have Apache and PHP installed. I also have netatalk installed and have mounted my pi e.g. open afp:// so that I can access it via OSX Finder.

In Apache I have set document root to a directory in home so that I can reach and edit files on my laptop via VS Code (installed on laptop, not RPi).

I have a really simple PHP / HTML form which sends an on/off value to a Python script which turns an LED on and off and all works perfectly. However I'm not sure how to properly go about debugging Python scripts that are called by PHP. For example my PHP file includes:

if (!empty($_GET["action"])) {
  $output = shell_exec("sudo python /home/pi/raspberry-pi-play/switchLED.py ".strtolower($_GET["action"])) ;
  echo "switching led: ". strtoupper($_GET["action"]);

in my Python script I have:

import sys
import RPi.GPIO as GPIO ## Import GPIO library
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT

#getting the input parms
argsLen = len(sys.argv)

if argsLen == 2:
  if sys.argv[1] == "on":
    #Turn ON
    print("Turning LED ON")

In VSCode I get a warning unable to import RPi.GPIO which I'm not sure I can do anything about, but I would like to understand how I can print out the values that are being received by the Python script. For example where can I see print("Turning LED ON") being printed? Nothing seems to show within VS Code integrated terminal.


1 Answer 1


How about logging to a file:

import datetime

def logPrint(s):
    now = datetime.datetime.now()
    f.write(now.strftime('%H:%M:%S')+' '+s+'\n')


This will produce a file eg /home/pi/log2019-02-01.txt containing:

21:42:00 testing
  • thanks for this, works great! One quick question, if i log logPrint('arg = ' + sys.argv[1]) the log get generated. If I add a function in e.g. logPrint('arg = ' + sys.argv[1] + 'lenght = ' + len(sys.argv)) it doesn't. Is there a reason for that or is it my error?! Feb 1, 2019 at 22:14
  • 1
    len(..) is an int so add str(len(..))
    – CoderMike
    Feb 1, 2019 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.