Skip to content

Commit f54ea7a

Browse files
committed
Make sure that paths added via 'add2virtualenv' always end up being listed *before* regularily installed packages in sys.path. This ensures that you can always use the command to replace an installed package with a out-of-virtualenv version.
1 parent 17eec7b commit f54ea7a

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

virtualenvwrapper_bashrc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,9 @@ function add2virtualenv () {
356356
return 1
357357
fi
358358

359-
path_file="$site_packages/virtualenv_path_extensions.pth"
359+
# Prefix with _ to ensure we are loaded as early as possible,
360+
# and at least before easy_install.pth.
361+
path_file="$site_packages/_virtualenv_path_extensions.pth"
360362

361363
if [ "$*" = "" ]
362364
then
@@ -370,15 +372,21 @@ function add2virtualenv () {
370372
return 1
371373
fi
372374

373-
touch "$path_file"
375+
if [ ! -x "$path_file" ]
376+
then
377+
echo "import sys; sys.__plen = len(sys.path)" >> "$path_file"
378+
echo "import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new)" >> "$path_file"
379+
fi
380+
374381
for pydir in "$@"
375382
do
376383
absolute_path=$(python -c "import os; print os.path.abspath(\"$pydir\")")
377384
if [ "$absolute_path" != "$pydir" ]
378385
then
379386
echo "Warning: Converting \"$pydir\" to \"$absolute_path\"" 1>&2
380387
fi
381-
echo "$absolute_path" >> "$path_file"
388+
contents=$(cat "$path_file")
389+
echo "$contents" | sed "1a $absolute_path" > "$path_file"
382390
done
383391
return 0
384392
}

0 commit comments

Comments
 (0)