How to invoke code in another Python file

Source: Internet
Author: User

The search path for the module

The search path of the module is placed in the Sys.path list, if the default Sys.path does not contain its own module or package path, you can dynamically join (sys.path.apend). Here are the rules for adding Sys.path under the Windows platform.

1. Sys.path The first path is often the directory where the main module resides. Add an empty item in the interactive environment that corresponds to the current directory.

2. If the PYTHONPATH environment variable exists, Sys.path will load the directory specified by this variable.

3, we try to find the Python home, if we set the PYTHONHOME environment variable, we think this is Python home, otherwise, we use Python.exe directory found lib/os.py to infer Python home.

If we do find the Python home, then the related subdirectories (Lib, Plat-win, LIB-TK, etc.) will be added to the Sys.path on a python home basis and imported (executed) lib/ site.py, add the site-specific directory and the package under it.

If we do not find the Python Home, the registry Software/python/pythoncore/2.5/pythonpath entries are added to Sys.path (HKLM and HKCU merge), but the associated subdirectories are not automatically added.

4, if we do not find the Python Home, and there is no pythonpath environment variable, and cannot find the Pythonpath in the registry, then the default relative path will be joined (such as:./lib;. /plat-win, etc.).

Summarized as follows

When running Python.exe in the installed home directory, first infer Python Home, if Pythonhome is found, the Pythonpath in the registry is ignored, otherwise the pythonpath of the registry is added.

If the PYTHONPATH environment variable exists, Sys.path will certainly load the directory specified by this variable.

If Python.exe is in another directory (different directories, such as embedding through COM into other programs), Python home will not infer that the Pythonpath of the registry will be used at this time.

If Python.exe cannot find his home directory (Pythonhome) and the registry does not Pythonpath, the default relative directory will be added.

How to invoke code in another Python file

No matter what language we choose to program, it is impossible to have only one file (except "Hello World"), usually we need to call another file in a file function Yes data and so on, in short, to manipulate the code in other files, in Java, As long as the same file directory, we do not need to import via import, but in Python, we need to import through imports, so that we can apply other files defined in the functions and data and other code. This is what we need to know about our new contact with Python.

The following is an example of two files, for example, we have a.py and b.py two files, when we need to apply the Func () function in the a.py in the b.py file, how to do it, there are two cases, respectively, in the same file directory and not in the same directory under the description.

In the same file directory

A call to the Func () function in the a.py file can be done with the following two statements in the b.py file

Import a #引用模块
A.func ()

or a

Import a# Application Module
From a import func #引用模块中的函数
Func () #这里调用函数就不需要加上模块名的前缀了

Under Different file directories

If you are not in the same directory, Python cannot find it, you must set up a lookup path, add the folder that contains the module to the system lookup path

Import Sys
Sys.path.append (' path where ' a.py is located ')
Import a
A.func ()

How to invoke code in another Python 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.