Running non-system Python with virtualenv in 32bit mode on OS X
Asked Answered
D

1

11

Short Question
Using virtualenv / virtualenvwrapper is it possible to add a prefix to the python call that linked to a specific virtual environment?

Background
I would like to have multiple virtual environment using a brew installed Python 2.7, but some running in 64 bit mode and the others 32bit mode.

Below I have the typical setup for my OS X development. The specific prefix I would like to add to the python call is the arch -i386 to force python to run as 32 bit mode. Again the most important part of it is that it would be added only after calling workon env32 (as shown the example). I know I can setup an alias in my .bash_profile, but this would have to be modified everytime I create / remove virtual environments.

EDIT
To elaborate more on the issues I have with using a simple alias, there could be more than 1 32bit virtual environment. This being said, the call to workon would ideally add the prefix to python call so the workflow at the terminal would be the same. Meaning after calling workon env_x_32 I would be able to just use python and the arch -i386 would be transparent to me when using Terminal.

Python Installation:

> brew install python --framework --universal

Creating Virtual Environments(after installing pip, virtualenv and virtualenvwrapper):

> mkvirtualenv env_1_64 --no-site-packages
> mkvirtualenv env_1_32 --no-site-packages

> mkvirtualenv env_2_64 --no-site-packages
> mkvirtualenv env_2_32 --no-site-packages

64 bit usage:

> workon env_1_64
> python myscript.py

> workon env_2_64
> python my_other_project_script.py

32 bit usage:(Current / Non-Ideal)

> workon env_1_32
> arch -i386 python myscript.py  

> workon env_2_32
> arch -i386 python my_other_project_script.py

32 bit usage: (Ideal)

> workon env_1_32
> python my_32bit_project.py # Note that the arch -i386 would be transparent

Solution
Running with Sean's comments:

I added an alias inside the activate / deactivate for the environments I wanted to run as 32bit. See below for more detail.

env_1_32: activate script

# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly

deactivate () {
    alias python='python' # <---- Added this line

    # reset old environment variables
    if [ -n "$_OLD_VIRTUAL_PATH" ] ; then
        PATH="$_OLD_VIRTUAL_PATH"
        export PATH
        unset _OLD_VIRTUAL_PATH
    fi

    # ****** Removed Content to keep the post shorter*********

}

# unset irrelavent variables
deactivate nondestructive

VIRTUAL_ENV="/Users/Adam/.envs/env_1_32"
export VIRTUAL_ENV

# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands.  Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then
    hash -r
fi

# ****** Removed Content to keep the post shorter*********

alias python='arch -i386 python' # <---- Added this line to run as 32bit
Demetria answered 5/10, 2011 at 15:34 Comment(1)
Yesterday this question (and answer) was very useful to me, thanks! However, today I'm stuck again, because my project(s) have no top level 'main.py' script, instead one is generated by setuptools(?) when I do a 'setup.py install' or 'setup.py develop', using the 'entry_points' config in my setup.py. This means that there's no place for me to insert my the 'arch' alias when it's deployed on a user's machine. I may ask a separate question about this.Bursiform
E
6

Adding an alias to your activate script, and activating your virtualenv each type you want to use it.

$ cd env32
$ echo "alias python='arch -i386 python'" >> bin/activate
$ source bin/activate
$ python myscript.py
Euchre answered 5/10, 2011 at 17:9 Comment(4)
I always suggest using the virtualenv option --no-site-packages when create your virtualenvs.Euchre
In general I do. This exact instance I am using wxPython (installed to the brewed version) so I actually need its site packages.Demetria
Adam - I suggest you could edit the activate script for each virtualenv, that manually adds an alias to python.Euchre
Actually it changes the PATH variable in order to select which python to use, not create an alias. I am however looking into modifying the activate script...Demetria

© 2022 - 2024 — McMap. All rights reserved.