The Apache Haus Forum

Please login or register.

Login with username, password and session length
Advanced search  


Welcome to Apache Haus Distribution Forum

Pages: [1]   Go Down

Author Topic: using Python and vitualenv  (Read 4606 times)


  • Newbie
  • *
  • Offline Offline
  • Posts: 2
using Python and vitualenv
« on: March 24, 2017, 07:21:44 PM »

On 64-bit Windows 10, I'm using a Python virtualenv for a project, for which I'd like to serve CGI scripts.
I'm using ApacheHaus's windows binary install for Apache 2.4, 32-bit.  Python is Python 2.7, 32-bit.

Maybe I want/need to use some of the settings of mod_python... but I can't find a suitable binary installer for that.  Or maybe I don't need mod_python.  Here are more details of my problem:

- My scripts need to be deployed/deployable on both Win 10 and Ubuntu.  No problem, I have all my scripts working just fine on both platforms.
- Things get dicey with the "#!" notation: what works on Ubuntu won't work on Windows.
- OK, maybe "ScriptInterpreterSource Registry" can save me...
- ...but the instance of Python I need to run is NOT in the Registry; it is the one that activates when I run the vitualenv command "workon".

"Almost fixes" that won't work for me:
1) I know there are script-based hacks to get virtualenvs to work... but those won't be portable to our Ubuntu installs.  (And of course I HATE having to graft custom "fix me" code at the top of every CGI file.)
2) I can't just "correct" the Windows registry then use "ScriptInterpreterSource Registry" to point to the venv python instance... because that is not right for the REST of my Windows box.
3) I'm not ready/interested to cut over my Python CGI files to WSGI yet.

It seems like I need something like "ScriptInterpreterSource Fixed C:/path/to/my/venv/bin/python.exe"... but that is not a valid arg to "ScriptInterpreterSource".

Or it seems I need to use something like mod_python's "PythonInterpreter" directive... but I can't find a Windows binary build of mod_python for Py 2.7, Apache 2.4.  Is mod_python still alive?  And: would this fix my problem anyways?

I'm having the darnedest time getting this to work.

Help!  Many, many thanks in advance.


  • Administrator
  • Member Elite
  • *****
  • Offline Offline
  • Posts: 886
Re: using Python and vitualenv
« Reply #1 on: March 25, 2017, 01:45:34 AM »

I'm not very up on phthon, ok not at all but I don't see why it cannot use #! since perl scripts use it.
Windows is allowed to also use '! to run vbs scripts.

In httpd.conf down about 3/4 of the way is

    # To use CGI scripts outside of ScriptAliased directories:
    # (You will also need to add "ExecCGI" to the "Options" directive.)
    AddHandler cgi-script .cgi

Have you uncommented that and either changed .cgi to .py or just add .py to the line?
AddHandler cgi-script .cgi .py

ExecCGI must also be set for the directory the scripts are being served from.
Options +ExecCGI

« Last Edit: March 25, 2017, 04:03:10 AM by Gregg »


  • Administrator
  • Member Elite
  • *****
  • Offline Offline
  • Posts: 886
Re: using Python and vitualenv
« Reply #2 on: March 25, 2017, 04:01:53 AM »

Following my own advice above, it works for me.

Python is located at e:/usr/python/python.exe on my server.

The script:
Code: [Select]
print "Content-type: text/html\n\n";
print "<br/><blockquote><h2>Hello World!</h2>\n\n";

print "<table cellpadding=2 cellspacing=0 bgcolor=#f4f4f4>\n";
print "<tr><td>Server Software:</td><td> %s<td></tr>\n" % os.environ['SERVER_SOFTWARE'];
print "<tr><td>SSL Version Library:</td><td> %s<td></tr>\n" % os.environ['SSL_VERSION_LIBRARY'];
print "<tr><td colspan=2>&nbsp;</td></tr>\n";
print "<tr><td>Server Protocol:</td><td> %s<td></tr>\n" % os.environ['SERVER_PROTOCOL'];
print "<tr><td>SSL Protocol:</td><td> %s<td></tr>\n" % os.environ['SSL_PROTOCOL'];
print "<tr><td>SSL Cipher:</td><td> %s<td></tr>\n" % os.environ['SSL_CIPHER'];
print "<tr><td colspan=2>&nbsp;</td></tr>\n";
print "<tr><td>Your Browser:</td><td> %s<td></tr>\n" % os.environ['HTTP_USER_AGENT'];
print "<tr><td>Your IP address:</td><td> %s<td></tr>\n" % os.environ['REMOTE_ADDR'];
print "<tr><td>Country:</td><td> <img align=absmiddle src='/icons/flags_iso/32/%s.png'> %s<td></tr>\n" % (os.environ['MM_REGISTERED_COUNTRY_ISO_CODE'], os.environ['MM_REGISTERED_COUNTRY_NAMES_EN']);
print "<tr><td colspan=2><br/><br/></td></tr>\n";
print "<tr><td align=center colspan=2><center><img src='/icons/apache_pbf.png'></center></td></tr>\n";
print "</table></blockquote><p>\n\n";


  • Newbie
  • *
  • Offline Offline
  • Posts: 2
Re: using Python and vitualenv
« Reply #3 on: March 25, 2017, 05:32:17 AM »

@gregg: Thanks for the input... but it doesn't address my problem.  I CAN run Python CGI on Windows.... and I CAN run it on Ubuntu.

Problem 1:

My application requires the SAME scripts to be deployed to Windows and Ubuntu.  And a Python CGI can only have one "#!".  The same Python script can't be BOTH:

#!/usr/bin/env python  (for Ubuntu)


#!C:\Projects\app1\venv\bin\python.exe  (for Windows)

So she-bangs don't save me.

Problem 2:

I could get around Problem 1 if I used the she-bang notation in the CGI scripts tailored for Ubuntu, then used the Apache directive

ScriptInterpreterSource Registry

in my Apache .conf file on Windows... because this means "ignore the she-bang, and use whatever Windows would run on the commandline for a *.py file."

HOWEVER... my Windows registry has to have my "generic" Python install as the EXE for .py files:


but my project really needs the instance of Python from that project's Python "virtual environment".  That is a SECOND copy of Python on my system, located in:


So "ScriptInterpreterSource Registry" won't work for me.

So I'm asking: are there any more tricks I can use?

a) Can "ScriptInterpreterSource" be used in a way where it specifies the EXACT Python instance? (I don't think so... maybe someone here can tell me I'm wrong)

b) Maybe mod_python's "PythonInterpreter" directive will work... but is mod_python basically dead?  (I can't find a binary build for Windows, Apache 2.4, and Python 2.7.... it doesn't look maintained.)

c) Maybe I can do this with mod_wsgi, and convert my CGI scripts to WSGI.... but that is more work than just one configuration setting since I have to touch every script.  AND I can't find a binary build for mod_wsgi for Windows, Apache 2.4, and Python 2.7 either.

Verily I say again.... help!
Pages: [1]   Go Up

Sitemap 1 2 3 4 5 6 7 8 9 10 11 12 13