This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Allow site.addsitedir insert to beginning of sys.path
Type: enhancement Stage: test needed
Components: Library (Lib) Versions: Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 2.7, Python 2.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: cjw296, gotgenes, mejo, webmaven
Priority: normal Keywords:

Created on 2010-01-20 20:19 by gotgenes, last changed 2022-04-11 14:56 by admin.

Messages (6)
msg98084 - (view) Author: Chris Lasher (gotgenes) Date: 2010-01-20 20:19
Would it be possible to add an extra option to site.addsitedir so that it left-appends (inserts at the beginning of the list rather than the end of the list) to sys.path the new path?

The use case for this is that sometimes the user has local versions of packages and modules they would prefer to use over versions installed system-wide. Since Python searches for packages and modules in the order given by sys.path, inserting the new path(s) at the beginning of sys.path. This leads to hack-ish work-arounds, such as the one given by http://code.google.com/p/modwsgi/wiki/VirtualEnvironments

If there was an option to left-append with site.addsitedir, it would really help in cases such as these.

Note that I'm not certain at the moment how best to add additional paths that are found in .pth files, i.e., whether to insert them at the beginning as well, or insert them between the initial path and the original paths (the paths that existed before site.addsitedir is called).
msg98085 - (view) Author: Chris Lasher (gotgenes) Date: 2010-01-20 21:21
One correction: by "beginning of sys.path", what I really mean is, "the portion of sys.path after the initial ''". I forgot that '', the empty path, should always be at the start of sys.path to ensure that packages and modules in the current working directory are given top priority. Thus, I would like to see insertions between sys.path[0] and sys.path[1].

Sorry for the confusion.
msg98184 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2010-01-23 12:30
This sounds like something best taken to the python-ideas mailing list.
Can you do that and update the issue with the outcome of any discussion?
msg139167 - (view) Author: Jonas Meurer (mejo) Date: 2011-06-26 11:44
I would be interested in that feature as well. It's currently impossible to use custom new versions of a python module by adding the directory with site.addsitedir in case a old version of the module is already installed in the python systemwide path.
msg226632 - (view) Author: Michael R. Bernstein (webmaven) Date: 2014-09-09 12:26
The lack of a left-append option for site.addsitedir(path), or an site.insertsitedir(index, path) (which is what I would consider a better solution), causes quite a few contortions on some hosted platforms, notably Google App Engine, for vendored packages.

I've been trying to find a more elegant solution than the following code, but haven't been able to find anything:

import os
import site
import sys

dirname = 'lib'
dirpath = os.path.join(os.path.dirname(__file__), dirname)

sys.path, remainder = sys.path[:1], sys.path[1:]
site.addsitedir(dirpath)
sys.path.extend(remainder)

And even asked on StackOverflow: http://stackoverflow.com/questions/25709832/what-is-the-cleanest-way-to-add-a-directory-of-third-party-packages-to-the-begin

Having a site.insertsitedir(index, path) available would make things so much simpler.
msg226696 - (view) Author: Michael R. Bernstein (webmaven) Date: 2014-09-10 13:26
And in case it isn't clear how such a method would help, here is what the earlier code would look like:

    import os
    import site
    
    dirname = 'lib'
    dirpath = os.path.join(os.path.dirname(__file__), dirname)

    site.insertsitedir(1, dirpath)

But (other than the imports) it could even be reduced to a one-liner:

    site.insertsitedir(1, os.path.join(os.path.dirname(__file__), 'lib'))
History
Date User Action Args
2022-04-11 14:56:56adminsetgithub: 51992
2014-09-10 13:26:44webmavensetmessages: + msg226696
2014-09-09 12:26:48webmavensetnosy: + webmaven

messages: + msg226632
versions: + Python 3.3, Python 3.4, Python 3.5
2011-06-26 11:44:41mejosetstatus: pending -> open
versions: + Python 2.6
nosy: + mejo

messages: + msg139167
2010-01-23 15:45:49brian.curtinsetstatus: open -> pending
priority: normal
stage: test needed
versions: + Python 2.7, Python 3.2
2010-01-23 12:30:56cjw296setnosy: + cjw296
messages: + msg98184
2010-01-20 21:21:56gotgenessetmessages: + msg98085
2010-01-20 20:19:22gotgenescreate