简短的问题
使用 virtualenv / virtualenvwrapper 是否可以添加前缀python
调用链接到特定的虚拟环境?
背景
我想使用多个虚拟环境已安装酿造 https://github.com/mxcl/homebrewPython 2.7,但有些在 64 位模式下运行,其他则在 32 位模式下运行。
下面是我的 OS X 开发的典型设置。我想添加到的具体前缀python
呼叫是arch -i386
强制 python 以 32 位模式运行。同样,最重要的部分是它将被添加only打电话后workon env32
(如示例所示)。我知道我可以在 .bash_profile 中设置别名,但是每次创建/删除虚拟环境时都必须修改它。
EDIT
为了详细说明我使用简单别名所遇到的问题,可能有超过 1 个 32 位虚拟环境。话虽如此,调用workon
理想情况下将前缀添加到python
调用,因此终端的工作流程将是相同的。调用后的含义workon env_x_32
我就可以使用python
和arch -i386
使用终端时对我来说是透明的。
Python安装:
> brew install python --framework --universal
创建虚拟环境(安装 pip、virtualenv 和 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位使用:
> workon env_1_64
> python myscript.py
> workon env_2_64
> python my_other_project_script.py
32 位使用:(当前/非理想)
> workon env_1_32
> arch -i386 python myscript.py
> workon env_2_32
> arch -i386 python my_other_project_script.py
32 位使用:(理想)
> workon env_1_32
> python my_32bit_project.py # Note that the arch -i386 would be transparent
Solution
跑步与Sean https://stackoverflow.com/users/304379/sean的评论:
我在激活/停用中添加了一个别名,用于我想要作为 32 位运行的环境。请参阅下文了解更多详细信息。
env_1_32:激活脚本
# 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