“蟒蛇"virtualenv激活后仍然运行系统版本 [英] "python" still runs the system version after virtualenv activate

查看:23
本文介绍了“蟒蛇"virtualenv激活后仍然运行系统版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这些命令安装了 Python 2.7

I installed Python 2.7 with these commands

./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall

然后我创建了 teh virtualenv 指向 2.7 安装

Then I created teh virtualenv to point to the 2.7 installation

$ virtualenv --python=/usr/local/bin/python2.7 testbox
Already using interpreter /usr/local/bin/python2.7
New python executable in /var/python_venv/testbox/bin/python2.7
Also creating executable in /var/python_venv/testbox/bin/python
Installing setuptools, pip, wheel...done.
$ source testbox/bin/activate
(testbox) $ python
Python 2.6.6 ( , Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

但是如果我激活它,它指向 2.6.6

but if I activate it its point to 2.6.6

cat /etc/redhat-release
CentOS release 6.6 (Final)

我不确定为什么这不起作用,但我仍然可以在我的 apache DJANGO 应用程序中使用 virtualenv,所以我不太担心

I am not sure why this doesnt work, but I can still use the virtualenv in my apache DJANGO app so I am not too concerned

推荐答案

底线:

您已将python"设置为 shell 别名(可能在您的 shell 启动脚本中).它会干扰 virtualenv 替换键入python"时运行的内容的工作.删除别名,你就好了.

You have set "python" as a shell alias (probably in your shell startup scripts). It interferes with virtualenv's work of replacing what would be run when you type "python". Remove the alias, and you're good.

您也不需要指定 --python=/usr/local/bin/python2.7 '因为您正在使用 Python 安装中的 virtualenv,所以它已经默认使用了.

You also don't need to specify --python=/usr/local/bin/python2.7 'cuz you're using virtualenv from that Python installation, so it already uses it by default.

带有 virtualenv 1.10.1 的 WFM:(请参阅下面的进一步猜测)

WFM with virtualenv 1.10.1: (see a guess further below)

$ virtualenv --python=/usr/local/bin/python2.7 testbox
Running virtualenv with interpreter /usr/local/bin/python2.7
New python executable in testbox/bin/python2.7
Also creating executable in testbox/bin/python
Installing Setuptools.........................................done.
Installing Pip................................................done.
$ ls -l testbox/bin/
total 40
-rw-r--r--. 1 root root 2194 Dec  7 03:06 activate
-rw-r--r--. 1 root root 1250 Dec  7 03:06 activate.csh
-rw-r--r--. 1 root root 2389 Dec  7 03:06 activate.fish
-rw-r--r--. 1 root root 1129 Dec  7 03:06 activate_this.py
-rwxr-xr-x. 1 root root  332 Dec  7 03:06 easy_install
-rwxr-xr-x. 1 root root  340 Dec  7 03:06 easy_install-2.7
-rwxr-xr-x. 1 root root  293 Dec  7 03:06 pip
-rwxr-xr-x. 1 root root  301 Dec  7 03:06 pip-2.7
lrwxrwxrwx. 1 root root    9 Dec  7 03:06 python -> python2.7
lrwxrwxrwx. 1 root root    9 Dec  7 03:06 python2 -> python2.7
-rwxr-xr-x. 1 root root 7788 Dec  7 03:06 python2.7

activate 的主要作用是:

PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH

<小时>

我的猜测是您正在使用为您的 /usr/local/bin/python2.7 安装的 virtualenv .已经使用..."消息的原因.如果是这种情况,您不需要传递 --python 因为 virtualenv 默认已经在使用它(检查它的 shebang).


My guess is that you're using virtualenv that was installed for your /usr/local/bin/python2.7. That's the reason for the "Already using..." message. If that's the case, you don't need to pass --python because that virtualenv is already using it by default (check its shebang).

仍然,由于 virtualenv 创建了一个无版本的可执行文件并且 activate 改变了 PATH,你应该得到 /var/python_venv/testbox/bin/python 作为 python.

Still, since virtualenv creates a versionless executable and activate alters PATH, you should get /var/python_venv/testbox/bin/python as python.

  • 因为 python 在你的情况下是一个别名,并且 activate 不使用别名 - 你必须在你的 bash 启动脚本.
  • Since python is an alias in your case, and activate doesn't use aliases - you must have it set in your bash startup scripts.

这篇关于“蟒蛇"virtualenv激活后仍然运行系统版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆