py / bridge
"Python-In-PHP" allows you to use any Python packages directly in PHP, as if they were native PHP classes. This PHP-Python bridge comes with built-in package manager, which is integrated directly into Composer
Requires
- php: >=8.2
- composer-plugin-api: ^2.6
- ext-sockets: *
- nette/php-generator: ^4.2
- textalk/websocket: ^1.6
Requires (Dev)
- composer/composer: ^2.8
- pestphp/pest: ^4.0
README
Installation
composer require syncfly/python-in-php
You need to answer "yes" when prompted to activate the plugin.
Documentation
You can find the documentation here: (coming soon)
Package manager
The Python-in-PHP Package Manager is a built-in package manager based on uv that allows you to install and manage Python packages.
Intsall a package
composer pip install <package-name>
Uninstall a package
composer pip uninstall <package-name>
Upgrade a package
composer pip install --upgrade <package-name>
Examples
Look at an example of using transformers and torch in PHP for running an AI model:
<?php use py\transformers; use py\torch; $model_name = 'google/gemma-3-4b-it'; $tokenizer = transformers\AutoTokenizer::from_pretrained($model_name); $model = transformers\AutoModelForCausalLM::from_pretrained( $model_name, torch_dtype: torch::$bfloat16, device_map: "auto" ); $messages = [ ['role' => 'user', 'content' => 'Why PHP is great?'] ]; $input_ids = $tokenizer->apply_chat_template( $messages, return_tensors: 'pt', add_generation_prompt: true ); $outputs = $model->generate( $input_ids, max_new_tokens: 2048 ); $result = $tokenizer->decode($outputs[0], skip_special_tokens: true);
Or simplier with transformers pipeline:
<?php use py\transformers; $pipe = transformers\pipeline( 'text-generation', model: 'google/gemma-3-4b-it', torch_dtype: torch::$bfloat16, device_map: 'auto' ); $messages = [ ['role' => 'user', 'content' => 'Why PHP is great?'] ]; $output = $pipe($messages, max_new_tokens: 2048); $generated = $output[0]['generated_text']; $result = end($generated)['content'];
License
This project is distributed under a source-available license.
Allowed:
- Using the package in your projects, including commercial ones ✅
- Making changes and submitting pull requests to this repository
Prohibited:
- Creating public forks or distributing the project under your own name
- Uploading the code (modified or original) anywhere else
✅ All contributions are accepted through pull requests to the official repository
Attribution:
- Attribution notice is required for software with publicly available source code
See LICENSE.md for full details.
