2

I’m totally confused with new python 2.7.10.

I've installed python 2.7.10 from tar.xz, which was downloaded from official site. Then I've linked /usr/local/bin/python2.7 with /usr/bin/python2.7 and /usr/bin/python, but when I try to import module, I get ImportError: No module named "module_name". For example:

python -c "import gtk"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named gtk

But if:

cd /usr/lib/python2.7/dist-packages/gtk-2.0/
python -c "import gtk"

We get:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "gtk/__init__.py", line 30, in <module>
    import gobject as _gobject
ImportError: No module named gobject

So, gtk module has been imported successfully.

Let's "cd ~" and look at sys.path:

python -c "import sys, pprint; pprint.pprint(sys.path)"
['',
 '/usr/local/lib/python2.7/site-packages/setuptools-17.1.1-py2.7.egg',
 '/usr/local/lib/python2.7/site-packages/pip-7.1.0.dev0-py2.7.egg',
 '/usr/local/lib/python27.zip',
 '/usr/local/lib/python2.7',
 '/usr/local/lib/python2.7/plat-linux2',
 '/usr/local/lib/python2.7/lib-tk',
 '/usr/local/lib/python2.7/lib-old',
 '/usr/local/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/site-packages']

Ok, there is no /usr/lib/python2.7/dist-packages, let's add it: export PYTHONPATH=$PYTHONPATH:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7

Now:

python -c "import sys, pprint; pprint.pprint(sys.path)"
['',
 '/usr/local/lib/python2.7/site-packages/setuptools-17.1.1-py2.7.egg',
 '/usr/local/lib/python2.7/site-packages/pip-7.1.0.dev0-py2.7.egg',
 '/home/s-quark',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7',
 '/usr/local/lib/python27.zip',
 '/usr/local/lib/python2.7',
 '/usr/local/lib/python2.7/plat-linux2',
 '/usr/local/lib/python2.7/lib-tk',
 '/usr/local/lib/python2.7/lib-old',
 '/usr/local/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/site-packages']

But it's still could not find module:

python -c "import gtk"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named gtk

What I have to do?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
NULL
  • 191
  • 1
  • 3
  • 13
  • It appears you are missing the `gtk-2.0` path in all of your lists, just because you have `dist-packages` doesn't mean you have the subdirectories :) Regardless, I would recommend starting over and using a packaging manager to handle the installing of Python and the libraries. – Wolph Jun 21 '15 at 10:29
  • Have you seen this http://stackoverflow.com/questions/11699675/how-to-install-gtk-in-python2-7 – ilciavo Jun 21 '15 at 10:55
  • @ilciavo, yes, I have. @Wolph, @ilciavo, I have problem with this too. When I try: `sudo pip install PyGTK` I get: `No matching distribution found for PyGTK` Then when I try something with gtk in dependencies: `sudo pip install pygtk-shell` I get: ` import gtk ImportError: No module named gtk ---------------------------------------- Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-y3ju0c/pygtk-shell` – NULL Jun 21 '15 at 11:54
  • @NULL It seems you have different python versions living in your system. What do you get after `which -a python`? They don't recommend doing `sudo pip install` instead create a virtual environment instead. After that use `python -m pip install` to make sure you use the virtual environment. Generally stay away from `sudo` – ilciavo Jun 21 '15 at 15:19
  • @NULL Additionally, if you install a new python version use a different alias to keep versions separately and clean like `alias pythonA='$HOME/anaconda/bin/python` in your `~/.bashrc`. Then you can install everything with `pythonA -m pip install` and avoid messing up your system. – ilciavo Jun 21 '15 at 15:24
  • @ilciavo, the result of `which -a python` is `/usr/local/bin/python /usr/bin/python`. Thank you for the tips, will be careful next time =) – NULL Jun 21 '15 at 16:28

1 Answers1

2

It seems that you have two python versions.

The default version generally is linked to:

/usr/bin/python 

As you can see from which -a python, your local version lives inside:

/usr/local/bin/python 

this is linked to your local binary with

/usr/bin/python 

pip install installs packages using the default python. This is the reason you can't import the packages from your local installation even if the package is visible in your $PYTHONPATH.

In case python -m pip install doesn't install the desired packages on your local version try creating a virtual environment (Do not use sudo)

In case everything fails or you are desperate, install a clean Anaconda and stay out of trouble.

I hope that works

ilciavo
  • 3,069
  • 7
  • 26
  • 40