New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--relocatable does not point activate script to the correct path #11

Open
vbabiy opened this Issue Mar 14, 2011 · 13 comments

Comments

Projects
None yet
6 participants
@vbabiy
Copy link
Contributor

vbabiy commented Mar 14, 2011

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.


@vbabiy

This comment has been minimized.

Copy link
Contributor

vbabiy commented Mar 14, 2011

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


Original Comment By: Jeff Hammel
@vbabiy

This comment has been minimized.

Copy link
Contributor

vbabiy commented Mar 14, 2011

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
@vbabiy

This comment has been minimized.

Copy link
Contributor

vbabiy commented Mar 14, 2011

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


Original Comment By: Jannis Leidel
@vbabiy

This comment has been minimized.

Copy link
Contributor

vbabiy commented Mar 14, 2011

  • Changed content.

Original Comment By: Jannis Leidel
@vbabiy

This comment has been minimized.

Copy link
Contributor

vbabiy commented Mar 14, 2011

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
@vbabiy

This comment has been minimized.

Copy link
Contributor

vbabiy commented Mar 14, 2011

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
@dsully

This comment has been minimized.

Copy link
Contributor

dsully commented Apr 11, 2011

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"

@kra

This comment has been minimized.

Copy link

kra commented Jun 23, 2011

I have made a pull request for this change (which implements the bash script only):
#143

@jpenney

This comment has been minimized.

Copy link
Contributor

jpenney commented Mar 6, 2012

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.

@arizvisa

This comment has been minimized.

Copy link

arizvisa commented May 29, 2018

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.

@adah1972

This comment has been minimized.

Copy link

adah1972 commented Nov 1, 2018

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.

@arizvisa

This comment has been minimized.

Copy link

arizvisa commented Nov 3, 2018

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

@adah1972

This comment has been minimized.

Copy link

adah1972 commented Nov 5, 2018

@arizvisa Thanks. I commented in #1067 too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment