Python模块不在正确的位置

时间:2018-03-27 16:06:46

标签: python

我们在Hadoop集群中安装了CentOS 7服务器。 Python和pip应该以相同的方式安装在所有服务器上,因为它是用Ansible完成的。但由于某些原因,有些服务器的Python无法使用已安装的模块。比如pandas就是安装,但是在python3.6中,我得到一个'没有名为pandas的模块'错误。

$ python3.6
Python 3.6.2 (default, Aug  2 2017, 14:51:00)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pandas'
>>>

如果我再次尝试安装pandas,我会收到已经满足要求的消息。

$ sudo pip3.6 install pandas
Requirement already satisfied: pandas in /usr/lib64/python3.6/site-packages
Requirement already satisfied: python-dateutil>=2 in /usr/lib/python3.6/site-packages (from pandas)
Requirement already satisfied: pytz>=2011k in /usr/lib/python3.6/site-packages (from pandas)
Requirement already satisfied: numpy>=1.9.0 in /usr/lib64/python3.6/site-packages (from pandas)
Requirement already satisfied: six>=1.5 in /usr/lib/python3.6/site-packages (from python-dateutil>=2->pandas)

似乎python 3.6 sys.path正在为站点包使用不同的位置。

$ python3.6 -m site
sys.path = [
    '/home/avalenti',
    '/usr/local/lib/python36.zip',
    '/usr/local/lib/python3.6',
    '/usr/local/lib/python3.6/lib-dynload',
    '/usr/local/lib/python3.6/site-packages',
]

这可能都源于将pip安装在一个不寻常的位置。

$ pip3.6 --version
pip 9.0.1 from /usr/lib/python3.6/site-packages (python 3.6)

Pip和模块似乎位于相同的位置,正确地在服务器上工作如下:

$ pip3.6 --version
pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6)
$ python3.6 -m site
sys.path = [
    '/home/avalenti',
    '/usr/local/lib/python36.zip',
    '/usr/local/lib/python3.6',
    '/usr/local/lib/python3.6/lib-dynload',
    '/usr/local/lib/python3.6/site-packages',
]
$ python3.6
Python 3.6.2 (default, Aug  2 2017, 14:17:20)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>> exit()
$ pip3.6 show pandas
Name: pandas
Version: 0.22.0
Summary: Powerful data structures for data analysis, time series,and statistics
Home-page: http://pandas.pydata.org
Author: The PyData Development Team
Author-email: pydata@googlegroups.com
License: BSD
Location: /usr/local/lib/python3.6/site-packages
Requires: numpy, pytz, python-dateutil

所以我的问题是,如何解决这个问题?如何防止这种情况发生?我似乎没有找到配置Pip的方法,我找不到任何方法来指定模块的安装位置。 我试图彻底了解所提供的信息,但我确信我错过了一些东西。

谢谢,

安东尼

嗯,配置的Pip文档向我指出了pip install的--target选项。这允许我指定我认为包应该去的位置 - /usr/local/lib/python3.6/site-packages(如sys.path中所述)。这允许导入找到pandas包,但导致一些基本系统模块 builtin 和binascii导入失败。好像python本身有点h。

# pip3.6 install --target /usr/local/lib/python3.6/site-packages pandas
Collecting pandas
  Using cached pandas-0.22.0-cp36-cp36m-manylinux1_x86_64.whl
Collecting numpy>=1.9.0 (from pandas)
  Downloading numpy-1.14.2-cp36-cp36m-manylinux1_x86_64.whl (12.2MB)
    100% |████████████████████████████████| 12.2MB 92kB/s
Collecting python-dateutil>=2 (from pandas)
  Downloading python_dateutil-2.7.2-py2.py3-none-any.whl (212kB)
    100% |████████████████████████████████| 215kB 2.4MB/s
Collecting pytz>=2011k (from pandas)
  Using cached pytz-2018.3-py2.py3-none-any.whl
Collecting six>=1.5 (from python-dateutil>=2->pandas)
  Using cached six-1.11.0-py2.py3-none-any.whl
Installing collected packages: numpy, six, python-dateutil, pytz, pandas
Successfully installed numpy-1.14.2 pandas-0.22.0 python-dateutil-2.7.2 pytz-2018.3 six-1.11.0
You are using pip version 9.0.1, however version 9.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
# python3.6
Python 3.6.2 (default, Aug  2 2017, 14:51:00)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/pandas/compat/__init__.py", line 48, in <module>
    import __builtin__ as builtins
ModuleNotFoundError: No module named '__builtin__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/pandas/__init__.py", line 23, in <module>
    from pandas.compat.numpy import *
  File "/usr/local/lib/python3.6/site-packages/pandas/compat/__init__.py", line 61, in <module>
    import http.client as httplib
  File "/usr/local/lib/python3.6/http/client.py", line 71, in <module>
    import email.parser
  File "/usr/local/lib/python3.6/email/parser.py", line 12, in <module>
    from email.feedparser import FeedParser, BytesFeedParser
  File "/usr/local/lib/python3.6/email/feedparser.py", line 27, in <module>
    from email._policybase import compat32
  File "/usr/local/lib/python3.6/email/_policybase.py", line 7, in <module>
    from email import header
  File "/usr/local/lib/python3.6/email/header.py", line 14, in <module>
    import binascii
ModuleNotFoundError: No module named 'binascii'
>>> exit()

啊...

3 个答案:

答案 0 :(得分:0)

您可以尝试在$HOME/.pip/pip.conf

中设置pip的位置

根据PIP文件:

  

配置配置文件

     

pip允许您在标准中设置所有命令行选项默认值   ini样式配置文件。

     

配置文件的名称和位置略有不同   跨平台。您可能有每个用户,每个virtualenv或站点范围   (在所有用户之间共享)配置:

     

每用户:

On Unix the default configuration file is: $HOME/.config/pip/pip.conf which respects the XDG_CONFIG_HOME
     

环境变量。       在macOS上,配置文件是$ HOME / Library / Application Support / pip / pip.conf。       在Windows上,配置文件为%APPDATA%\ pip \ pip.ini。

     

还有一个传统的每用户配置文件   受到尊重,这些位于:

On Unix and macOS the configuration file is: $HOME/.pip/pip.conf
On Windows the configuration file is: %HOME%\pip\pip.ini
     

您可以使用以下命令为此配置文件设置自定义路径位置   环境变量PIP_CONFIG_FILE。

     

在virtualenv中:

On Unix and macOS the file is $VIRTUAL_ENV/pip.conf
On Windows the file is: %VIRTUAL_ENV%\pip.ini
     

站点范围:

On Unix the file may be located in /etc/pip.conf. Alternatively it may be in a "pip" subdirectory of any of the paths set in the
     例如,

环境变量XDG_CONFIG_DIRS(如果存在)   /etc/xdg/pip/pip.conf。       在macOS上,文件是:/ Library / Application Support / pip / pip.conf       在Windows XP上,文件为:C:\ Documents and Settings \ All Users \ Application Data \ pip \ pip.ini       在Windows 7及更高版本中,该文件是隐藏的,但可在C:\ ProgramData \ pip \ pip.ini中写入       Windows Vista不支持站点范围的配置

     

如果pip找到了多个配置文件,那么它们就是   按以下顺序组合:

Firstly the site-wide file is read, then
The per-user file is read, and finally
The virtualenv-specific file is read.
     

每个文件读取都会覆盖从以前的文件中读取的任何值,因此如果   全局超时在站点范围文件和   每个用户文件,然后后一个值将是将使用的值。

答案 1 :(得分:0)

在拥有多个Python发行版时,需要非常小心地使用pip。而不是使用pip3.6', try using python3.6 -m pip`来安装pandas:

python3.6 -m pip install pandas

(如果你真的需要,请提前sudo)。通过这种方式,您可以通过Python本身调用pip,因此您可以保证获得属于所需Python的pip。

答案 2 :(得分:0)

简短回答,但我建议使用Python虚拟环境。使管理Python版本和pip包非常容易。

https://docs.python.org/3/tutorial/venv.html

通过这种方式,您可以拥有许多不同的虚拟化Python环境,并为您正在运行的任何脚本,Ansible playbook等激活它们。另一个很棒的功能是你可以创建一个需要与其他人共享的需求文件,所以如果他们想要运行你的代码,他们只需从需求文件安装就可以保证他们安装了正确版本的Python和相关软件包。