0

I have written a function in python, which I want to be able to use in many different scripts.

Can I save this and then load in?

So the function is

def dosomething(x):
     do something 

I want to save this as a file in my documents

file=r/xxx/xxx/dosomething.py

Then in a new script I want to load it in and use it

New Script

    df=data
    
    load_function(path=file)

    df=df.apply(dosomething)

Is anything like this possible?

fred.schwartz
  • 2,023
  • 4
  • 26
  • 53
  • https://docs.python.org/3/reference/import.html –  Aug 28 '21 at 13:13
  • Does this answer your question? [Call a function from another file?](https://stackoverflow.com/questions/20309456/call-a-function-from-another-file) – Mark Aug 28 '21 at 13:32

2 Answers2

1

You can use an import statement. for example if your script is called actions.py:

from actions import do_something
Daniel
  • 1,895
  • 9
  • 20
1

It's possible like below.

# Custom Script Path: /Users/mert/workspace/my_func.py

def say_hello(name):
    print(f"Hello {name}")
# Your another project folder

import sys
sys.path.append('path-to-your-func-folder')

import my_func
my_func.say_hello('Mert')

# Output
>> Hello Mert

MertG
  • 753
  • 1
  • 6
  • 22