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

Dynamically generate VIRTUAL_ENV environment variable #1166

Closed
hsiaoyi0504 opened this issue Apr 19, 2018 · 3 comments
Closed

Dynamically generate VIRTUAL_ENV environment variable #1166

hsiaoyi0504 opened this issue Apr 19, 2018 · 3 comments

Comments

@hsiaoyi0504
Copy link

hsiaoyi0504 commented Apr 19, 2018

Currently, the VIRTUAL_ENV environment variable in activate script will be generated when virtual environment is created.
However, if we change the name of the folder. It would not work.

Isn't it better to generate VIRTUAL_ENV variable dynamically using the path of the activate script ?
Maybe I am wrong, but based on my understanding, the activate script is a bash script and we can get the path of this script using mechanism similar to DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )".

@hsiaoyi0504 hsiaoyi0504 changed the title Dynamically generate VIRTUAL_ENV variable Dynamically generate VIRTUAL_ENV environment variable Apr 19, 2018
@pfmoore pfmoore mentioned this issue Sep 29, 2018
@asottile
Copy link
Contributor

moving a virtualenv is generally not a supported operation as the shebangs (#!...) of installed console_scripts contain full absolute paths. (though you can use something like virtualenv-tools3 to relocate a venv ~safely)

that said, I had a usecase for almost exactly what's desired here as I'm managing virtualenvs inside ~/.cache/... and then symlinking them to ./venv (sorry, closed source tool) and wanted which python to resolve as ./venv/bin/python instead of ~/.cache/ugly long path/bin/python

I used this chunk of code to accomplish what I wanted:

        def _fixup_activate_file(filename: str, replacement: str) -> None:
            dest = os.path.join(target, 'bin', filename)
            backup = f'{dest}.old'
            os.rename(dest, backup)

            with open(backup) as f:
                contents = f.read()
            with open(dest, 'w') as f:
                f.write(contents.replace(f'"{target}"', replacement))

        # make relocatable activate files
        _fixup_activate_file(
            'activate',
            # bash / zsh (sh not supported, is this ok?)
            '"$(cd "$(dirname "$(dirname "${BASH_SOURCE:0}")")"; pwd)"',
        )
        _fixup_activate_file(
            'activate.fish',
            "(bash -c 'cd '(dirname (dirname (status --current-filename)))'; pwd')",  # noqa: E501
        )

note that this doesn't work for sh, but I only needed to support bash / zsh / fish

@stale
Copy link

stale bot commented Apr 16, 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 Apr 16, 2019
@gaborbernat gaborbernat added this to the 20.1.0 milestone Jan 2, 2020
@gaborbernat
Copy link
Contributor

We no longer support the relocate-able flag, so this is now out of scope too.

@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.
Projects
None yet
Development

No branches or pull requests

3 participants