For those struggling with basic Python and coding, here's a very simple piece of code that might help you a lot.
Set this code as Main.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 17 10:34:38 2022
@author: nobody
"""
import imp
try:
imp.find_module('externalsubroutine')
found = True
print("subroutine exists")
except ImportError:
found = False
print("Subroutine is missing")
#from externalsubroutine import Testit
import externalsubroutine
externalsubroutine.Testit()
print("Adding 5 and 6")
print(externalsubroutine.ReturnValue(5,6))
Now set this code in a file called "externalsubroutine.py"
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 17 10:31:47 2022
@author: nobody
"""
#try to code an external subroutine
def Testit():
print("yeah, that works")
def ReturnValue(num1, num2):
num3 = num1 + num2
return num3
When you run the code, your output should look similar to this:
runfile('/home/CERN/projects/external subroutines/mainstuff.py', wdir='/home/CERN/projects/external subroutines')
subroutine exists
yeah, that works
Adding 5 and 6
11
Congratulations - you can now put different batches of code into external library files.