ImportError: No module named mysql.connector using Python3?

只愿长相守 提交于 2019-12-23 21:46:49

问题


trying to import python-mysql.connector on Python 3.2.3 and receiving an odd stack. I suspect bad configuration on my ubuntu 12.04 install.

    vfi@ubuntu:/usr/share/pyshared$ python3
    Python 3.2.3 (default, Sep 25 2013, 18:22:43) 
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import mysql.connector
    Traceback (most recent call last):
      File "", line 1, in 
    ImportError: No module named mysql.connector
    Error in sys.excepthook:
    Traceback (most recent call last):
      File "/usr/share/pyshared/apport_python_hook.py", line 66, in apport_excepthook
        from apport.fileutils import likely_packaged, get_recent_crashes
      File "apport/__init__.py", line 1, in 
        from apport.report import Report
      File "apport/report.py", line 20, in 
        import apport.fileutils
      File "apport/fileutils.py", line 22, in 
        from apport.packaging_impl import impl as packaging
      File "apport/packaging_impl.py", line 20, in 
        import apt
      File "apt/__init__.py", line 24, in 
        from apt.package import Package
      File "apt/package.py", line 1051
        return file_list.read().decode("utf-8").split(u"\n")
                                                          ^
    SyntaxError: invalid syntax

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

here is the related modules state on my pc:

vfi@ubuntu:/usr/share/pyshared$ sudo aptitude search python3-apt
i   python3-apt                                                                                    - Python 3 interface to libapt-pkg                                                                        
p   python3-apt:i386                                                                               - Python 3 interface to libapt-pkg                                                                        
p   python3-apt-dbg                                                                                - Python 3 interface to libapt-pkg (debug extension)                                                      
p   python3-apt-dbg:i386                                                                           - Python 3 interface to libapt-pkg (debug extension)                                                      
v   python3-apt-dbg:any                                                                            -                                                                                                         
v   python3-apt-dbg:any:i386                                                                       -                                                                                                         
v   python3-apt:any                                                                                -                                                                                                         
v   python3-apt:any:i386                                                                           -                                                                                                         
vfi@ubuntu:/usr/share/pyshared$ sudo aptitude search python-apt
i   python-apt                                                                                     - Python interface to libapt-pkg                                                                          
p   python-apt:i386                                                                                - Python interface to libapt-pkg                                                                          
i   python-apt-common                                                                              - Python interface to libapt-pkg (locales)                                                                
p   python-apt-dbg                                                                                 - Python interface to libapt-pkg (debug extension)                                                        
p   python-apt-dbg:i386                                                                            - Python interface to libapt-pkg (debug extension)                                                        
v   python-apt-dbg:any                                                                             -                                                                                                         
v   python-apt-dbg:any:i386                                                                        -                                                                                                         
p   python-apt-dev                                                                                 - Python interface to libapt-pkg (development files)                                                      
p   python-apt-doc                                                                                 - Python interface to libapt-pkg (API documentation)                                                      
v   python-apt-p2p                                                                                 -                                                                                                         
v   python-apt-p2p-khashmir                                                                        -                                                                                                         
v   python-apt:any                                                                                 -                                                                                                         
v   python-apt:any:i386                                                                            -                                                                                                         
i   python-aptdaemon                                                                               - Python module for the server and client of aptdaemon                                                    
p   python-aptdaemon-gtk                                                                           - Transitional dummy package                                                                              
i   python-aptdaemon.gtk3widgets                                                                   - Python GTK+ 3 widgets to run an aptdaemon client                                                        
p   python-aptdaemon.gtkwidgets                                                                    - Python GTK+ 2 widgets to run an aptdaemon client                                                        
i   python-aptdaemon.pkcompat                                                                      - PackageKit compatibilty for AptDaemon                                                                   
p   python-aptdaemon.test                                                                          - Test environment for aptdaemon clients                                                                  
vfi@ubuntu:/usr/share/pyshared$ sudo aptitude search python-mysql.connector
pi  python-mysql.connector                                                                         - pure Python implementation of MySQL Client/Server protocol     

Hope you can help!

Thanks


回答1:


Finally figured out what was my problem.

python-mysql.connector was not a py3 package and apt-get nor aptitude was proposing such version.

I managed to install it with pip3 which was not so simple on ubuntu 12.04 because it's only bundled with ubuntu starting at 12.10 and the package does not have the same name under pip...

vfi@ubuntu:$sudo apt-get install python3-setuptools
vfi@ubuntu:$sudo easy_install3 pip

vfi@ubuntu:$ pip --version
pip 1.4.1 from /usr/local/lib/python3.2/dist-packages/pip-1.4.1-py3.2.egg (python 3.2)

vfi@ubuntu:$sudo pip install mysql-connector-python



回答2:


This worked for me on CentOS 6.5

sudo yum install mysql-connector-python


来源:https://stackoverflow.com/questions/20275176/importerror-no-module-named-mysql-connector-using-python3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!