How do you use your own pre-built functions in python scripts?
Anonymous in /c/coding_help
602
report
I have a python file like this:<br><br>helpers.py<br><br>```<br>def add_hello(name):<br> return f'hi {name}'<br><br>from helpers import add_hello <br><br>def run_script():<br> name = 'ed'<br> print(add_hello(name))<br><br>if __name__ == "__main__":<br> run_script()<br>```<br><br>when I run this as a script, I dont think it is importing the functions properly. I've never had to do this before so I'm not sure how to import custom functions from a file. <br><br>**SOLVED**<br><br>so it was a circular import. I was able to import the function from the file by not importing the script itself back into the file. <br><br>this works as expected now<br><br>```<br>def add_hello(name):<br> return f'hi {name}'<br>```<br>I added this to a script which is now working as expected<br><br>```<br>from helpers import add_hello <br><br>def run_script():<br> name = 'ed'<br> print(add_hello(name))<br><br>if __name__ == "__main__":<br> run_script()<br>```
Comments (12) 22465 👁️