How to call function from another .py file in python?

I am trying to call a function 'myfunction()' defined in another py file 'file2.py' in same directory using below code

from file2 import *

myfunction()

Unfortunately, its executing my function twice. The import statement is also executing the function. Any insights on how to avoid this?

Probably you are calling inside your file2.py your function myfunction()

when you do

from file2 import *

you are loading every definitions (class, def, etc), and of course, every function called inside that .py.

To avoid this problem you can call your function myfunction() in your file2.py inside this scope:

if __name__ == '__main__':
    myfunction()

in this way it will not be executed when imported, but only if the file2.py is executed directly:

python3 file2.py

Python - Call function from another file, There isn't any need to add file.py while importing. Just write from file import function , and then call the function using function(a, b) . The network can be created by calling functions from one file to another. Python has a simple way to use the functions of other Python files. Importing that file using import keyword and aliasing it is very simple. For example, the same directory has two Python file baseFile.py and callerFile.py having their functions.

Check your file2 and look for anything with myfunction() in it. If there is something like it, just remove it.

Call a function from another file in Python, Remember the file that contains the function definitions and the file calling the functions must be in the same directory. To use the functions written in one file inside another file include the import line, from filename import function_name . Note that although the file name must contain a . py extension, . There isn't any need to add file.py while importing. Just write from file import function, and then call the function using function(a, b).The reason why this may not work, is because file is one of Python's core modules, so I suggest you change the name of your file.

#File name = file2
class file3:
   def method():
      print ("printing statement")
      return "Hello world"

Main file name file1.py

from file2 import *
print(file3.method())

You can define function and write return statement to avoid extra printing

Calling Functions from Other Files, I can't figure out how to separate my python macros into different files/modules. What I am trying to do is: Have a LibreOffice button call: foo()� It does not work when adding new functions to another file (bar) unless restarted. I'm assuming this is because the script is read at boot up so the import only takes the current functions. Is there any way to get the calc file to re-import other files/functions? reload() does not work

Python - how to call function from another py file?, Get code examples like "python use functions from another file" instantly right import a py file function into another py file � python run function from another file � If you want to call a function from another file in Python then there isn't any need to add file.py at the time of importing. You just need to write from file import function, and then call the function using function(a, b)and you are done.

python use functions from another file Code Example, Get code examples like "call function from another file python" instantly right from your google search results with the Grepper from file.py import function(a,b). In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems. In this article, we will learn how can we call a defined function from another function with help of multiple examples.

call function from another file python Code Example, A Python module is a file with the same name. (plus the .py Everything in somefile.py gets imported. • To refer to These look like Java or C++ method calls. Another way: File test1.py: print "test1.py" File service.py: import subprocess subprocess.call("test1.py", shell=True) The advantage to this method is that you don't have to edit an existing Python script to put all its code into a subroutine. Documentation: Python 2, Python 3

Comments
  • does file2 call myfunction anywhere?
  • You can try using as word like this from file2 import myfunction as myfunc
  • Thank you!.. Exactly this was the issue!