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: pyenv activate for bash and tcsh
Type: enhancement Stage: needs patch
Components: Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: PyAcrisel, vinay.sajip
Priority: normal Keywords:

Created on 2017-07-03 19:36 by PyAcrisel, last changed 2022-04-11 14:58 by admin.

Messages (9)
msg297611 - (view) Author: Arnon Sela (PyAcrisel) Date: 2017-07-03 19:36
Remove hard coded path in activate family of virtualenv scripts.

Currently, VIRTUAL_ENV is set hard coded.  Event after --relocatable

Instead, change to have is defined dynamically:

E.g. bash:

called=$_
[[ $called != $0 ]] && fullpath="${BASH_SOURCE[@]}" || fullpath=$0
fullpath=$(readlink -f $fullpath)
VIRTUAL_ENV=$(dirname $(dirname $fullpath))

E.g. tcsh:

set sourced=($_)
if ("$sourced" != "") then
    set fullpath="$sourced[2]"
endif
if ("$0" != "tcsh") then
    set fullpath="$0"
endif
set fullpath=`readlink -f $fullpath`
set binpath=`dirname $fullpath`
setenv VIRTUAL_ENV `dirname $binpath`
msg298351 - (view) Author: Arnon Sela (PyAcrisel) Date: 2017-07-14 12:33
Additional information:

This suggestion will make pyenv much better. 

To emphasize the need, there are many places that due to security you cannot build virtualenv.  Rather you have to push a virtualenv with your python code. Often enough, you also don't have control on where the code will be stationed on the installation server.  Hence the mobility of virtualenv is important for Python code to be migratable.  Many times I find myself and other edit activate shell scripts correcting their path to a relative one.

In fact, virtualenv has --relocatable flag but it doesn't do anything to the batch and shell activate files.  Python programs in Scripts, however, are already built with a relative path.  There is no reason not to do that to the shell and batch programs.

The only thing is that there will need to be more particular shell activate scripts (tcsh, ksh, csh, etc) as they may have different characteristics for relativity.

Progress is not defined by how things are, but how they can be.
msg298377 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2017-07-14 23:14
This seems reasonable, but scripts written to venvs by e.g. pip hard-code the path to the venv, and this is a stumbling block to relocatability. There is no reason to use the activate scripts except under interactive use - a script installed in a venv should be directly runnable from its bin directory, but that currently won't work if the venv is moved to a new location.

Outside of Python I maintain the distlib library which pip uses under the covers to write installed scripts, and I will try to consider a solution which solves the wider problem.
msg298382 - (view) Author: Arnon Sela (PyAcrisel) Date: 2017-07-15 04:28
Thank you so much for considering this.

Both windows and linux based systems provides mechanisms to be dynamic and
relative to the path.

I wish there would be a way to add such behavior to Python development
manifesto and to check in pypi that packages are aligned

*Arnon Sela*
214-205-2151 <%2B1-214-205-2151>
arnon@acrisel.com
www.acrisel.com

IT Solutions and Ab Initio Experts
--
Notice: from Acrisel: If received in error, please destroy and notify
sender, and make no further use, disclosure, or distribution. This email
(including attachments) may contain information subject to confidentiality
obligations, and sender does not waive confidentiality or privilege.

On Fri, Jul 14, 2017 at 6:14 PM, Vinay Sajip <report@bugs.python.org> wrote:

>
> Vinay Sajip added the comment:
>
> This seems reasonable, but scripts written to venvs by e.g. pip hard-code
> the path to the venv, and this is a stumbling block to relocatability.
> There is no reason to use the activate scripts except under interactive use
> - a script installed in a venv should be directly runnable from its bin
> directory, but that currently won't work if the venv is moved to a new
> location.
>
> Outside of Python I maintain the distlib library which pip uses under the
> covers to write installed scripts, and I will try to consider a solution
> which solves the wider problem.
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue30842>
> _______________________________________
>
msg299463 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2017-07-29 10:31
This needs further thought as to how to implement. For example, readlink -f isn't supported on OS X. If a patch were available which works across platforms, that would potentially speed up resolution of this issue.
msg299466 - (view) Author: Arnon Sela (PyAcrisel) Date: 2017-07-29 13:26
readlink -f can be replaced by the following sequence:

    pushd $fullpath
    fullpath=$(pwd -P)
    popd

Please note that:

    [[ $called != $0 ]] && fullpath="${BASH_SOURCE[@]}" || fullpath=$0

Should be replaced with:

    [[ $called != $0 ]] && fullpath="${BASH_SOURCE[0]}" || fullpath=$0

0 instead of @ - that was a misspelling.
msg299513 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2017-07-30 19:24
Note that the activate script currently also works with /bin/sh (using . venv-dir/bin/activate) but pushd and popd are not supported there, and introducing them would presumably break this script on /bin/sh.
msg299527 - (view) Author: Arnon Sela (PyAcrisel) Date: 2017-07-30 22:02
The procedure:

    pushd $fullpath
    fullpath=$(pwd -P)
    popd

Can be made sh (and other shells) friendly by:
    
    here=$PWD # or $(pwd)
    cd $fullpath
    fullpath=$(pwd -P)
    cd $here

More to write, but should work, right?
msg376854 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2020-09-13 20:56
If no PR is forthcoming for this, I'd like to close this issue as out of date. Any objections?
History
Date User Action Args
2022-04-11 14:58:48adminsetgithub: 75025
2020-09-13 20:56:35vinay.sajipsetmessages: + msg376854
2017-08-01 01:25:12vinay.sajipsetstage: needs patch
2017-07-30 22:02:07PyAcriselsetmessages: + msg299527
2017-07-30 19:24:49vinay.sajipsetmessages: + msg299513
2017-07-29 13:26:02PyAcriselsetmessages: + msg299466
2017-07-29 10:31:55vinay.sajipsetmessages: + msg299463
2017-07-15 04:28:48PyAcriselsetmessages: + msg298382
2017-07-14 23:14:10vinay.sajipsetmessages: + msg298377
2017-07-14 12:33:52PyAcriselsetmessages: + msg298351
versions: + Python 3.7, - Python 3.5
2017-07-03 22:07:36ned.deilysetnosy: + vinay.sajip
2017-07-03 19:36:47PyAcriselcreate