How can i run user's code securely on my web app?

I am working on a django based web app that takes python file as input which contains some function, then in backend i have some lists that are passed as parameters through the user’s function,which will generate a single value output.The result generated will be used for some further computation.

Here is how the function inside the user’s file look like :

def somefunctionname(list):

    ''' some computation performed on list''''
    
    return float value

At present the approach that i am using is taking user’s file as normal file input. Then in my views.py i am executing the file as module and passing the parameters with eval function. Snippet is given below.

Here modulename is the python file name that i had taken from user and importing as module

exec("import "+modulename)

result = eval(f"{modulename}.{somefunctionname}(arguments)")

Which is working absolutely fine. But i know this is not the secured approach.

My question , Is there any other way through which i can run users file securely as the method that i am using is not secure ? I know the proposed solutions can’t be full proof but what are the other ways in which i can run this (like if it can be solved with dockerization then what will be the approach or some external tools that i can use with API )?

Any reference or resource will be helpful.

Thanks