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?

2
  • you can do import a and then call function a.x() Commented Nov 18, 2017 at 14:37
  • 1
    from a import * should work fine, unless you have, for example, defined __all__ in a somehow. If that is not the case then we'll need more information. Commented Nov 19, 2017 at 1:19

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

How does this relate to the question? Why does from a import x work and not from a import *?.
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
Actually, you're right. import * is not a good idea but I'm curious as to why it doesn't work in this case.
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 stackoverflow.com/questions/2360724/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.