【问题标题】:mysql connector installed in python 2.6, but not on 2.7mysql 连接器安装在 python 2.6 中,但不在 2.7 上
【发布时间】:2014-03-04 15:34:32
【问题描述】:

RedHat 是我的操作系统。默认情况下,它运行python2.6。但是,我需要安装python2.7。我在this post 之后安装了它。现在,当我跑步时:

$ python -V
Python 2.7.5

然而

$ sudo python -V
Python 2.6.6

所以,我在安装库时遇到了问题。 我尝试通过以下方式安装mysql-connector

但是,python 2.7 版本上没有安装 mysql-connector 模块。如果我跑:

$ python
>>> import mysql.connector
ImportError: No module named mysql.connector

如何在 python 2.7 版上安装 mysql 模块?

编辑

附加信息:

$ which pip
/usr/bin/pip

$ sudo which pip
/usr/bin/pip

$ which easy_install
/opt/rh/python27/root/usr/bin/easy_install

$ sudo which easy_install
/usr/bin/easy_install

/usr/bin/ $ ls -al pip
-rwxr-xr-x. 1 root root 281 Feb  7 11:07 pip

/opt/rh/python27/root/usr/bin $ ls -al easy_install
-rwxr-xr-x. 1 root root 344 Aug 16 11:40 easy_install

【问题讨论】:

  • 类似于 this answer
  • 可以在python 2.6中导入mysql.connector吗?也许问题不在于多个 python 版本,而在于您尝试导入的方式?

标签: python python-2.7 pip mysql-python mysql-connector


【解决方案1】:

你应该在python2.7.5环境下安装pip。之后 pip install 将按预期工作

【讨论】:

  • 能否描述一下针对特定环境的热安装pip?
  • 您的 2.7 环境中似乎有 easy_install。使用 easy_install pip
  • 运行 easy_install pip 时出现错误:[Errno 13] Permission denied: '/opt/rh/python27/root/usr/lib/python2.7/site-packages/test- easy-install-9705.write-test' 如果我尝试sudo easy_install pip,它会在 2.6 环境中安装它
  • 试试 sudo -u username easy_install pip
【解决方案2】:

这正是您应该使用virtualenv的原因。

【讨论】:

  • 谢谢,我正在设置 virtualenv:virtualenv project --python=python2.7。但是,当尝试在激活的 virtualenv 中运行 pip install mysql-connector-python 时,我得到以下信息: 下载/解包 mysql-connector-python 找不到任何满足 mysql-connector-python 要求的下载 忽略了一些外部托管的文件(使用 - -allow-external mysql-connector-python 允许)。清理...没有找到 mysql-connector-python 的任何发行版
  • 然后我设法通过pip install --allow-all-external mysql-connector-python安装了它
【解决方案3】:

请检查 pip 和 easy_installed 安装在哪里?我猜which pip 会显示 pip 在 python 2.6 文件夹中。您需要将 pip 和 easy_install 重新安装到 python 2.7 文件夹。你可以这样做:

wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | python 

然后检查which easy_install 指向正确的方向(您可能需要将easy_install 别名/符号链接到新安装。以覆盖系统easy_install)然后您就可以easy_install pip

编辑(OP 输入后)

  1. 尝试easy_install pip(因为基本的easy_install是python 2.7)

更新:

由于某种原因 python2.7 easy install 被安装为 root 而不是您的用户,您可以更改 easy_instlal 的所有权(或者您可能需要整个 python2.7 库。尝试尽可能少地更改) -你可以用 chown 命令来做:

chown username:usergroup(usually your username also) path/to/change/ownership

【讨论】:

  • 我添加了which 命令的结果。但是,当我运行您发布的命令时,出现以下错误: [Errno 13] Permission denied: '/opt/rh/python27/root/usr/lib/python2.7/site-packages/test-easy-install -9203.write-test'
  • 还在 pip 和 easy_install 位置运行 ls -al(如 ls --al /usr/bin/pip。我怀疑 pip 符号链接到其他地方
  • 我添加了ls -al的结果。运行 easy_install pip 时,出现错误:[Errno 13] Permission denied: '/opt/rh/python27/root/usr/lib/python2.7/site-packages/test-easy-install-9705.wr‌​ ite-test' 如果我尝试 sudo easy_install pip,它会在 2.6 env 中安装它
猜你喜欢
  • 2021-05-26
  • 2016-05-11
  • 1970-01-01
  • 2016-08-08
  • 2016-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-25
相关资源
最近更新 更多