我系统中的默认python版本是2.6.6。我安装了virtualenv,每当我打开终端时,我都希望默认的virtualenv为2.7。
所以,我在〜/ .bashrc文件中添加了以下命令:
source $HOME/virtualenvs/py2.7/bin/activate
现在每当我通过点击Gnome环境中的图标启动终端(即,我已经登录到机器并在Gnome中打开一个新的终端窗口(xterm)),shell符号如下所示:
(py2.7)(py2.7)
看起来好像我在另一个virtualenv中有一个virtualenv。更糟糕的是,我只能停用一个virtualenv而不是另一个,如下所示:
(py2.7)(py2.7)deactivate
(py2.7)python
Python 2.7.5 (default, Jun 28 2013, 14:53:08)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(py2.7)deactivate
bash: deactivate: command not found
(py2.7)python
Python 2.7.5 (default, Jun 28 2013, 14:53:08)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
正如你所看到的,虽然我系统中的默认python是2.6,但我仍然坚持使用virtualenv(2.7)
如果我通过Ctrl + Alt + F2切换到文本虚拟控制台并登录,则看起来很正常。
(py2.7)[username@host ~]$
我可以停用并返回系统的默认python 2.6。
(py2.7)[username@host ~]$ python
Python 2.7.5 (default, Jun 28 2013, 14:53:08)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(py2.7)[username@host ~]$ deactivate
[username@host ~]$ python
Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
有什么问题?每当我在Gnome中打开终端时,是否可以将默认的virtualenv设置为2.7?
我的Linux发行版是RedHat 6。
答案 0 :(得分:5)
这个怎么样?要测试你是否已经在virtualenv中:)
test -z "$VIRTUAL_ENV" && source $HOME/virtualenvs/py2.7/bin/activate
答案 1 :(得分:3)
沃尔夫的回答对我不起作用。相反,我在.bashrc中使用了以下测试:
if (tty -s); then
source /pathto/virtualenvs/py2.7/bin/activate
fi
如果标准输入是终端,则修改:如果您现在“屏幕”,您将再次获得 (py2.7)(py2.7)user@computer] $
答案 2 :(得分:2)
我建议使用非常方便的autoenv。
答案 3 :(得分:2)
我使用virtualenvwrapper然后类似于Godrebh's approach,我只是在我的// Events
$stmt = $db->query("
SELECT id, columnX, created
FROM table1
");
$row_count = $stmt->rowCount();
if ($row_count != '0') {
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$event = $xml->createElement("event");
$events->appendChild($event);
$event->appendChild($xml->createElement("ID", "XXXX"));
$event->appendChild($xml->createElement("columnX", $row['columnX']));
$event->appendChild($xml->createElement("created", $row['created']));
}
}
// Other events
$stmt = $db->query("
SELECT id, columnY1, columnY2, columnY3, created
FROM table2
");
$row_count = $stmt->rowCount();
if ($row_count != '0') {
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$event = $xml->createElement("event");
$events->appendChild($event);
$event->appendChild($xml->createElement("ID", "XXXX"));
$event->appendChild($xml->createElement("columnY1", $row['columnY1']));
$event->appendChild($xml->createElement("columnY2", $row['columnY2']));
$event->appendChild($xml->createElement("columnY3", $row['columnY3']));
$event->appendChild($xml->createElement("created", $row['created']));
}
}
(或其他登录脚本)中调用我首选的默认virtualenv。
.bashrc