Virtualenv: --relocatable does not point activate script to the correct path

Created on 14 Mar 2011  ·  14Comments  ·  Source: pypa/virtualenv

Using Ubuntu 10.04, --relocatable works if I directly invoke the python binary or the setuptools-generated scripts. However, bin/activate still reflects the old path:

> pwd
/home/jhammel
> virtualenv.py foo
New python executable in foo/bin/python
Installing setuptools............done.
> virtualenv.py --relocatable foo
Making script foo/bin/easy_install relative
Making script foo/bin/easy_install-2.6 relative
Making script foo/bin/pip relative
> mv foo bar
> cd bar
> . bin/activate
(foo)> echo $VIRTUAL_ENV
/home/jhammel/foo
(foo)> which python
/usr/bin/python
(foo)>

This is because VIRTUAL_ENV is set to an absolute path in the script. Instead, this should be made relative when --relocatable is called.

Since the activate script must be sourced, it is a bit more complicated to get the path than just (e.g.) dirname $0. The following seems to work in bash:

command=$(history 1) # this should go at the top of the file
parent_path() {
DIRECTORY=$(dirname ${!#})
cd $DIRECTORY/..
pwd
}
VIRTUAL_ENV=$(parent_path $command)

If this meets with your approval, Ian, I'm glad to write a patch.


bug

All 14 comments

sorry for the poor formatting....bitbucket tricked me again :(


Original Comment By: Jeff Hammel

And %~dp0% could be used in a windows environment to get the path to the
current script, that is, for activate.bat.


Original Comment By: Sylvain Prat

I think it's a good idea, a patch would be great.


Original Comment By: Jannis Leidel
  • Changed content.

Original Comment By: Jannis Leidel

Here is a better version for bash:

VIRTUAL_ENV=$(cd $(dirname "$BASH_SOURCE"); dirname `pwd`)

And for (t)csh:

set sourced=($_)

set scriptdir=`dirname "$sourced[2]"`

set scriptpwd=`cd "$scriptdir"; pwd`

setenv VIRTUAL_ENV `dirname "$scriptpwd"`

Original Comment By: Anonymous

This is what I am using:

VIRTUAL_ENV="$(dirname $(dirname $(readlink --canonicalize --no-newline

$BASH_SOURCE)))"

or with the short options

VIRTUAL_ENV="$(dirname $(dirname $(readlink -f -n $BASH_SOURCE)))"

Original Comment By: Matteo Bertini

And an option for those still using tcsh:

set sourced=($_)
set scriptdir=dirname "$sourced[2]"
set scriptpwd=cd "$scriptdir"; pwd
setenv VIRTUAL_ENV dirname "$scriptpwd"

I have made a pull request for this change (which implements the bash script only):
https://github.com/pypa/virtualenv/pull/143

For windows, in ACTIVATE_BAT you can replace:

set VIRTUAL_ENV=__VIRTUAL_ENV__

with:

pushd %~dp0..
set VIRTUAL_ENV=%CD%
popd

This is better than just setting it to %~dp0.. directly in that you end up with the same absolute path you would without the change, but it's relocatable.

Adding a reference to the newest issue #1067 related to this to help consolidate efforts on solving this half-decade old problem with the activation scripts.

This is what I am using:

VIRTUAL_ENV="$(dirname $(dirname $(readlink --canonicalize --no-newline

$BASH_SOURCE)))"

or with the short options

VIRTUAL_ENV="$(dirname $(dirname $(readlink -f -n $BASH_SOURCE)))"

Using readlink could be problematic, as it has different command-line options between Linux, BSD/macOS, and potentially other Unix flavours.

@adah1972, issue #1067 is the latest one. This issue is from 2012, #1067 is from 2017.

@arizvisa Thanks. I commented in #1067 too.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Just add a comment if you want to keep it open. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vbabiy picture vbabiy  ·  4Comments

abn picture abn  ·  4Comments

npinto picture npinto  ·  4Comments

asottile picture asottile  ·  6Comments

erbatyr picture erbatyr  ·  5Comments