Unable to import PIL through PHP












1















<?php
echo exec("python txt_img.py admin 2>&1");
highlight_file(__FILE__);
?>


The php is running with no issues in the server,but when I access the php through browser it's showing the error ImportError: No module named 'PIL'.
The python code is also running without any issues in the server.



# desired size
import random
import sys
from PIL import Image, ImageDraw, ImageFont
img = random.randint(1,10)
image = Image.open(str(img)+'.jpeg')

# initialise the drawing context with
# the image object as background

draw = ImageDraw.Draw(image)
font = ImageFont.truetype('arial.ttf',100)
# starting position of the message
(x, y) = (100,70)
name = sys.argv[1]
color = 'rgb(0,0,0)'
draw.text((x, y), "Welcome "+name, fill=color,font=font)
# save the edited image
img= random.randint(1,10)
img = "greet_"+str(img)+".jpeg"
image.save(img)
print(img)


Thanks in advance.










share|improve this question

























  • but the python script works fine independently?

    – gavin
    Nov 18 '18 at 5:24











  • @gavin yes,the python script works fine

    – Santoshkumar Sunny
    Nov 18 '18 at 5:27











  • I'd suggest you import pip and add a try-catch to pip.main(['install', 'pillow']) before the imports, maybe just on the first run; maybe it's installed currently for another user or root or environment

    – gavin
    Nov 18 '18 at 5:39











  • The directory '/var/www/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. I got this error

    – Santoshkumar Sunny
    Nov 18 '18 at 5:52








  • 1





    solved the proble with : sudo -H pip install pillow Thanks @gavin

    – Santoshkumar Sunny
    Nov 18 '18 at 6:08


















1















<?php
echo exec("python txt_img.py admin 2>&1");
highlight_file(__FILE__);
?>


The php is running with no issues in the server,but when I access the php through browser it's showing the error ImportError: No module named 'PIL'.
The python code is also running without any issues in the server.



# desired size
import random
import sys
from PIL import Image, ImageDraw, ImageFont
img = random.randint(1,10)
image = Image.open(str(img)+'.jpeg')

# initialise the drawing context with
# the image object as background

draw = ImageDraw.Draw(image)
font = ImageFont.truetype('arial.ttf',100)
# starting position of the message
(x, y) = (100,70)
name = sys.argv[1]
color = 'rgb(0,0,0)'
draw.text((x, y), "Welcome "+name, fill=color,font=font)
# save the edited image
img= random.randint(1,10)
img = "greet_"+str(img)+".jpeg"
image.save(img)
print(img)


Thanks in advance.










share|improve this question

























  • but the python script works fine independently?

    – gavin
    Nov 18 '18 at 5:24











  • @gavin yes,the python script works fine

    – Santoshkumar Sunny
    Nov 18 '18 at 5:27











  • I'd suggest you import pip and add a try-catch to pip.main(['install', 'pillow']) before the imports, maybe just on the first run; maybe it's installed currently for another user or root or environment

    – gavin
    Nov 18 '18 at 5:39











  • The directory '/var/www/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. I got this error

    – Santoshkumar Sunny
    Nov 18 '18 at 5:52








  • 1





    solved the proble with : sudo -H pip install pillow Thanks @gavin

    – Santoshkumar Sunny
    Nov 18 '18 at 6:08
















1












1








1








<?php
echo exec("python txt_img.py admin 2>&1");
highlight_file(__FILE__);
?>


The php is running with no issues in the server,but when I access the php through browser it's showing the error ImportError: No module named 'PIL'.
The python code is also running without any issues in the server.



# desired size
import random
import sys
from PIL import Image, ImageDraw, ImageFont
img = random.randint(1,10)
image = Image.open(str(img)+'.jpeg')

# initialise the drawing context with
# the image object as background

draw = ImageDraw.Draw(image)
font = ImageFont.truetype('arial.ttf',100)
# starting position of the message
(x, y) = (100,70)
name = sys.argv[1]
color = 'rgb(0,0,0)'
draw.text((x, y), "Welcome "+name, fill=color,font=font)
# save the edited image
img= random.randint(1,10)
img = "greet_"+str(img)+".jpeg"
image.save(img)
print(img)


Thanks in advance.










share|improve this question
















<?php
echo exec("python txt_img.py admin 2>&1");
highlight_file(__FILE__);
?>


The php is running with no issues in the server,but when I access the php through browser it's showing the error ImportError: No module named 'PIL'.
The python code is also running without any issues in the server.



# desired size
import random
import sys
from PIL import Image, ImageDraw, ImageFont
img = random.randint(1,10)
image = Image.open(str(img)+'.jpeg')

# initialise the drawing context with
# the image object as background

draw = ImageDraw.Draw(image)
font = ImageFont.truetype('arial.ttf',100)
# starting position of the message
(x, y) = (100,70)
name = sys.argv[1]
color = 'rgb(0,0,0)'
draw.text((x, y), "Welcome "+name, fill=color,font=font)
# save the edited image
img= random.randint(1,10)
img = "greet_"+str(img)+".jpeg"
image.save(img)
print(img)


Thanks in advance.







php python linux pip importerror






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 10:13









gavin

99212




99212










asked Nov 18 '18 at 5:22









Santoshkumar SunnySantoshkumar Sunny

83




83













  • but the python script works fine independently?

    – gavin
    Nov 18 '18 at 5:24











  • @gavin yes,the python script works fine

    – Santoshkumar Sunny
    Nov 18 '18 at 5:27











  • I'd suggest you import pip and add a try-catch to pip.main(['install', 'pillow']) before the imports, maybe just on the first run; maybe it's installed currently for another user or root or environment

    – gavin
    Nov 18 '18 at 5:39











  • The directory '/var/www/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. I got this error

    – Santoshkumar Sunny
    Nov 18 '18 at 5:52








  • 1





    solved the proble with : sudo -H pip install pillow Thanks @gavin

    – Santoshkumar Sunny
    Nov 18 '18 at 6:08





















  • but the python script works fine independently?

    – gavin
    Nov 18 '18 at 5:24











  • @gavin yes,the python script works fine

    – Santoshkumar Sunny
    Nov 18 '18 at 5:27











  • I'd suggest you import pip and add a try-catch to pip.main(['install', 'pillow']) before the imports, maybe just on the first run; maybe it's installed currently for another user or root or environment

    – gavin
    Nov 18 '18 at 5:39











  • The directory '/var/www/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. I got this error

    – Santoshkumar Sunny
    Nov 18 '18 at 5:52








  • 1





    solved the proble with : sudo -H pip install pillow Thanks @gavin

    – Santoshkumar Sunny
    Nov 18 '18 at 6:08



















but the python script works fine independently?

– gavin
Nov 18 '18 at 5:24





but the python script works fine independently?

– gavin
Nov 18 '18 at 5:24













@gavin yes,the python script works fine

– Santoshkumar Sunny
Nov 18 '18 at 5:27





@gavin yes,the python script works fine

– Santoshkumar Sunny
Nov 18 '18 at 5:27













I'd suggest you import pip and add a try-catch to pip.main(['install', 'pillow']) before the imports, maybe just on the first run; maybe it's installed currently for another user or root or environment

– gavin
Nov 18 '18 at 5:39





I'd suggest you import pip and add a try-catch to pip.main(['install', 'pillow']) before the imports, maybe just on the first run; maybe it's installed currently for another user or root or environment

– gavin
Nov 18 '18 at 5:39













The directory '/var/www/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. I got this error

– Santoshkumar Sunny
Nov 18 '18 at 5:52







The directory '/var/www/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. I got this error

– Santoshkumar Sunny
Nov 18 '18 at 5:52






1




1





solved the proble with : sudo -H pip install pillow Thanks @gavin

– Santoshkumar Sunny
Nov 18 '18 at 6:08







solved the proble with : sudo -H pip install pillow Thanks @gavin

– Santoshkumar Sunny
Nov 18 '18 at 6:08














1 Answer
1






active

oldest

votes


















0














PIL must be installed for current user or for root, which can be achieved by either installing it through the script itself, using subprocess or by



sudo pip install pillow


For earlier versions of pip (<10) pip.main(['install', package]) could also work but using subprocess is a safer bet.



This is just a hacky way to do this, there must be better ways around this.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53358141%2funable-to-import-pil-through-php%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    PIL must be installed for current user or for root, which can be achieved by either installing it through the script itself, using subprocess or by



    sudo pip install pillow


    For earlier versions of pip (<10) pip.main(['install', package]) could also work but using subprocess is a safer bet.



    This is just a hacky way to do this, there must be better ways around this.






    share|improve this answer




























      0














      PIL must be installed for current user or for root, which can be achieved by either installing it through the script itself, using subprocess or by



      sudo pip install pillow


      For earlier versions of pip (<10) pip.main(['install', package]) could also work but using subprocess is a safer bet.



      This is just a hacky way to do this, there must be better ways around this.






      share|improve this answer


























        0












        0








        0







        PIL must be installed for current user or for root, which can be achieved by either installing it through the script itself, using subprocess or by



        sudo pip install pillow


        For earlier versions of pip (<10) pip.main(['install', package]) could also work but using subprocess is a safer bet.



        This is just a hacky way to do this, there must be better ways around this.






        share|improve this answer













        PIL must be installed for current user or for root, which can be achieved by either installing it through the script itself, using subprocess or by



        sudo pip install pillow


        For earlier versions of pip (<10) pip.main(['install', package]) could also work but using subprocess is a safer bet.



        This is just a hacky way to do this, there must be better ways around this.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 18 '18 at 6:34









        gavingavin

        99212




        99212






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53358141%2funable-to-import-pil-through-php%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()