Skip to content
Advertisement

How to include a python file in PHP without giving complete path?

I have a python program called ‘cal.py’ and a PHP program called ‘index.php’ in the same folder called ‘trails’.

Now I want to include the python file in the PHP file without specifying the whole path of python program. For example, I want to avoid giving the path as “C://folder/subfolder../cal.py”.

This is because I want to run the same program in multiple other PCs for which the python program will have different paths.

This is not a duplicate of other similar questions because I don’t want to know how to specify the path in a single computer but I want it to be able to run the same program in multiple devices.

Is there any way to specify the path inside php file like -> “./cal.py” or “./trails/cal.py” ?

index.php

    <?php
       echo shell_exec(__DIR__."/cal.py");
    ?>

cal.py

    print ("Hello")

Any help would be appreciated. Thanks in advance.

Advertisement

Answer

I think you want to use the __DIR__ magic constant. This always returns with the folder path where the php script is executed from. So if the py file is in the same folder as the php file then you can get the full path on every machine like this: __DIR__ . '/cal.py'

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement