简体   繁体   English

Python 3 ImportError:没有名为“ConfigParser”的模块

[英]Python 3 ImportError: No module named 'ConfigParser'

I am trying to pip install the MySQL-python package, but I get an ImportError .我正在尝试pip install MySQL-python包,但我收到了ImportError

Jans-MacBook-Pro:~ jan$ /Library/Frameworks/Python.framework/Versions/3.3/bin/pip-3.3 install MySQL-python
Downloading/unpacking MySQL-python
  Running setup.py egg_info for package MySQL-python
    Traceback (most recent call last):
      File "<string>", line 16, in <module>
      File "/var/folders/lf/myf7bjr57_jg7_5c4014bh640000gn/T/pip-build/MySQL-python/setup.py", line 14, in <module>
        from setup_posix import get_config
      File "./setup_posix.py", line 2, in <module>
        from ConfigParser import SafeConfigParser
    ImportError: No module named 'ConfigParser'
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 16, in <module>

  File "/var/folders/lf/myf7bjr57_jg7_5c4014bh640000gn/T/pip-build/MySQL-python/setup.py", line 14, in <module>

    from setup_posix import get_config

  File "./setup_posix.py", line 2, in <module>

    from ConfigParser import SafeConfigParser

ImportError: No module named 'ConfigParser'

----------------------------------------
Command python setup.py egg_info failed with error code 1 in /var/folders/lf/myf7bjr57_jg7_5c4014bh640000gn/T/pip-build/MySQL-python
Storing complete log in /Users/jan/.pip/pip.log
Jans-MacBook-Pro:~ jan$ 

Any ideas?有任何想法吗?

You can instead use the mysqlclient package as a drop-in replacement for MySQL-python.您可以改为使用mysqlclient包作为 MySQL-python 的替代品。 It is a fork of MySQL-python with added support for Python 3.它是MySQL-python一个分支,增加了对 Python 3 的支持。

I had luck with simply我很幸运

pip install mysqlclient

in my python3.4 virtualenv after在我的 python3.4 virtualenv 之后

sudo apt-get install python3-dev libmysqlclient-dev

which is obviously specific to ubuntu/debian, but I just wanted to share my success :)这显然是针对 ubuntu/debian 的,但我只是想分享我的成功:)

In Python 3, ConfigParser has been renamed to configparser for PEP 8 compliance.在 Python 3 中, ConfigParser已重命名为configparser以符合 PEP 8。 It looks like the package you are installing does not support Python 3.您正在安装的软件包似乎不支持 Python 3。

Here is a code that should work in both Python 2.x and 3.x这是一个应该在 Python 2.x 和 3.x 中工作的代码

Obviously you will need the six module, but it's almost impossible to write modules that work in both versions without six.显然,您将需要six模块,但几乎不可能编写在没有六个模块的情况下在两个版本中都能工作的模块。

try:
    import configparser
except:
    from six.moves import configparser
pip install configparser
sudo cp /usr/lib/python3.6/configparser.py /usr/lib/python3.6/ConfigParser.py

Then try to install the MYSQL-python again.然后再次尝试安装MYSQL-python。 That Worked for me这对我有用

MySQL-python is not supported on python3 instead of this you can use mysqlclient python3 不支持 MySQL-python 而不是你可以使用mysqlclient

If you are on fedora/centos/Red Hat install following package如果您在fedora/centos/Red Hat安装以下软件包

  1. yum install python3-devel
  2. pip install mysqlclient

如果您使用的是 CentOS,那么您需要使用

  1. yum install python34-devel.x86_64
  2. yum groupinstall -y 'development tools'
  3. pip3 install mysql-connector
  4. pip install mysqlclient

Python 2/3 对configparser兼容性可以简单地通过six库来解决

from six.moves import configparser

I was having the same problem.我遇到了同样的问题。 Turns out, I needed to install python3 devel on my centos.原来,我需要在我的 centos 上安装 python3 devel。 First, you need to search for the package that is compatible with your system.首先,您需要搜索与您的系统兼容的软件包。

yum search python3 | grep devel

Then, install the package as:然后,将软件包安装为:

yum install -y python3-devel.x86_64

Then, install mysqlclient from pip然后,从pip安装mysqlclient

pip install mysqlclient

Do pip3 install PyMySQL and then pip3 install mysqlclient .pip3 install PyMySQL然后pip3 install mysqlclient Worked for me为我工作

I got further with Valeres answer:我得到了更进一步的 Valeres 回答:

pip install configparser sudo cp /usr/lib/python3.6/configparser.py /usr/lib/python3.6/ConfigParser.py Then try to install the MYSQL-python again. pip install configparser sudo cp /usr/lib/python3.6/configparser.py /usr/lib/python3.6/ConfigParser.py 然后再次尝试安装MYSQL-python。 That Worked for me这对我有用

I would suggest to link the file instead of copy it.我建议链接文件而不是复制它。 It is save to update.保存更新。 I linked the file to /usr/lib/python3/ directory.我将文件链接到/usr/lib/python3/目录。

Try this solution which worked fine for me.试试这个对我来说很好的解决方案。

Basically it's to reinstall/upgrade to latest version of mysql from brew , and then installing mysqlclient or MySQL-Python from global pip3 instead of virtualenv pip3 .基本上是从brew重新安装/升级到最新版本的mysql ,然后从global pip3而不是virtualenv pip3安装mysqlclientMySQL-Python

Then accessing the virtualenv and successfully install mysqlclient or MySQL-Python .然后访问virtualenv并成功安装mysqlclientMySQL-Python

how about checking the version of Python you are using first.首先检查您使用的Python版本如何。

import six
if six.PY2:
    import ConfigParser as configparser
else:
    import configparser

I run kali linux- Rolling and I came across this problem ,when I tried running cupp.py in the terminal, after updating to python 3.6.0.我运行 kali linux-Rolling 并且在更新到 python 3.6.0 后尝试在终端中运行 cupp.py 时遇到了这个问题。 After some research and trial I found that changing ConfigParser to configparser worked for me but then I came across another issue.经过一些研究和试验,我发现将ConfigParser更改为configparser对我configparser ,但后来我遇到了另一个问题。

config = configparser.configparser() AttributeError: module 'configparser' has no attribute 'configparser'

After a bit more research I realised that for python 3 ConfigParser is changed to configparser but note that it has an attribute ConfigParser() .经过更多的研究,我意识到对于 python 3 ConfigParser已更改为configparser但请注意它有一个属性ConfigParser()

I was getting the same error on Mac OS 10, Python 3.7.6 & Django 2.2.7.我在 Mac OS 10、Python 3.7.6 和 Django 2.2.7 上遇到了同样的错误。 I want to use this opportunity to share what worked for me after trying out numerous solutions.在尝试了多种解决方案后,我想借此机会分享对我有用的方法。

Steps脚步

  1. Installed Connector/Python 8.0.20 for Mac OS from link链接为 Mac OS 安装了连接器/Python 8.0.20

  2. Copy current dependencies into requirements.txt file, deactivated the current virtual env, and deleted it using;将当前依赖复制到 requirements.txt 文件中,停用当前虚拟环境,并使用删除它;

    create the file if not already created with;如果尚未创建,则创建文件; touch requirements.txt

    copy dependency to file;将依赖复制到文件; python -m pip3 freeze > requirements.txt

    deactivate and delete current virtual env;停用和删除当前的虚拟环境; deactivate && rm -rf <virtual-env-name>

  3. Created another virtual env and activated it using;创建另一个虚拟环境并使用; python -m venv <virtual-env-name> && source <virtual-env-name>/bin/activate

  4. Install previous dependencies using;使用安装以前的依赖项; python -m pip3 install -r requirements.txt

I still have this issue, so I go to /usr/lib/python3.8 and type as sudoer:我仍然有这个问题,所以我转到 /usr/lib/python3.8 并键入 sudoer:

cp configparser.py ConfigParser.py

You may have another python version than 3.8.您可能有 3.8 以外的另一个 Python 版本。

对我来说,以下命令有效:

sudo python3 -m pip install mysql-connector

Additional info:附加信息:

Python 2x蟒蛇 2x

import ConfigParser

Python 3x蟒蛇 3x

import configparser
brew install mysql

pip3 install mysql仍然给出“ configparser错误”,我不知道为什么现在还没有纠正。

I am trying to run tryton server for GNU health with this command './trytond --verbose' but getting this error : 我正在尝试使用此命令'./trytond --verbose'为GNU健康运行tryton服务器,但出现此错误:

Traceback (most recent call last): File "./trytond", line 16, in from trytond.config import config, split_netloc File "/home/health/gnuhealth/tryton/server/trytond-4.6.21/trytond/config.py", line 4, in import configparser ImportError: No module named configparser 追溯(最近一次通话最后一次):从trytond.config导入配置进入文件“ ./trytond”,第16行,split_netloc文件“ /home/health/gnuhealth/tryton/server/trytond-4.6.21/trytond/config”。 py”,第4行,在导入configparser ImportError中:没有名为configparser的模块

What can I do to run the server? 我该怎么办才能运行服务器? Please help. 请帮忙。

Following @MaciejNg I tried making a copy:在@MaciejNg 之后,我尝试复制:

sudo cp ./env/lib/python3.8/site-packages/configparser.py ./env/lib/python3.8/site-packages/ConfigParser.py

Which didn't work哪个不起作用

configparser.py and ConfigParser.py are identical

So I renamed the file:所以我重命名了文件:

sudo mv ./env/lib/python3.8/site-packages/configparser.py ./env/lib/python3.8/site-packages/ConfigParser.py

Kindly to see what is /usr/bin/python pointing to请看看/usr/bin/python指向什么

if it is pointing to python3 or higher change to python2.7如果它指向python3 or higher更改为python2.7

This should solve the issue.这应该可以解决问题。

I was getting install error for all the python packages.我收到所有 python 包的安装错误。 Abe Karplus's solution & discussion gave me the hint as to what could be the problem. Abe Karplus 的解决方案和讨论给了我可能是什么问题的提示。 Then I recalled that I had manually changed the /usr/bin/python from python2.7 to /usr/bin/python3.5 , which actually was causing the issue.然后我想起我手动将/usr/bin/pythonpython2.7更改为/usr/bin/python3.5 ,这实际上是导致问题的原因。 Once I reverted the same.一旦我reverted了同样的。 It got solved.它得到了解决。

这对我有用

cp /usr/local/lib/python3.5/configparser.py /usr/local/lib/python3.5/ConfigParser.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM