Run Python3 From C


----------------
Written by:
Stephen Andersen, Lee Ingram
Group 8
Winter 2016
----------------

Introduction

This tutorial or 'app note' will demonstrate how to run python3 from C

This tutorial expects you to understand basic C and Python3.



RUN COMMANDS FROM C

		char buffer[100];
		sprintf(buffer, "python3 hello.py");
		puts("starting python3 in C"); 
		system(buffer);
		

Example


C Side


		#include  <stdio.h>
		#include  <stdlib.h>
		
		int main(){
		  char buffer[100];
		
		  sprintf(buffer, "python3 hello.py");
		  puts("starting python3 in C"); 
		  system(buffer); // Runs command in bash; Stops C from executing
		  puts("resuming C");
		  return 0;
		}
		

Python Side


		from os import system
		import sys
		import time
		
		if __name__ == "__main__":
			print("starting python")
			time.sleep(2);# Sleep for 2 seconds
			print("leaving python")