[英]virtualenv with python interpreter newer than systemwide interpreter
EDIT: If you vote this question to be a duplicate, please do at least take the time to read the question instead of just flagging it a duplicate because it looks somewhat similar to another question.编辑:如果您认为这个问题是重复的,请至少花时间阅读该问题,而不是仅仅将其标记为重复,因为它看起来与另一个问题有些相似。 If you would have done that, you would immediately realize that it is not a duplicate.如果您这样做了,您会立即意识到它不是重复的。 I'm merely trying to show some wider context.我只是想展示一些更广泛的背景。
My distro still uses Python 2.6 as python interpreter.我的发行版仍然使用 Python 2.6 作为 Python 解释器。 Now I want to use a module, which needs Python 2.7.现在我想使用一个需要 Python 2.7 的模块。 I installed Python 2.7, but it would break other applications.我安装了 Python 2.7,但它会破坏其他应用程序。 So I set up a virtual environment with Python 2.7 as interpreter:所以我建立了一个使用 Python 2.7 作为解释器的虚拟环境:
virtualenv -p ~/pkg/bin/python2.7 venv
If I activate the virtual environment and run python the new interpreter is used.如果我激活虚拟环境并运行 python,则会使用新的解释器。 Good!好的! Now I need to import
modules, eg现在我需要import
模块,例如
import gtk
This works globally (ie in Python 2.6), but not in my virtual environment (ie in Python 2.7).这适用于全局(即在 Python 2.6 中),但不适用于我的虚拟环境(即在 Python 2.7 中)。 I tried to set the sys.path
the same for the virtual environment, but this would give me errors such as我试图为虚拟环境设置相同的sys.path
,但这会给我带来错误,例如
ImportError: /usr/lib64/python2.6/site-packages/gtk-2.0/glib/_glib.so: undefined symbol: PyUnicodeUCS4_DecodeUTF8导入错误:/usr/lib64/python2.6/site-packages/gtk-2.0/glib/_glib.so:未定义符号:PyUnicodeUCS4_DecodeUTF8
which is somewhat expected.这有点在意料之中。
A search with搜索与
pip search gtk
would not lead any results either.也不会导致任何结果。 My best guess right now is that I have to install gtk from source, and compile it against Python 2.7.我现在最好的猜测是我必须从源代码安装 gtk,并针对 Python 2.7 编译它。 However, this pulls in other dependencies and going down that hole for about 7 or 8 steps, I resigned.但是,这会引入其他依赖项,并在大约 7 或 8 个步骤中进入该漏洞,我辞职了。
Is there an easier way to solve this issue?有没有更简单的方法来解决这个问题?
After you first activate your virtual environment, you need to install any packages you require (for example, pip install pep8-naming
)首次激活虚拟环境后,您需要安装所需的任何软件包(例如pip install pep8-naming
)
When you create a virtual environment, it basically acts as a separate standalone installation of python.当你创建一个虚拟环境时,它基本上作为一个单独的 python 独立安装。 If you previously installed gtk
(or any other package) in the global Python 2.6 instance, that is not available within your Python 2.7 virtual environment.如果您之前在全局 Python 2.6 实例中安装了gtk
(或任何其他包),则在您的 Python 2.7 虚拟环境中不可用。
Instead, you need to re-install any needed packages after switching to the environment.相反,您需要在切换到环境后重新安装任何需要的包。
For example, on my Mac, the default system python is 2.7例如,在我的 Mac 上,默认系统 python 是 2.7
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
But I have many pythons installed:但是我安装了很多python:
$ python <tab><tab>
python python2.7-32 python3.2m-config python3.4m pythonw2.7-32
python-32 python2.7-config python3.3 python3.4m-config pythonw3
python-config python3 python3.3-32 pythontex pythonw3-32
python2 python3-32 python3.3-config pythonw pythonw3.2
python2-32 python3-config python3.3m pythonw-32 pythonw3.2-32
python2-config python3.2 python3.3m-config pythonw2 pythonw3.3
python2.6 python3.2-32 python3.4 pythonw2-32 pythonw3.3-32
python2.6-config python3.2-config python3.4-32 pythonw2.6
python2.7 python3.2m python3.4-config pythonw2.7
If I create a new virtualenv, you can see it has only the minimum packages installed如果我创建一个新的 virtualenv,你可以看到它只安装了最少的包
$ mkvirtualenv -p `which python3.4` test
Running virtualenv with interpreter /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.4'
New python executable in test/bin/python3.4
Also creating executable in test/bin/python
Installing setuptools, pip, wheel...pdone.
$ pip list
pip (7.1.2)
setuptools (18.2)
wheel (0.24.0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.