Hướng dẫn no module named mysql-connector - không có mô-đun nào có tên mysql-connector

Tôi đã phải đối mặt với vấn đề tương tự. Chi tiết env của tôi - Python 2.7.11 PIP 9.0.1 Centos Phát hành 5.11 (cuối cùng)

Lỗi trên trình thông dịch Python -

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

Sử dụng PIP để tìm kiếm mô -đun có sẵn -

$ 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

Cài đặt MySQL-ConneNector-Python-RF-

$ pip install mysql-connector-python-rf

Kiểm chứng

$ 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
>>>

Cảm ơn =)

Đối với Python3 và sau đó sử dụng lệnh tiếp theo: $ pip3 cài đặt mysql-connector-python-rf

ModulenotFounderror: Không có mô -đun có tên 'MySQL' trong Python #

Python "ModulenotFounderRor: Không có mô -đun có tên 'MySQL'" xảy ra khi chúng ta quên cài đặt mô -đun

$ 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
2 trước khi nhập hoặc cài đặt nó trong môi trường không chính xác. Để giải quyết lỗi, cài đặt mô -đun bằng cách chạy lệnh
$ 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
3.

Hướng dẫn no module named mysql-connector - không có mô-đun nào có tên mysql-connector

ModulenotFounderror: Không có mô -đun có tên 'MySQL' trong Python #

Copied!

# 👇️ in a virtual environment or using Python 2 pip install mysql-connector-python # 👇️ for python 3 (could also be pip3.10 depending on your version) pip3 install mysql-connector-python # 👇️ if you get permissions error sudo pip3 install mysql-connector-python pip install mysql-connector-python --user # 👇️ if you don't have pip in your PATH environment variable python -m pip install mysql-connector-python # 👇️ for python 3 (could also be pip3.10 depending on your version) python3 -m pip install mysql-connector-python # 👇️ using py alias (Windows) py -m pip install mysql-connector-python # 👇️ for Anaconda conda install -c anaconda mysql-connector-python # 👇️ for Jupyter Notebook !pip install mysql-connector-python

Mở thiết bị đầu cuối của bạn trong thư mục gốc của dự án và cài đặt mô -đun

$ 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
2.

Copied!

import mysql.connector # Connect to server cnx = mysql.connector.connect( host="127.0.0.1", port=3306, user="alice", password="dogsandcats123") # Get a cursor cur = cnx.cursor() # Execute a query cur.execute("SELECT CURDATE()") # Fetch one result row = cur.fetchone() print("Current date is: {0}".format(row[0])) # Close connection cnx.close()

Sau khi bạn cài đặt gói MySQL-ConneNector-Python, hãy thử nhập nó như:

  1. Lỗi python "ModulenotFounderRor: Không có mô -đun có tên 'MySQL'" xảy ra vì nhiều lý do:
  2. Không có gói
    $ 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
    
    2 được cài đặt bằng cách chạy
    $ 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
    
    3.
  3. Cài đặt gói trong một phiên bản Python khác với phiên bản bạn đang sử dụng.
  4. Cài đặt gói trên toàn cầu và không trong môi trường ảo của bạn.
  5. IDE của bạn chạy một phiên bản Python không chính xác.
  6. Đặt tên cho mô -đun của bạn
    $ 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
    
    7 sẽ theo dõi mô -đun chính thức.

Nếu lỗi vẫn còn, hãy lấy phiên bản Python của bạn và đảm bảo bạn đang cài đặt gói bằng phiên bản Python chính xác.

Hướng dẫn no module named mysql-connector - không có mô-đun nào có tên mysql-connector

Ví dụ: phiên bản Python của tôi là

$ 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
9, vì vậy tôi sẽ cài đặt gói mysql-connector-python với
$ pip install mysql-connector-python-rf
0.

Copied!

pip3.10 install mysql-connector-python # 👇️ if you get permissions error use pip3 (NOT pip3.X) sudo pip3 install mysql-connector-python

Lưu ý rằng số phiên bản tương ứng với phiên bản

$ pip install mysql-connector-python-rf
1 tôi đang sử dụng.

Nếu đường dẫn cho

$ pip install mysql-connector-python-rf
1 không được thiết lập trên máy của bạn, hãy thay thế
$ pip install mysql-connector-python-rf
1 bằng
$ pip install mysql-connector-python-rf
4:

Copied!

# 👇️ make sure to use your version of Python, e.g. 3.10 python3 -m pip install mysql-connector-python

Nếu lỗi "Không có tên là 'MySQL'", hãy thử khởi động lại IDE và máy chủ/tập lệnh phát triển của bạn.

Bạn có thể kiểm tra xem bạn có gói

$ 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
2 được cài đặt bằng cách chạy lệnh
$ pip install mysql-connector-python-rf
6 không.

Copied!

# 👇️ check if you have mysql-connector-python installed pip3 show mysql-connector-python # 👇️ if you don't have pip set up in PATH python3 -m pip show mysql-connector-python

Lệnh

$ pip install mysql-connector-python-rf
6 sẽ nêu rõ rằng gói không được cài đặt hoặc hiển thị một loạt thông tin về gói, bao gồm cả vị trí cài đặt gói.

Nếu gói không được cài đặt, hãy đảm bảo IDE của bạn đang sử dụng phiên bản Python chính xác.

Nếu bạn đã cài đặt nhiều phiên bản Python trên máy, bạn có thể đã cài đặt gói

$ 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
2 bằng phiên bản không chính xác hoặc IDE của bạn có thể được thiết lập để sử dụng phiên bản khác.

Ví dụ: trong VSCODE, bạn có thể nhấn

$ pip install mysql-connector-python-rf
9 hoặc (
$ 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
>>>
0 +
$ 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
>>>
1 +
$ 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
>>>
2 trên Mac) để mở bảng lệnh.

Sau đó nhập "Python chọn trình thông dịch" vào trường.

Hướng dẫn no module named mysql-connector - không có mô-đun nào có tên mysql-connector

Sau đó chọn phiên bản Python chính xác từ menu thả xuống.

Hướng dẫn no module named mysql-connector - không có mô-đun nào có tên mysql-connector

IDE của bạn nên sử dụng cùng một phiên bản Python (bao gồm cả môi trường ảo) mà bạn đang sử dụng để cài đặt các gói từ thiết bị đầu cuối của mình.

Nếu bạn đang sử dụng môi trường ảo, hãy chắc chắn rằng bạn đang cài đặt

$ 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
2 trong môi trường ảo của bạn chứ không phải trên toàn cầu.

Bạn có thể thử tạo một môi trường ảo nếu bạn chưa có.

Copied!

# 👇️ use correct version of Python when creating VENV python3 -m venv venv # 👇️ activate on Unix or MacOS source venv/bin/activate # 👇️ activate on Windows (cmd.exe) venv\Scripts\activate.bat # 👇️ activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # 👇️ install mysql-connector-python in virtual environment pip install mysql-connector-python

Nếu lệnh

$ 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
>>>
4 không hoạt động, hãy thử 2 lệnh sau:

  • $ 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
    >>>
    
    5
  • $ 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
    >>>
    
    6

Môi trường ảo của bạn sẽ sử dụng phiên bản Python được sử dụng để tạo ra nó.

Nếu lỗi vẫn còn, hãy đảm bảo bạn chưa đặt tên cho một mô -đun trong dự án của mình là

$ 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
7 vì điều đó sẽ che mờ mô -đun
$ 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
2 ban đầu.

Bạn cũng không nên khai báo một biến có tên

$ 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
2 vì điều đó cũng sẽ che mờ mô -đun ban đầu.

Nếu lỗi không được giải quyết, hãy thử gỡ cài đặt gói

$ 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
2 và sau đó cài đặt nó.

$ 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
0

Hãy thử khởi động lại IDE và máy chủ phát triển/tập lệnh phát triển của bạn.

Bạn cũng có thể cố gắng nâng cấp phiên bản của gói MySQL-ConneNector-Python.

$ 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
1

Nếu lỗi vẫn còn, tôi sẽ đề nghị xem một video nhanh về cách sử dụng môi trường ảo trong Python.

Cái này là để sử dụng môi trường ảo (VENV) trên

Copied!

# 👇️ in a virtual environment or using Python 2 pip install mysql-connector-python # 👇️ for python 3 (could also be pip3.10 depending on your version) pip3 install mysql-connector-python # 👇️ if you get permissions error sudo pip3 install mysql-connector-python pip install mysql-connector-python --user # 👇️ if you don't have pip in your PATH environment variable python -m pip install mysql-connector-python # 👇️ for python 3 (could also be pip3.10 depending on your version) python3 -m pip install mysql-connector-python # 👇️ using py alias (Windows) py -m pip install mysql-connector-python # 👇️ for Anaconda conda install -c anaconda mysql-connector-python # 👇️ for Jupyter Notebook !pip install mysql-connector-python
1:

Cái này là để sử dụng môi trường ảo (VENV) trên

Copied!

# 👇️ in a virtual environment or using Python 2 pip install mysql-connector-python # 👇️ for python 3 (could also be pip3.10 depending on your version) pip3 install mysql-connector-python # 👇️ if you get permissions error sudo pip3 install mysql-connector-python pip install mysql-connector-python --user # 👇️ if you don't have pip in your PATH environment variable python -m pip install mysql-connector-python # 👇️ for python 3 (could also be pip3.10 depending on your version) python3 -m pip install mysql-connector-python # 👇️ using py alias (Windows) py -m pip install mysql-connector-python # 👇️ for Anaconda conda install -c anaconda mysql-connector-python # 👇️ for Jupyter Notebook !pip install mysql-connector-python
2 và

Copied!

# 👇️ in a virtual environment or using Python 2 pip install mysql-connector-python # 👇️ for python 3 (could also be pip3.10 depending on your version) pip3 install mysql-connector-python # 👇️ if you get permissions error sudo pip3 install mysql-connector-python pip install mysql-connector-python --user # 👇️ if you don't have pip in your PATH environment variable python -m pip install mysql-connector-python # 👇️ for python 3 (could also be pip3.10 depending on your version) python3 -m pip install mysql-connector-python # 👇️ using py alias (Windows) py -m pip install mysql-connector-python # 👇️ for Anaconda conda install -c anaconda mysql-connector-python # 👇️ for Jupyter Notebook !pip install mysql-connector-python
3:

Sự kết luận #

Python "ModulenotFounderRor: Không có mô -đun có tên 'MySQL'" xảy ra khi chúng ta quên cài đặt mô -đun

$ 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
2 trước khi nhập hoặc cài đặt nó trong môi trường không chính xác. Để giải quyết lỗi, cài đặt mô -đun bằng cách chạy lệnh
$ 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
3.