mysql-connector 不会导入到 pycharm 中的脚本中 [英] mysql-connector won't import into script in pycharm

查看:55
本文介绍了mysql-connector 不会导入到 pycharm 中的脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 pip 成功安装了 mysql-connector.

I have successfully installed mysql-connector using pip.

Installing collected packages: mysql-connector
Running setup.py install for mysql-connector ... done
Successfully installed mysql-connector-2.1.6

但是,在 PyCharm 中,当我有一个使用该行的脚本时:

However, in PyCharm when I have a script that uses the line:

import mysql-connector

PyCharm 提示我没有安装名为 "mysql" 的包.是否应该使用某种语法来指示整个包名称包含-"而不仅仅是mysql"?

PyCharm gives me an error saying there isn't a package called "mysql" installed. Is there some sort of syntax that should be used to indicate that the entire package name contains the "-" and is not just "mysql"?

当我在 IDLE 中运行我的脚本时,mysql.connector 导入得很好.(我看到包名中有-",在PyCharm中遇到了问题,我把它改成了mysql-connector.)

When I run my script in IDLE, mysql.connector imports just fine. (I changed it to mysql-connector after seeing the "-" in the name of the package and having trouble in PyCharm.)

根据@FlyingTeller 的建议,在终端中,where python"返回 C:...Programs\Python\Python36-32\python.exe."where pip" 返回 ...Python\Python36-32\Scripts\pip.exe.这个项目的 PyCharm 解释器是同一个文件路径 &exe 作为终端中的where python".

per @FlyingTeller's suggestions, in the terminal, "where python" returns C:...Programs\Python\Python36-32\python.exe. "where pip" returns ...Python\Python36-32\Scripts\pip.exe. The interpreter in PyCharm for this project is this same filepath & exe as "where python" in the terminal.

根据@Tushar 的评论,该程序未使用虚拟环境,并且 mysql-connector 库已存在于 Preferences->Project->Python Interpreter 中.

Per @Tushar's comment, this program isn't using a virtual environment and the mysql-connector library is already present in the Preferences->Project->Python Interpreter.

感谢您的反馈以及您可以提供的任何其他指导.

Thanks for the feedback and any additional guidance you may be able to provide.

推荐答案

您需要将连接器导入为

import mysql.connector

查看文档为此

如果这不起作用,则 pycharm 使用的解释器与您安装软件包的解释器之间可能存在不一致.在pycharm中,转到

If that doesn't work, then there might be an inconsistency between the interpreter that pycharm uses and the one you installed the package for. In pycharm, go to

File->Settings->Project Interpreter

在终端输入

where python #Windows
which python #Linux

还有

where/which pip

确保在 pycharm 中配置的解释器与在命令行/shell 中键入 which/where python 时出现的解释器相同.还要确保 pip 也指向相同的 python 发行版.

make sure that the interpreter configured in pycharm is the same that appears when typing which/where python in the command line/shell. Also make sure that pip also points to the same python distribution.

这篇关于mysql-connector 不会导入到 pycharm 中的脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆