Talk:Python

Make sure the permissions on the enclosing directory of your pythons scripts is also set to 755.
 * Does it possible to have a 775 ? -- Bact 01:32, 26 Jul 2005 (PDT)

---

I'm fairly sure that the "py" extension is not required as long as you have a correct shebang line at the beginning of the script. If no one has contradicted this by the time I visit this page again, I'll correct the page. - Cobra libre 16:54, 1 Jun 2005 (PDT)

I copied the "Sample Python CGI" script in the Python page and try to run it from my dreamhost space, also followed all the steps described. But it doesn't work. Instead, the web browser show this message:

Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, webmaster@siit.net and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

What I have done wrong ?

cheers, -- Bact 01:32, 26 Jul 2005 (PDT)

Ok, it got fixed now. It just only because of difference in Windows vs UNIX newline. -- Bact 01:43, 26 Jul 2005 (PDT)
 * What is an internal server error, and why doesn't my CGI work?

Why aren't the python dev packages installed? Is there a way I can update things like the mysql-python library without them? I don't think there is. When I try to run the install script with --root=$HOME, it complains that it can't find the /usr/lib/python2.3/config/Makefile, which indeed is not there. --Johnsu01 13:25, 4 Mar 2006 (PST)

Quoting from the article:

The Basics All Python CGI scripts on dreamhost MUST... 1. end in ".py" (NOTE: ".cgi" works as well)

Actually, it appears to be sufficient to have ".py" anywhere in the file. I named a script 7and Apache still tried to run it. I consider this a bug.

2. have #!/usr/bin/python in the very first line of the file (NOTE: #!/usr/bin/python2.x or #!/usr/bin/env python2.x will work as well)

If you don't have this, you will get an ISE and a "Premature end of script headers" in your error.log. If you have a correct chmod set, you will also see "failed to open log file" and "fopen: Permission denied."

3. be marked as executable: chmod 755

If you don't have this, you will get an ISE and a "Premature end of script headers" in your error.log.

5. If you want to view printed output from your Python code, you must print print "Content-type: text/html\n\n" as the first line of output.

If you don't have this, you will get an ISE and a "malformed header from script" error in your error.log.

Hope this helps you debug! --Glasserc 15:12, 19 Sep 2006 (PDT)


 * After the Content-Type header, you only need one \n, not two. The print function has the oft-annoying behaviour of adding another. But yeah, that ".py.txt.html.junk.tar.gz" thing is really annoying and doesn't make sense. I was trying to name one script "script.py.txt" or something so people could see the source of it, but Apache insisted on executing it. Argh! --Matt Nordhoff (talk) 15:25, 19 Sep 2006 (PDT)


 * I even went to the trouble of opening a support request before I figured out a way to work around this. At least for me, all the .py scripts that I don't want to execute are in one directory, so I could put in the .htaccess for that directory:


 * RemoveHandler .py


 * which sets it not to run any Python scripts in that directory at all. -- Glasserc 15:51, 19 Sep 2006 (PDT)

Shell works; internet doesn't.
Letting you know right away, I've chmodded the entire domain to be 755.

I'm testing out the python by using the example script. It's uploaded, it's chmodded, it works when I ssh to the server and run it, but I keep getting a 500 Internal Server Error when I actually go through my browser.

Looking at the error logs, I see 'fopen: Permission denied', and 'Premature end of script headers: helloworld.py'.

Any ideas?
 * I just took 5 minutes and ran through the article, and tested the example script. Worked fine for me without incident.  I can only suggest you make sure that you do not have problems with *nix style line endings not being present, and/or that you re-check your work, as the example script works as it should on my domain. Again, make sure that the actual file itself is set with 755 permissions, *irrespective" of how you have "chmodded the entire domain". -- Rlparker 19:24, 18 Feb 2007 (PST)


 * I checked, but I didn't find any non-nix end-of-lines. But since I just copy/pasted, why would that matter? Also, I double-checked, the permissions are 755. This isn't really crucial to my site, just something I was playing around with. (I do know Python, just haven't used it on a server before.) Thanks for any help!

---

Any hints if we actually want to use the current 2.5.x versions of Python? --Gizbot 00:42, 20 June 2007 (PDT)
 * You can try editing the first line of the script to reflect the custom python path. #!/usr/bin/env python turns into #!/home/your_username/packages/bin/python --Meteu336 04:43, 2 February 2010 (UTC)

PIL
I'm trying to install the Python imaging library but not having much luck. I'm curious why this isn't available already, and if there is any documentation/steps to help with the installation.
 * Please ask questions like this at http://discussion.dreamhost.com/ -- Scjessey 10:03, 6 January 2008 (PST)

Isn't it much easier just to use easy_install to install virtualenv?
I think the article makes installation much more complicated than needed.

Is there any good reason why a user shouldn't simply do something like:

mkdir -p ~/local/site-packages easy_install --install-dir ~/local/site-packages/ virtualenv

NB. I got the idea from: http://mitchfournier.com/2010/06/25/getting-started-with-virtualenv-isolated-python-environments Spk 03:03, 8 November 2010 (UTC)

Setting the path
The article didn't say this explicitly, and not being an expert, I wish it did. The python command (and hence easy_install) pointed to the /usr/lib/ version of python until I set my path variable like so

export PATH=/home/my_username/env_install_dir/bin:$PATH