Day 3 - Running Python without any serving it with web application.

prashannaprashanna is verified member.

Administrator
Staff member
Joined
Mar 7, 2024
Messages
11
Reaction score
1
As I was searching for a way to host my Python scripts in the SSD cloud hosting plan, I stumbled around an Apache module that can run Python as a backend without any need to serve the WSGI (Web Server Gateway Interface). The module was named mod_python. So, hoping that the module was pre-installed and enabled, I started configuring the .htaccess file and Python scripts as per the instructions. However, I got an internal server error indicating the module was not installed or enabled in the cloud. 

Moreover, there is even mod_wsgi, which lets you run the Flask or Django web server directly from Apache, which is not available here. After this, I asked support for all the Apache modules available here. The list comprised of many modules, but only thing that caught my mind is the CGI (Common Gateway Interface). This came pre-installed in the server and apparently can run most of the scripts, such as Perl, Python, Ruby, Node, etc. However, I needed to poke around with the config files, which I don't have access to. Trying out the same configuration on .htaccess didn't work. When requesting the file, the server spitted out the same file, unexecuted.
Here is where I tried.

Ok! I had one more plan (spoilers: it worked.)
What if I used Laravel's Process facade? The facade starts a new process within the Laravel ecosystem, in this case, a Python executable. This is how I did it:

Code:
// app/Http/Controller/PythonController.php
use Illuminate\Support\Facades\Process;
...
function python(Request $request) {
    $output = Process::path(base_path('python'))->run('python3 script.py')->output(); // Results of all the 'prints' from python script
    // process the output and return user the view/json
}

// routes/web.php
Route::get('/python', [PythonController::class, 'python']);

And on the root directory of the Laravel project, I created a folder named Python with a file named script.py

Code:
// python/script.py

print("Hello from python")

As a result, the $output variable in the 'python' function was set to "Hello from python\n". With this approach, you need to code most of the logic in Laravel/PHP and the 'required' logic in Python, as it will be slow to start a new process again and again. You can even do a Process::forever. However you do it, it will be slower anyway. So you may ask, Why do we need Python, and specifically Python? To which my reply would be "AI."

Hence, as soon as the method worked, I searched for an AI model on Kaggle by Google. On my search, I stumbled over a text-generation AI model named Gemma. So I tried downloading the ~13GB AI model into the cloud instance as well as installing the required Python packages in the cloud. 
Code:
pip install --user keras-nlp
pip install --user keras

But I got an error stating, "PermissionError: [Errno 13] Permission denied: 'c++'". This was expected, as we are only given access to specified binaries of PHP, Python, Ruby, Composer, Lua, and Perl. And some Python packages require C/C++ to compile while installing.

Code:
Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File ".../grpcio/setup.py", line 263, in <module>
        if check_linker_need_libatomic():
      File ".../grpcio/setup.py", line 213, in check_linker_need_libatomic
        stderr=PIPE)
      File "/usr/lib64/python3.6/subprocess.py", line 729, in __init__
        restore_signals, start_new_session)
      File "/usr/lib64/python3.6/subprocess.py", line 1364, in _execute_child
        raise child_exception_type(errno_num, err_msg, err_filename)
    PermissionError: [Errno 13] Permission denied: 'c++'

So, the conclusion for today is: Even though I got Python to link with the web server in SSD Cloud Hosting, most of the packages cannot build on the system, or the older version of Python is a problem. I would take it as a win, though!

Signing off,
Prashanna Tamrakar
 
Back
Top