1

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, e.g.

import gtk

This works globally (i.e. in Python 2.6), but not in my virtual environment (i.e. 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?

pfnuesel
  • 14,093
  • 14
  • 58
  • 71
  • 2
    Possible duplicate of [trying to import a module: undefined symbol: PyUnicodeUCS4\_DecodeUTF8](http://stackoverflow.com/questions/16871799/trying-to-import-a-module-undefined-symbol-pyunicodeucs4-decodeutf8) – snakecharmerb Apr 17 '16 at 06:33
  • 1
    @snakecharmerb not that. And pfnuesel: No, you cannot use Python 2.6 C extensions in Python 2.7 whatsoever. Instead: consider upgrading your operating system so that you get 2.7 *or* even better a decent Python 3. – Antti Haapala -- Слава Україні Apr 17 '16 at 07:52
  • @AnttiHaapala Yes, that makes sense to me. Upgrading, while good advice, is not possible unfortunately, since this is our server operating system which runs on several thousand servers. – pfnuesel Apr 17 '16 at 14:17
  • @snakecharmerb See my edit. – pfnuesel Apr 17 '16 at 14:22

1 Answers1

1

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)
kdopen
  • 8,032
  • 7
  • 44
  • 52
  • I understand all of that. My question is how to install `gtk` for an interpreter which is newer than the one installed on the system. – pfnuesel Apr 17 '16 at 14:15
  • @pfnuesel, you use pip from/in the activated virtualenv. – rkersh Apr 17 '16 at 14:27
  • So, you just wanted to know which package to install? Did you try `pip install PyGTK`? – kdopen Apr 17 '16 at 19:54
  • @kdopen Yes. `pip install PyGTK` gives me `Could not find a version that satisfies the requirement PyGTK (from versions: ) No matching distribution found for PyGTK`. – pfnuesel Apr 18 '16 at 00:40