ramkrishanbhatt / modwsgi

Automatically exported from code.google.com/p/modwsgi
0 stars 0 forks source link

Reorder sys.path after applying WSGIPythonPath or python-path option. #112

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
When WSGIPythonPath and python-path option are applied, the site.addsitedir() 
function is used. Problem with this is that any new 
module search directories are appended to sys.path when by rights they should 
be inserted at head before those of the main Python 
installations directories. Being prepended would be preferred as it then allows 
modules in the extra directories to override module of 
same name but different version in main Python installation.

To avoid this problem, advice when using virtual environments with mod_wsgi is 
to always create a virgin virtual environment which 
is not associated with site-packages of main Python installation and refer to 
that virgin virtual environment with WSGIPythonHome 
directive. Any specific virtual environments used with WSGIPythonPath or 
python-path options would also be disassociated from 
main Python installation site-packages, but they would then have all required 
modules installed.

If it is hard to install a required module in a virtual environment, for 
example where it is installed into main Python installation site-
packages using Debian packaging or similar, doing as above, to get access to it 
would mean adding manually a symlink or .pth file, 
as appropriate, into virtual environment site-packages directory.

In virtualenv 1.3, an attempt is made to address this ordering issue with 
site.addsitedir() by providing a Python script which should be 
execfile'd. This script will call site.addsitedir() and then reorder sys.path 
to move newly added directories to the front of sys.path. 
Thus, if the ordering problem affects you, or you really need to access modules 
in main Python installation site-packages and can't 
disassociate from it, that script in virtualenv 1.3 can be used from WSGI 
script file.

Since virtualenv is providing the steps for doing this, probably a good idea to 
integrate the same reordering into mod_wsgi itself. 
This wouldn't use virtualenv script though, but would just duplicate it in C 
code in mod_wsgi, as mechanism isn't itself linked to 
virtualenv but would be applicable to any virtual environment mechanism.

Original issue reported on code.google.com by Graham.Dumpleton@gmail.com on 26 Sep 2008 at 11:45

GoogleCodeExporter commented 8 years ago
The new script that virtualenv 1.3 provides contains:

import sys
import os

base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
site_packages = os.path.join(base, 'lib', 'python%s' % sys.version[:3], 
'site-packages')
prev_sys_path = list(sys.path)
import site
site.addsitedir(site_packages)
sys.real_prefix = sys.prefix
sys.prefix = base
# Move the added items to the front of the path:
new_sys_path = []
for item in list(sys.path):
    if item not in prev_sys_path:
        new_sys_path.append(item)
        sys.path.remove(item)
sys.path[:0] = new_sys_path

One issue with this script is that it sets sys.prefix. Thus that this script is 
offered as way of using virtualenv 1.3 with mod_python/mod_wsgi is wrong and 
may cause 
problems because it may override what would be correct sys.prefix.

The only part of this script which is really relevant to mod_wsgi is the 
reordering of sys.path.

Original comment by Graham.Dumpleton@gmail.com on 28 Sep 2008 at 7:05

GoogleCodeExporter commented 8 years ago
Code for reordering sys.path added in revision 1102 of subversion trunk for 
mod_wsgi 3.0.

Original comment by Graham.Dumpleton@gmail.com on 28 Oct 2008 at 11:22

GoogleCodeExporter commented 8 years ago
The actual Python code snippet you should use to do reordering, ie., with 
unnecessary stuff above deleted is:

  import sys 
  prev_sys_path = list(sys.path) 
  import site 
  site.addsitedir('/some/path/.../site-packages') 
  # Move the added items to the front of the path: 
  new_sys_path = [] 
  for item in list(sys.path): 
      if item not in prev_sys_path: 
          new_sys_path.append(item) 
          sys.path.remove(item) 
  sys.path[:0] = new_sys_path 

Ie., this would replace the:

  site.addsitedir('/some/path/.../site-packages')

Ammendments to:

  http://code.google.com/p/modwsgi/wiki/VirtualEnvironments

coming soon.

Original comment by Graham.Dumpleton@gmail.com on 27 Nov 2008 at 6:17

GoogleCodeExporter commented 8 years ago
Backported to 2.X for 2.4 in r1248.

Original comment by Graham.Dumpleton@gmail.com on 17 Mar 2009 at 6:44

GoogleCodeExporter commented 8 years ago
In python 2.6 Python has gained the support for "Per user site-packages 
directory"
(pep-370) ... actually it's not per user but per config, since a single user 
can have
several of these local site-packages directories.

The important part is the site.adduserpackage() function
(http://www.python.org/dev/peps/pep-0370/#implementation) that does the correct 
thing
vis-a-vis virtual environments.

Original comment by gdam...@gmail.com on 1 Apr 2009 at 1:23

GoogleCodeExporter commented 8 years ago
Version 2.4 of mod_wsgi now released.

Original comment by Graham.Dumpleton@gmail.com on 11 Apr 2009 at 10:25