无法在 virtualenv 中安装 mysql-connector-python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22100757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:13:29  来源:igfitidea点击:

Can not get mysql-connector-python to install in virtualenv

pythonmysql

提问by slim

I'm using Amazon Linux AMI release 2013.09. I've install virtualenv and after activation then I run pip install mysql-connector-python, but when I run my app I get an error: ImportError: No module named mysql.connector. Has anyone else had trouble doing this? I can install it outside of virtualenv and my script runs without issues. Thanks in advance for any help!

我正在使用 Amazon Linux AMI 版本 2013.09。我已经安装了 virtualenv 并在激活后运行 pip install mysql-connector-python,但是当我运行我的应用程序时出现错误:ImportError: No module named mysql.connector. 有没有其他人在这样做时遇到问题?我可以在 virtualenv 之外安装它,我的脚本运行没有问题。在此先感谢您的帮助!

采纳答案by Kinjal Dixit

Several things. There is an inconsistency in package naming so you may want to do:

几件事。包命名不一致,因此您可能需要执行以下操作:

pip search mysql-connector

to find out what it is called on your platform. I got two results mysql-connector-pythonand mysql-connector-repackaged.

找出它在您的平台上的名称。我得到了两个结果mysql-connector-pythonmysql-connector-repackaged

so try this first:

所以先试试这个:

pip install mysql-connector-python

this may additionally give an error like this:

这可能还会出现这样的错误:

Some externally hosted files were ignored (use 
    --allow-external mysql-connector-python to allow).

so finally this should do the job:

所以最后这应该可以完成工作:

pip install mysql-connector-python --allow-external mysql-connector-python

回答by George Chalhoub

Solution I found:

我发现的解决方案:

sudo pip install mysql-connector-python-rf

If you see this error: option --single-version-externally-managed not recognized, try this:

如果您看到此错误:option --single-version-externally-managed not recognized,请尝试以下操作:

sudo pip install --egg mysql-connector-python-rf

回答by SanD

try my answer here. Though i meant it for Python3, u can just modify the command python3->python to make it work for python2

在这里试试我的答案。虽然我的意思是 Python3,但你可以修改命令 python3->python 使其适用于 python2

回答by DenisH

I've battled with this and tried upgrading pip and setuptools but actually it seems all you need to do is:

我已经与此作斗争并尝试升级 pip 和 setuptools 但实际上似乎您需要做的就是:

sudo pip install virtualenv --upgrade

Once you've upgraded virtualenv, create a new virtual environment, activate it and try installing with:

升级 virtualenv 后,创建一个新的虚拟环境,激活它并尝试安装:

pip install https://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.1.3.tar.gz

(you might need to update the url, but that's the current one)

(您可能需要更新网址,但这是当前网址)

回答by wpodgorski

I have managed to overcome this problem by entering mysql-connector-pythonpackage URL, taken from MySQL page, directly to the requirements file, instead of dependency name.

我设法通过输入mysql-connector-python从 MySQL 页面获取的包 URL 直接到需求文件而不是依赖项名称来克服这个问题。

My requirements file looks like this:

我的需求文件如下所示:

bson==0.4.2
https://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.0.4.tar.gz
pymongo==3.2.1

After creating virtualenv and switching into it I am executing

在创建 virtualenv 并切换到它之后,我正在执行

$ pip install -r ./requirements

And pip is doing the rest of the work, i.e., downloading, extracting and installing.

而 pip 正在做剩下的工作,即下载、解压和安装。

回答by prashanth

pip install mysql-connector

This worked for me!

这对我有用!

回答by slim

I'd like to add that

我想补充一点

sudo easy_install mysql-connector

worked for me after pip kept crashing no matter what I did.

无论我做什么,pip 都在不断崩溃后为我工作。

回答by Rishi

I was facing the similar issue. My env details -
Python 2.7.11
pip 9.0.1
CentOS release 5.11 (Final)

我面临着类似的问题。我的环境细节 -
Python 2.7.11
pip 9.0.1
CentOS release 5.11(最终版)

Error on python interpreter -

python解释器错误 -

>>> import mysql.connector
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named mysql.connector
>>>

Use pip to search the available module -

使用 pip 搜索可用模块 -

$ pip search mysql-connector | grep --color mysql-connector-python



mysql-connector-python-rf (2.2.2)        - MySQL driver written in Python
mysql-connector-python (2.0.4)           - MySQL driver written in Python

Install the mysql-connector-python-rf -

安装 mysql-connector-python-rf -

$ pip install mysql-connector-python-rf

Verify

核实

$ python
Python 2.7.11 (default, Apr 26 2016, 13:18:56)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>>

回答by Thomas Webber

Command

命令

pip install mysql-connector-python-rf

worked for me in my pyenv python version 2.7.12 and pip version 9.0.1.

在我的 pyenv python 版本 2.7.12 和 pip 版本 9.0.1 中为我工作。

回答by Mario

Also something that can go wrong: Don't name your own module mysql

还有一些可能出错的地方:不要命名你自己的模块 mysql

import mysql.connectorwill fail because the import gives the module in the project precedence over site packages and yours likely doesnt have a connector.pyfile.

import mysql.connector将失败,因为导入使项目中的模块优先于站点包,而您的可能没有connector.py文件。