Skip to content
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

Closed
vbabiy opened this issue Mar 14, 2011 · 14 comments
Closed

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

vbabiy opened this issue Mar 14, 2011 · 14 comments
Labels

Comments

@vbabiy
Copy link

@vbabiy 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
Copy link
Author

@vbabiy vbabiy commented Mar 14, 2011

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


Original Comment By: Jeff Hammel

@vbabiy
Copy link
Author

@vbabiy 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
Copy link
Author

@vbabiy vbabiy commented Mar 14, 2011

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


Original Comment By: Jannis Leidel

@vbabiy
Copy link
Author

@vbabiy vbabiy commented Mar 14, 2011

  • Changed content.

Original Comment By: Jannis Leidel

@vbabiy
Copy link
Author

@vbabiy 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
Copy link
Author

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

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

@kra kra commented Jun 23, 2011

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

@jpenney
Copy link

@jpenney 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
Copy link

@arizvisa 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
Copy link

@adah1972 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
Copy link

@arizvisa arizvisa commented Nov 3, 2018

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

@adah1972
Copy link

@adah1972 adah1972 commented Nov 5, 2018

@arizvisa Thanks. I commented in #1067 too.

@stale
Copy link

@stale stale bot commented Feb 3, 2019

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.

@stale stale bot added the wontfix label Feb 3, 2019
@stale stale bot closed this Feb 11, 2019
@rtd-helper rtd-helper bot mentioned this issue Oct 16, 2019
@pypa pypa locked and limited conversation to collaborators Jan 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants