简体   繁体   中英

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. Now I want to use a module, which needs Python 2.7. I installed Python 2.7, but it would break other applications. So I set up a virtual environment with Python 2.7 as interpreter:

virtualenv -p ~/pkg/bin/python2.7 venv

If I activate the virtual environment and run python the new interpreter is used. Good! Now I need to import modules, eg

import gtk

This works globally (ie in Python 2.6), but not in my virtual environment (ie in Python 2.7). I tried to set the sys.path the same for the virtual environment, but this would give me errors such as

ImportError: /usr/lib64/python2.6/site-packages/gtk-2.0/glib/_glib.so: undefined symbol: 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. However, this pulls in other dependencies and going down that hole for about 7 or 8 steps, I resigned.

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 )

When you create a virtual environment, it basically acts as a separate standalone installation of 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.

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

$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python

But I have many pythons installed:

$ 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

$ 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM