【问题标题】:Running non-system Python with virtualenv in 32bit mode on OS X在 OS X 上以 32 位模式使用 virtualenv 运行非系统 Python
【发布时间】:2011-12-01 14:34:13
【问题描述】:

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

背景
我想拥有多个使用brew installed Python 2.7 的虚拟环境,但一些运行在 64 位模式下,另一些运行在 32 位模式下。

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

编辑
为了详细说明我使用简单别名时遇到的问题,可能有超过 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

解决方案
Sean 的 cmets 一起运行:

我在激活/停用中为我想以 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

【问题讨论】:

  • 昨天这个问题(和答案)对我非常有用,谢谢!但是,今天我再次陷入困境,因为我的项目没有顶级“main.py”脚本,而是由 setuptools(?) 在我执行“setup.py install”或“setup.py”时生成的开发”,使用我的 setup.py 中的 'entry_points' 配置。这意味着当它部署在用户的机器上时,我没有地方可以插入我的“arch”别名。我可能会就此提出一个单独的问题。

标签: python macos virtualenv virtualenvwrapper


【解决方案1】:

为您的激活脚本添加一个别名,并激活您想要使用的每种类型的 virtualenv。

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

【讨论】:

  • 我总是建议在创建你的 virtualenvs 时使用 virtualenv 选项 --no-site-packages。
  • 一般来说我会。我正在使用 wxPython(安装到 brewed 版本)的这个确切实例,所以我实际上需要它的站点包。
  • Adam - 我建议你可以编辑每个 virtualenv 的激活脚本,手动为 python 添加一个别名。
  • 实际上它更改 PATH 变量是为了选择要使用的 python,而不是创建别名。但是,我正在考虑修改激活脚本...
猜你喜欢
  • 2013-04-09
  • 1970-01-01
  • 2012-06-12
  • 2014-01-27
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
相关资源
最近更新 更多