Running non-system Python with virtualenv in 32bit mode on OS X
简短问题使用virtualenv/virtualenvwrapper是否可以向链接到特定虚拟环境的
背景我希望有多个使用BREW安装的python 2.7的虚拟环境,但有些是在64位模式下运行的,另一些是在32位模式下运行的。
下面是我的OS X开发的典型设置。我想添加到
编辑为了更详细地说明我使用简单别名时遇到的问题,可能会有超过1个32位的虚拟环境。也就是说,对
python安装:
1 | > brew install python --framework --universal |
创建虚拟环境(安装pip、virtualenv和virtualenvwrapper后):
1 2 3 4 5 | > 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位使用:
1 2 3 4 5 | > workon env_1_64 > python myscript.py > workon env_2_64 > python my_other_project_script.py |
32位用法:(当前/非理想)
1 2 3 4 5 | > workon env_1_32 > arch -i386 python myscript.py > workon env_2_32 > arch -i386 python my_other_project_script.py |
32位用法:(理想)
1 2 | > workon env_1_32 > python my_32bit_project.py # Note that the arch -i386 would be transparent |
解决方案听肖恩的评论:
我在激活/停用中添加了一个别名,用于我想要以32位运行的环境。详情请参见下文。
env_1_32:激活脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | # 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 |
在激活脚本中添加别名,并激活您想要使用的每种类型的virtualenv。
1 2 3 4 | $ cd env32 $ echo"alias python='arch -i386 python'">> bin/activate $ source bin/activate $ python myscript.py |