在OS X上以32位模式使用virtualenv运行非系统Python

在OS X上以32位模式使用virtualenv运行非系统Python,python,macos,virtualenv,virtualenvwrapper,Python,Macos,Virtualenv,Virtualenvwrapper,简短问题 使用virtualenv/virtualenvwrapper是否可以为链接到特定虚拟环境的python调用添加前缀 背景 我希望使用Python2.7创建多个虚拟环境,但有些虚拟环境以64位模式运行,有些虚拟环境以32位模式运行 下面是我的OS X开发的典型设置。我想添加到python调用中的特定前缀是arch-i386,以强制python以32位模式运行。同样,它最重要的部分是,只有在调用workon env32之后才能添加它(如示例所示)。我知道我可以在.bash_配置文件中设置别

简短问题
使用virtualenv/virtualenvwrapper是否可以为链接到特定虚拟环境的
python
调用添加前缀

背景
我希望使用Python2.7创建多个虚拟环境,但有些虚拟环境以64位模式运行,有些虚拟环境以32位模式运行

下面是我的OS X开发的典型设置。我想添加到
python
调用中的特定前缀是
arch-i386
,以强制python以32位模式运行。同样,它最重要的部分是,只有在调用
workon env32
之后才能添加它(如示例所示)。我知道我可以在.bash_配置文件中设置别名,但每次创建/删除虚拟环境时都必须修改别名

编辑
为了详细说明我使用简单别名时遇到的问题,可能会有超过1个32位的虚拟环境。也就是说,对
workon
的调用理想情况下会将前缀添加到
python
调用中,这样终端上的工作流将是相同的。这意味着在调用
workon env_x_32
后,我将能够使用
python
,并且
arch-i386
在使用终端时对我是透明的

Python安装:

> brew install python --framework --universal
> workon env_1_32
> arch -i386 python myscript.py  

> workon env_2_32
> arch -i386 python my_other_project_script.py
> workon env_1_32
> python my_32bit_project.py # Note that the arch -i386 would be transparent
# 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
创建虚拟环境(安装pip、virtualenv和VirtualEnvrapper后):

> 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
> workon env_1_64
> python myscript.py

> workon env_2_64
> python my_other_project_script.py
64位用法:

> 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
> workon env_1_64
> python myscript.py

> workon env_2_64
> python my_other_project_script.py
32位用法:(当前/非理想)

> brew install python --framework --universal
> workon env_1_32
> arch -i386 python myscript.py  

> workon env_2_32
> arch -i386 python my_other_project_script.py
> workon env_1_32
> python my_32bit_project.py # Note that the arch -i386 would be transparent
# 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
32位用法:(理想)

> brew install python --framework --universal
> workon env_1_32
> arch -i386 python myscript.py  

> workon env_2_32
> arch -i386 python my_other_project_script.py
> workon env_1_32
> python my_32bit_project.py # Note that the arch -i386 would be transparent
# 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

解决方案
使用的注释运行:

我在activate/deactivate中添加了一个别名,用于我希望以32位运行的环境。详见下文

环境1\u 32:激活脚本

> brew install python --framework --universal
> workon env_1_32
> arch -i386 python myscript.py  

> workon env_2_32
> arch -i386 python my_other_project_script.py
> workon env_1_32
> python my_32bit_project.py # Note that the arch -i386 would be transparent
# 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
#此文件必须与bash中的“source bin/activate”一起使用*
#您不能直接运行它
停用(){

alias python='python'#将别名添加到激活脚本中,并激活您想要使用的每种类型的virtualenv

$ cd env32
$ echo "alias python='arch -i386 python'" >> bin/activate
$ source bin/activate
$ python myscript.py

我总是建议使用virtualenv选项——在创建virtualenvs时不要使用站点包。通常我会这样做。我使用的正是wxPython(安装到酝酿版本)因此,我实际上需要它的站点包。Adam-我建议您可以编辑每个virtualenv的激活脚本,手动为python添加别名。实际上,它更改PATH变量以选择要使用的python,而不是创建别名。不过,我正在研究修改激活脚本…昨天,这个问题(和答案)非常有用,谢谢!但是,今天我又被卡住了,因为我的项目没有顶级的“main.py”脚本,而是由setuptools(?)生成的当我使用setup.py中的“entry_points”配置执行“setup.py安装”或“setup.py开发”时。这意味着当“arch”别名部署到用户的计算机上时,我没有地方插入它。我可能会问一个单独的问题。