laleksandrov / larapy
A Laravel package to provide Python script helpers and manage Python resources.
Requires
- php: ^8.2
- illuminate/support: ^9.0 || ^10.0
- symfony/process: ^6.0
Requires (Dev)
- orchestra/testbench: ^8.0
This package is auto-updated.
Last update: 2025-07-20 05:04:54 UTC
README
LaraPy is a Laravel package designed to make managing and executing Python scripts seamless. It provides helper functions to simplify referencing Python scripts in your Laravel project and executing them with arguments.
Features
python_path()
: Resolve the absolute path to Python scripts located inresources/python
.python($script, $args = [])
: Execute Python scripts and pass arguments dynamically.
Installation
- Install LaraPy via Composer:
composer require laleksandrov/larapy
- After installing, the package automatically creates the directory
resources/python
, where your Python scripts should be placed. If the directory does not exist, create it manually:
mkdir -p resources/python
Usage
Referencing Python Scripts
LaraPy provides the python_path()
helper to get the absolute path of a Python script within resources/python
.
Example:
$scriptPath = python_path('example.py'); // Output: /absolute/path/to/resources/python/example.py
Executing Python Scripts
LaraPy allows you to execute Python scripts using the python()
helper. You can pass arguments as an array.
Example:
If you have a script called example.py
in resources/python
:
# example.py import sys if len(sys.argv) > 1: print(f"Arguments received: {', '.join(sys.argv[1:])}") else: print("No arguments provided.")
Run the script with arguments in PHP:
$output = python('example.py', ['arg1', 'arg2']); echo $output; // Output: Arguments received: arg1, arg2
Error Handling:
If the script fails, the helper throws a Symfony\Component\Process\Exception\ProcessFailedException
. You can handle this exception as follows:
try { $output = python('example.py', ['arg1']); echo $output; } catch (ProcessFailedException $e) { echo $e->getMessage(); }
Troubleshooting
resources/python
Directory Missing? If theresources/python
folder wasn't automatically created during installation, you can manually create it:
mkdir -p resources/python
- Python Not Found?
Ensure Python is installed on your system and accessible via the
python
command. If your environment usespython3
, update thepython()
helper's command to usepython3
.
Requirements
- PHP version
^8.0
- Laravel version
^10.0
- Python installed and accessible in your environment
Contributing
We welcome contributions! To contribute:
- Fork this repository.
- Implement your feature or bugfix in a new branch.
- Submit a Pull Request.
Author
- laleksandrov (GitHub Profile)
License
This package is licensed under the MIT license.