问题描述
简短问题
使用virtualenv/virtualenvwrapper是否可以在链接到特定虚拟环境的python
调用中添加前缀?
Short Question
Using virtualenv / virtualenvwrapper is it possible to add a prefix to the python
call that linked to a specific virtual environment?
背景
我想使用安装了浏览器 Python 2.7的多个虚拟环境,但有些运行在64位模式下以及其他32位模式.
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.
以下是我的OS X开发的典型设置.我想添加到python
调用中的特定前缀是arch -i386
,以强制python以32位模式运行.同样,它最重要的部分是在调用workon env32
之后仅添加 (如示例所示).我知道我可以在.bash_profile中设置别名,但是每次创建/删除虚拟环境时都必须对其进行修改.
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.
编辑
为了详细说明我使用简单别名的问题,可能会有1个以上的32位虚拟环境.话虽这么说,对workon
的调用将理想地在python
调用中添加前缀,因此终端的工作流程将是相同的.意思是在调用workon env_x_32
之后,我可以只使用python
,而arch -i386
在使用Terminal时对我来说是透明的.
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安装:
> 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 的评论运行:
Solution
Running with Sean's comments:
我在我想以32位运行的环境的激活/停用中添加了一个别名.有关更多详细信息,请参见下文.
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:激活脚本
# 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.
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
这篇关于在OS X上以32位模式运行带有virtualenv的非系统Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!