3.2 KiB
Core
All built-in functions of Python are implemented as static methods of the PyCore class. Please refer to the Python documentation for usage of built-in methods.
Importing Packages
$os = PyCore::import('os');
This will return a PyModule object upon success. You can import built-in packages, as well as third-party packages or user-defined packages.
You can only load modules and it does not support the from module import class syntax in Python. You can use the following syntax instead.
$module = PyCore::import($moduleName);
$class = $m->$className;
Python will cache the loaded modules internally. When a module is loaded for the second time, it will automatically return the module from the cache, avoiding duplicate loading. Therefore, it can be used in short-lived environments such as PHP-FPM/Apache without performance issues.
Import Paths
You can use PyCore::import('sys')->path->append() to add directories to the import path list.
For example, if you have a custom package located at /workspace/app/user.py, you can load it using the following steps:
PyCore::import('sys')->path->append('/workspace')to add/workspacetosys.path.PyCore::import('app.user')will automatically searchsys.pathand load the correspondingapp/user.pypackage.
Built-in Methods
PyCore::import($module)- Import a modulePyCore::str()- Convert an object to a stringPyCore::repr()-PyCore::type()- Get the type of an objectPyCore::locals()- Get all local variables in the current scopePyCore::globals()- Get all global variablesPyCore::hash()- Get the hash valuePyCore::hasattr()- Check if an object has a specific attribute-PyCore::id()Get the internal identifier of an objectPyCore::len()Get the lengthPyCore::dir()Get all attributes and methods of an objectPyCore::int()Construct an integerPyCore::float()Construct a floatPyCore::fn()Construct a callable functionPyCore::eval()Execute Python codePyCore::dict()Construct a dictionary objectPyCore::set()Construct a set objectPyCore::range()Construct a range sequencePyCore::scalar()Convert aPyObjectobject to a scalar type in PHP, for example,PyStrwill be converted toPHP string,Dict/Tuple/Set/Listwill be converted toArray
PyCoreimplements the__callStatic()magic method, so calling a static method ofPyCorewill automatically call the corresponding method in thebuiltinsmodule of Python. You can refer to Built-in Functions to learn more about the usage of built-in methods.
Dynamic Linking Library Issue
If you encounter a dynamic linking library error when importing the library, the reason may be an incorrect LD path. You can set the environment variable to specify the dynamically linked library path for the Python C module.
export LD_LIBRARY_PATH=/opt/anaconda3/lib
php plot.php
This approach only applies to the current bash session and will not affect the global settings. Do not directly modify /etc/ld.so.conf.d/*.conf to add /opt/anaconda3/lib, as this may cause conflicts with libc library and affect the normal operation of other programs in the operating system.