简体   繁体   中英

How to import other Python files

 file abc.py def asb(): print("hellow") def xyz(): print("World")

in file main.py

from abc import asb

from abc import xyz

asb()

xys()

can i import both function only one line eg something like his...

from abc import asb and xyz

thanks:)

from abc import asb, xyz

Or to import all functions:
from abc import *

Do refer to websites. They give lots of examples.
For eg https://www.geeksforgeeks.org/python-call-function-from-another-file/

Taken from Python documentation :

There is a variant of the import statement that imports names from a module directly into the importing module's symbol table. For example:

>>> from fibo import fib, fib2
>>> fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

In your case, this would be

>>> from abc import asb, xyz

If you need to import many functions, I would suggest using:

>>> import abc
>>> abc.asb()
>>> abc.xyz()

or, if your abc is too long, use an alias:

>>> import abc as m
>>> m.asb()
>>> m.xyz()

instead of:

>>> from abc import *

as this will pollute your namespace.

you can separate your function names using comma ','.

from datetime import datetime, date

so in your case, you will have

from abc import asb, xyz

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM