-1

I would like to create multiple files with my own functions and store it as multiple script files. what is the best way to make it work. How do I call it from another python script?

For example I have the files a.py, b.py, c.py and x.py in the files a.py, b.py and c.py. I would like to use functions from the other imported files.

def a():
  print("Function a is called")
  print(c())
  return 1

b.py

def b():
  # Something
  print("function b is called.")
  return 2

c.py

def c():
  # de Something
  print("Function C is called.")
  print(b())
  return 3

print(a())

x.py

from a import *
from b import *
from c import *
print(a())
print(b())
print(c())

Thanks.

  • What's the goal with this? If they are in the same directory you could use `os.sys()` to call them? Or do you have a different use case? – Jarred Parr Apr 19 '18 at 13:13
  • 1
    what is the problem with the code you posted? – Ma0 Apr 19 '18 at 13:14
  • 1
    Possible duplicate of [Python: How to import other Python files](https://stackoverflow.com/questions/2349991/python-how-to-import-other-python-files) – eguaio Apr 19 '18 at 13:16
  • The case i have is that i have a script with functions with ask questions though the serial port. TransmitCom(Com,"help") time.sleep(0.20) Error = 0 ReceiveCom(Com,"help", 200 ,"IGNORE") – Leo Ruyven Apr 19 '18 at 13:30
  • You have `a` calling `c` and `c` calling `b`. If you want to do that then `a` needs to import `c` and `c` needs to import `b`. But if you then want `b` to import `a` you will have circular references and it won't work. Putting one function or class per file is common in compiled languages but it doesn't work well in dynamic ones. – BoarGules Apr 19 '18 at 13:40
  • The case i have is that i have a script with functions with ask questions though the serial port. The interface of te total program is in main.py. I use wx python for that. So for the separate test functions witch are called from mail.py I have imported test_functions.py i the file test_functions.py i would like to cal functions from serial_functions.py from Serial_functions import * from test_functions import * if test1: print("Test succeed.") else: print("Test failed.") if test2: print("Test succeed.") else: print("Test failed.") – Leo Ruyven Apr 19 '18 at 13:46
  • @JarredParr The idea is to make a saparation between main program the test functions and the serial communication functions the scripts are all in the same directory. The main.py is ment for the gui the serial_functions.py is ment for the serial communication and i would like the test_functions.py as simple as possible. – Leo Ruyven Apr 19 '18 at 13:52
  • @LeoRuyven I understand your wish to compartmentalize the code, but if you want a function `a` in `a.py` to call function `c` in `c.py` then `a` needs to import `c`. Doing all the imports in `x` has no effect on `a`. `import` is not a `#include`, it is an executable statement, which is why circular imports don't work. – BoarGules Apr 19 '18 at 13:56

1 Answers1

0

Looks fine. Function c() should return some value (integer) like other functions, a, b. Within x.py, just import should do, e.g.

x.py

import a
import b
import c
print(a())
print(b())
print(c())

make sure all indentations are ok

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
  • Thanks, but my problem is that if i run x.py I have an error in module c.py. NameError: name 'a' is not defined – Leo Ruyven Apr 19 '18 at 13:25