How Python calls other. py functions within a. py file

Source: Internet
Author: User

Assume that a file named a.py needs to call the C (x, Y) function in the b.py file

If you are in the same directory, just

Import B if __name__ " __main__ " :    B.C (x, y)

If you just call a single function, you can also

 from Import C if __name__ " __main__ " :    C (x, y)

If a.py and b.py are located in different directories, you can use the following methods

(assuming b.py is located at the root of the D-disk)

1. The path where the reference is located

Import syssys.path.append ('d:/') Import B if __name__= ="__main__"    :Print b.pr (x, y)

2. Using Imp

Import IMPB=imp.load_source ('B','d:/b.py') Import B if __name__= ="__main__"    :Print b.pr (x, y)

How Python calls other. py functions within a. py file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.