To import a custom python package, you can follow the steps below:
Create an empty file named __init__.py in the directory where the Python
file is located. This file tells Python that this directory is a package.
Put customized Python files (modules) into this directory.
In another Python file, use the import
statement to import the custom package or module. For example, if your package name is mypackage
, you can import the entire package using the following statement:
import mypackage
Or, if you only want to import a certain module in the package, you can use the following statement:
from mypackage import module_name
Call a function, class or variable in a package or module. For example, if you import mypackage.module_name
, you can call the functions in it using the following statement:
mypackage.module_name.function_name()
Or, if you use the from mypackage import module_name
statement, you can call the function directly:
function_name()
It should be noted that ensure that the file of your custom package or module is in the path searchable by the Python interpreter, or place it in the current working directory.
The above is the detailed content of How to import python custom package. For more information, please follow other related articles on the PHP Chinese website!