7

Say i have a python file called a.py with the functions x,y,z.

I want to import them to a new file called b.py

I tried: from a import * AND from a import all with no luck.

I can just do it separately: from a import x , from a import y ....

How can i import them ALL at once?

shayms8
  • 671
  • 6
  • 13
  • 28

1 Answers1

14

You can import a file and then use the functions on that file using the import name before the function name.

import example #this import the entire file named example.py

example.myfunction() #this call a function from example file
Saelyth
  • 1,694
  • 2
  • 25
  • 42
  • 2
    How does this relate to the question? Why does `from a import x` work and not `from a import *`?. – roganjosh Nov 18 '17 at 14:41
  • it imports all functions of a file to be used from a different file. And as bonus I added the proper way to call those imported functions. That covers the entire question O,o – Saelyth Nov 18 '17 at 14:49
  • Actually, you're right. `import *` is not a good idea but I'm curious as to why it doesn't work in this case. – roganjosh Nov 18 '17 at 14:51
  • I am not sure. Is hard to tell without checking the real code. It might be that * is for modules or packages, while the OP may not have a `__init__` in there and just a simple .py file with a method or two in no particular order. I need to research this also as I never seen that behaviour before (never tried to be honest). __Edit:__ found this https://stackoverflow.com/questions/2360724/what-exactly-does-import-import – Saelyth Nov 19 '17 at 00:55