Python无法找到PYTHONPATH集的模块

时间:2015-06-21 10:12:18

标签: python import path pythonpath sys.path

我对新的python 2.7.10感到困惑。

我从tar.xz安装了python 2.7.10,这是从官方网站下载的。然后我将/usr/local/bin/python2.7与/usr/bin/python2.7和/ usr / bin / python相关联,但是当我尝试导入模块时,我得到 ImportError:No名为" module_name" 的模块。例如:

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

但是如果:

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

我们得到:

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

因此,gtk模块已成功导入。

让&#34; cd~&#34;并查看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']

好的,没有/usr/lib/python2.7/dist-packages,让我们添加它: 导出PYTHONPATH=$PYTHONPATH:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7

现在:

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']

但它仍然找不到模块:

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

我必须做什么?

1 个答案:

答案 0 :(得分:2)

似乎你有两个python版本。

默认版本通常链接到:

/usr/bin/python 

which -a python可以看到,您的本地版本位于:

/usr/local/bin/python 

这与

的本地二进制文件相关联
/usr/bin/python 

pip install使用默认的python安装软件包。这就是您无法从本地安装导入软件包的原因,即使该软件包在$PYTHONPATH中可见。

如果python -m pip install未在您的本地版本上安装所需的软件包,请尝试创建虚拟环境(不要使用sudo

如果一切都失败或您绝望,请安装干净的Anaconda并避免麻烦。

我希望有效