5 min read
In one of our Laravel projects, we needed to build a system to estimate how investments could perform over a selected time period. This required handling sophisticated financial logic, ensuring accuracy, and maintaining consistency with existing calculations.
The client already had a Python-based solution that returned results. The key challenge was not rebuilding the logic from scratch, but seamlessly integrating this existing calculation engine into a modern Laravel application hosted on Laravel Cloud.
Our focus was on making this integration secure, maintainable, and transparent for developers, while ensuring the business could continue to rely on accurate, repeatable financial projections without disruption.
We evaluated three possible approaches:
At first, rewriting the code in PHP seemed reasonable. But once we started exploring that path, the trade-offs became obvious.
Python has a clear advantage when it comes to mathematically intensive and data-driven workloads. Its ecosystem is simply better suited for this type of problem.
Libraries such as:
are incredibly powerful, well-optimized, and battle-tested.
In practice, this meant that functionality we could implement in one day using NumPy would easily take a week or more in PHP, with more code, more edge cases, and higher maintenance costs.
So option one was off the table.
We then explored installing Python and its dependencies directly on Laravel Cloud during deployment. This approach made sense conceptually, but ran into practical limitations that we’ll cover shortly.
The third option, bundling Python and all dependencies into a single executable, technically worked. However, it introduced several architectural drawbacks:
We kept this as Plan B, but it was far from ideal.
Python offers several dependency management tools, including:
We initially tried venv, but quickly hit a wall. On Laravel Cloud, you can run commands before building the Docker image, but:
Every attempt to install dependencies failed.
After trying multiple approaches and tools, we eventually landed on uv (from Astral: https://astral.sh/).
uv is a modern, fast Python package manager that allows users without root access to manage virtual environments and dependencies. This made it a perfect fit for Laravel Cloud’s constraints.
Since Python was a core part of this Laravel application, we decided to integrate everything directly into composer.json.
Here’s what we added:
"post-install-cmd": [
"bash -lc '[ -x \"$HOME/.local/bin/uv\" ] || (curl -LsSf https://astral.sh/uv/install.sh | sh)'",
"bash -lc 'export PATH=\"$HOME/.local/bin:$PATH\"; uv venv --seed --python=python3'",
"bash -lc 'export PATH=\"$HOME/.local/bin:$PATH\"; uv pip install -r storage/python/requirements.txt'"
],
This setup can be refactored or moved into deployment scripts, but the key benefit is simplicity:
As a result, running composer install is all developers need to do, both locally and in Laravel Cloud. No extra Python setup steps required.
On the Laravel side, we created a small abstraction to handle Python execution.
app/Support/PythonScriptCaller.php:
class PythonScriptCaller
{
public function runPythonCalculations(SimulationRequestData $payload): SimulationResponseData
{
$scriptPath = base_path('storage/python/calculator.py');
$result = Process::timeout(2)
->env([
'PYTHONUNBUFFERED' => '1',
'PATH' => base_path('.venv/bin'),
])
->input($payload->toJson())
->run("python3 $scriptPath");
if ((int) $result->exitCode()) {
throw new Exception($result->errorOutput());
}
return SimulationResponseData::from(
array_merge(
json_decode(
$result->output(),
true,
flags: JSON_THROW_ON_ERROR
)
)
);
}
}
This class:
From a PHP developer’s perspective, this is just a normal Laravel method call. The underlying language becomes an implementation detail.
Here’s a simplified Python example that receives JSON input and returns JSON output:
ef main():
all_parameters = json.load(sys.stdin)
final_output_json = {}
# Your code goes here
sys.stdout.write(json.dumps(final_output_json, separators=(",", ":"), ensure_ascii=False))
if __name__ == "__main__":
main()
Laravel and its ecosystem are incredibly powerful and developer-friendly. But no single programming language is the best tool for every problem.
Some challenges are better solved with Python. Others with Go, Lua, C++, or something else entirely.
The real strength comes from knowing when to integrate, not rewrite.
This approach shows how Laravel can act as the backbone of a system while delegating specialized tasks to the languages and tools best suited for them.

We are a 200+ people agency and provide product design, software development, and creative growth marketing services to companies ranging from fresh startups to established enterprises. Our work has earned us 100+ international awards, partnerships with Laravel, Vue, Meta, and Google, and the title of Georgia’s agency of the year in 2019 and 2021.
