‘import mysql.connector’ 无法工作,但’from mysql import connector’可以。

huangapple go评论56阅读模式
英文:

Why does 'import mysql.connector' not work but 'from mysql import connector' does?

问题

我开始学习一些SQL Python,刚开始时遇到了这个现象。我可以

from mysql import connector

但不能

import mysql.connector

这是为什么?

‘import mysql.connector’ 无法工作,但’from mysql import connector’可以。

我还可以在第一行去掉导入语句,第二行仍然能够正常工作。

英文:

I'm starting to learn some SQL Python and upon starting I stumbled over this phenomenon. I can

from mysql import connector

but not

import mysql.connector

What is the reason for this?

‘import mysql.connector’ 无法工作,但’from mysql import connector’可以。

I can also leave away the import statement in the first line for the second line to still work.

答案1

得分: 1

因为 import mysql.connector 意味着 connector 是一个模块,而 from mysql import connector 意味着它是模块内的对象/类/变量。

英文:

That would be because import mysql.connector implies that connector is a module, whereas from mysql import connector implies that it's an object/class/variable inside the module.

huangapple
  • 本文由 发表于 2023年2月16日 06:44:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定