问题
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
回答1:
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
来源:https://stackoverflow.com/questions/7663852/running-non-system-python-with-virtualenv-in-32bit-mode-on-os-x