Navigation Menu

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

OSX virtualenv fixing by creating a simple alias #5516

Merged
merged 4 commits into from Dec 7, 2015

Conversation

nils-werner
Copy link
Contributor

Added simple solution of creating frameworkpython alias. to OSX virtualenv fixing.

The alias does exactly what the script does: it runs python with a variable PYTHONHOME set. The script is a bit overkill because virtualenv source bin/activate puts the root dir of the virtualenv already in a variable $VIRTUAL_ENV. So picking this one up and running the system wide Python interpreter can be easily done in a one line alias.

@tacaswell tacaswell added this to the next major release (2.0) milestone Nov 19, 2015
@tacaswell
Copy link
Member

attn @jenshnielsen @mdehoon

@jenshnielsen
Copy link
Member

That is a nice solution. The only issue that I have is that calling frameworkpython outside a virtualenv will result in errors like no module named site in python 2 and a segfault in python 3. I wonder if there is a better way of solving this.

@nils-werner
Copy link
Contributor Author

I have pushed another change that first checks if $VIRTUAL_ENV is actually set before trying to run Python. If it isn't set it will simply run Python without setting $PYTHONHOME.

@jenshnielsen
Copy link
Member

Unfortunately that does not allow me to pass commandline arguments to the alias inside the virtualenv (it does work outside) I suggest something like:

alias frameworkpython2='[[ -z "$VIRTUAL_ENV" ]] && echo "Not running in a virtualenv use regular python" || PYTHONHOME=$VIRTUAL_ENV /usr/local/bin/python/'

Which does allow passing of cmd arguments. What do you think?

@tacaswell What do you think about backporting this to the 1.5.0-docs branch. This is a much better solution than what is in there now.

@nils-werner
Copy link
Contributor Author

I have updated it again to use a proper bash function. It's much more
readable and works properly now.
Am 21.11.2015 9:55 vorm. schrieb "Jens Hedegaard Nielsen" <
notifications@github.com>:

Unfortunately that does not allow me to pass commandline arguments to the
alias inside the virtualenv (it does work outside) I suggest something like:

alias frameworkpython2='[[ -z "$VIRTUAL_ENV" ]] && echo "Not running in a virtualenv use regular python" || PYTHONHOME=$VIRTUAL_ENV /usr/local/bin/python/'

Which does allow passing of cmd arguments. What do you think?

@tacaswell https://github.com/tacaswell What do you think about
backporting this to the 1.5.0-docs branch. This is a much better solution
than what is in there now.


Reply to this email directly or view it on GitHub
#5516 (comment)
.

@jenshnielsen
Copy link
Member

Yes that is probably better. The only thing missing for me it the ability to test if we are running python3 or 2. Perhaps this is being to clever but the following works for me.

function frameworkpython {
    if [[ ! -z "$VIRTUAL_ENV" ]]; then
        #test if we are running python 2 or 3
        pyversion=`python -V 2>&1 | cut -d ' '  -f2 | cut -d. -f1`
        PYTHONHOME=$VIRTUAL_ENV /usr/local/bin/python$pyversion "$@"
    else
        /usr/local/bin/python "$@"
    fi
}

I use virtualenvs with both python 2 and 3. This will naturally fail if you are using more than 1 python 3 version in your virtual env

@jenshnielsen
Copy link
Member

Actually we might as well be a bit more clever:

function frameworkpython {
    if [[ ! -z "$VIRTUAL_ENV" ]]; then
        #test if we are running python 2 or 3
        pyversion=`python -V 2>&1 | cut -d ' '  -f2 | cut -d. -f1,f2`
        PYTHONHOME=$VIRTUAL_ENV /usr/local/bin/python$pyversion "$@"
    else
        /usr/local/bin/python "$@"
    fi
 }

@jenshnielsen
Copy link
Member

We should probably also note that users might have to modify /usr/local/bin depending on where their python is installed.

We might as well also modify the script above to use $VIRTUAL_ENV rather than the python -c import os ... thing.

@tacaswell
Copy link
Member

+1 on back porting to 1.5.0-doc

On Sat, Nov 21, 2015, 05:05 Jens Hedegaard Nielsen notifications@github.com
wrote:

We should probably also note that users might have to modify
/usr/local/bin depending on where their python is installed.

We might as well also modify the script above to use $VIRTUAL_ENV rather
than the python -c import os ... thing.


Reply to this email directly or view it on GitHub
#5516 (comment)
.

@nils-werner
Copy link
Contributor Author

We can make it even more clever:

export GLOBALPYTHON=$(which python)

function frameworkpython {
    if [[ ! -z "$VIRTUAL_ENV" ]]; then
        #test if we are running python 2 or 3
        pyversion=`python -V 2>&1 | cut -d ' '  -f2 | cut -d. -f1,f2`
        PYTHONHOME=$VIRTUAL_ENV $GLOBALPYTHON$pyversion "$@"
    else
        $GLOBALPYTHON "$@"
    fi
 }

Needless to say this slowly stops being a simple, easy to understand solution :-)

@nils-werner
Copy link
Contributor Author

@jenshnielsen Running

python -V 2>&1 | cut -d ' '  -f2 | cut -d. -f1,f2

fails with cut: [-cf] list: illegal list value but

python -V 2>&1 | cut -d ' '  -f2 | cut -d. -f1 -f2

works for me. Is there a particular reason why you picked -f1,f2?

@jenshnielsen
Copy link
Member

Sorry that is a bug as I copied from my bash profile. I was using:

python -V 2>&1 | cut -d ' '  -f2 | cut -d. -f1,2

but

python -V 2>&1 | cut -d ' '  -f2 | cut -d. -f1 -f2

is equally good

jenshnielsen added a commit that referenced this pull request Dec 7, 2015
OSX virtualenv fixing by creating a simple alias
@jenshnielsen jenshnielsen merged commit 42ea00f into matplotlib:master Dec 7, 2015
@jenshnielsen
Copy link
Member

Merging this now. We can always make it more elaborate

jenshnielsen added a commit that referenced this pull request Dec 7, 2015
OSX virtualenv fixing by creating a simple alias
@jenshnielsen
Copy link
Member

backported to 1.5.0-doc as 6f0e6ad

jenshnielsen added a commit that referenced this pull request Dec 9, 2015
OSX virtualenv fixing by creating a simple alias
@QuLogic QuLogic modified the milestones: v1.5.1, 2.0 (style change major release) Oct 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants