Python3 始终显示 ImportError 消息
Python3 always shows ImportError message
每当我尝试 运行 脚本时,python 解释器总是显示 ImportError
消息,例如(例如)No module named 'setuptools'
。因此,我尝试使用 apt-get
安装(或满足此要求)...我对 Python 2.7 和 Python 3.5 都执行此操作,直到 Requirement already satisfied
.
首先,我不使用 Python 2.7,但它是解释器的默认版本。那么,我该如何解决这个问题以使用 Python 3.5?我试过这个:
>>> import sys
>>> print(sys.path)
['',
'/usr/local/lib/python35.zip',
'/usr/local/lib/python3.5',
'/usr/local/lib/python3.5/plat-linux',
'/usr/local/lib/python3.5/lib-dynload',
'/usr/local/lib/python3.5/site-packages']
这是 Python3,Python2 我做了同样的比较路径,我得到了这个:
>>> import sys
>>> print(sys.path)
['',
'/usr/local/lib/python2.7/dist-packages/pygame-1.9.2b8-py2.7-linux-x86_64.egg',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0']
现在... 如果我使用append()
方法将Python2 中的所有路径添加到Python3 中的路径是否可以工作?另外,我曾考虑完全卸载 Python2,但我知道这会导致我的系统出现比我尝试解决的问题更多的问题。
尝试:
python3.5 -m pip install setuptools
根据您对问题的描述,您可能已经安装了 Python 2 个版本的软件包 apt
and/or pip
。例如,sudo apt-get install python-django
将安装 Python 2 版本的 Django,而 sudo apt-get install python3-django
将安装 Py3 版本。
你最终会 运行 进入需要使用 pip
的情况,因为你想要的包不会在 Debian/Ubuntu 存储库中。在这种情况下,请确保您使用的是正确的 pip
。尝试 运行宁
pip -V
和
pip3 -V
查看在您调用 pip
时附加了哪些 Python 版本,然后使用适合您希望定位的 Python 版本的版本。
最后,在任何情况下 都不应将 Python 2 路径添加到 Python 3 的 sys.path
。
编辑
这是我的 sys.path
在 Ubuntu 16.04 使用系统的 Python 3.5.2:
$ python3
Python 3.5.2 (default, Jul 5 2016, 12:43:10)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> from pprint import pprint as pp
>>> pp(sys.path)
['',
'/usr/local/lib/python3.5/dist-packages/pandas-0.18.1-py3.5-linux-x86_64.egg',
'/usr/local/lib/python3.5/dist-packages/github3.py-1.0.0a4-py3.5.egg',
'/usr/local/lib/python3.5/dist-packages/uritemplate.py-0.3.0-py3.5.egg',
'/usr/lib/python3/dist-packages',
'/usr/lib/python35.zip',
'/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu',
'/usr/lib/python3.5/lib-dynload',
'/usr/local/lib/python3.5/dist-packages']
>>> print(sys.executable)
/usr/bin/python3
>>>
您会注意到路径是 dist-packages
条路径,而您有 site-packages
条路径。从用户的角度来看,两者之间的差别很小,所以不用担心。我还特意改了一些路径(说来话长)。
每当我尝试 运行 脚本时,python 解释器总是显示 ImportError
消息,例如(例如)No module named 'setuptools'
。因此,我尝试使用 apt-get
安装(或满足此要求)...我对 Python 2.7 和 Python 3.5 都执行此操作,直到 Requirement already satisfied
.
首先,我不使用 Python 2.7,但它是解释器的默认版本。那么,我该如何解决这个问题以使用 Python 3.5?我试过这个:
>>> import sys
>>> print(sys.path)
['',
'/usr/local/lib/python35.zip',
'/usr/local/lib/python3.5',
'/usr/local/lib/python3.5/plat-linux',
'/usr/local/lib/python3.5/lib-dynload',
'/usr/local/lib/python3.5/site-packages']
这是 Python3,Python2 我做了同样的比较路径,我得到了这个:
>>> import sys
>>> print(sys.path)
['',
'/usr/local/lib/python2.7/dist-packages/pygame-1.9.2b8-py2.7-linux-x86_64.egg',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0']
现在... 如果我使用append()
方法将Python2 中的所有路径添加到Python3 中的路径是否可以工作?另外,我曾考虑完全卸载 Python2,但我知道这会导致我的系统出现比我尝试解决的问题更多的问题。
尝试:
python3.5 -m pip install setuptools
根据您对问题的描述,您可能已经安装了 Python 2 个版本的软件包 apt
and/or pip
。例如,sudo apt-get install python-django
将安装 Python 2 版本的 Django,而 sudo apt-get install python3-django
将安装 Py3 版本。
你最终会 运行 进入需要使用 pip
的情况,因为你想要的包不会在 Debian/Ubuntu 存储库中。在这种情况下,请确保您使用的是正确的 pip
。尝试 运行宁
pip -V
和
pip3 -V
查看在您调用 pip
时附加了哪些 Python 版本,然后使用适合您希望定位的 Python 版本的版本。
最后,在任何情况下 都不应将 Python 2 路径添加到 Python 3 的 sys.path
。
编辑
这是我的 sys.path
在 Ubuntu 16.04 使用系统的 Python 3.5.2:
$ python3
Python 3.5.2 (default, Jul 5 2016, 12:43:10)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> from pprint import pprint as pp
>>> pp(sys.path)
['',
'/usr/local/lib/python3.5/dist-packages/pandas-0.18.1-py3.5-linux-x86_64.egg',
'/usr/local/lib/python3.5/dist-packages/github3.py-1.0.0a4-py3.5.egg',
'/usr/local/lib/python3.5/dist-packages/uritemplate.py-0.3.0-py3.5.egg',
'/usr/lib/python3/dist-packages',
'/usr/lib/python35.zip',
'/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu',
'/usr/lib/python3.5/lib-dynload',
'/usr/local/lib/python3.5/dist-packages']
>>> print(sys.executable)
/usr/bin/python3
>>>
您会注意到路径是 dist-packages
条路径,而您有 site-packages
条路径。从用户的角度来看,两者之间的差别很小,所以不用担心。我还特意改了一些路径(说来话长)。