Python can't find modules with PYTHONPATH set

大兔子大兔子 提交于 2020-01-04 23:48:27

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/30963386/python-cant-find-modules-with-pythonpath-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!