9

I'm trying to run db_create.py in Flask, and am getting the following error:

from six.moves.configparser import ConfigParser
ImportError: No module named configparser

Even after a successfull pip install, the same error code comes up.

I'm seeing solutions targeting Python 3, but not lower.

fstopzero
  • 1,073
  • 2
  • 10
  • 24
  • 1
    In Python<3 the `configparser` module is called `ConfigParser` (see [here](https://docs.python.org/2/library/configparser.html#module-ConfigParser)) so I'm guessing the crux of the problem is the db_create.py script is meant only to work with Python3. Can you share where you are getting that script from? – lemonhead Jul 27 '15 at 20:13
  • 1
    also, FWIW I ran `pip install six` in my py2.7 installation and then in my python-2.7 shell, I'm able to run: `from six.moves.configparser import ConfigParser` – lemonhead Jul 27 '15 at 20:15
  • Six is installed, and the configparser package is installed as well, but when I try your suggestion, it tells me that there's no module named configparser – fstopzero Jul 27 '15 at 20:37
  • And, in the shell, print configparser.__file__ gives me the following path: /Library/Python/2.7/site-packages/configparser.pyc, which is what is expected. – fstopzero Jul 27 '15 at 20:41
  • 2
    hmm... actually I'd expect it to be the camelcase *ConfigParser* for py2.7, e.g. for me it's '/usr/lib/python2.7/ConfigParser.pyc'. Did you `pip install ConfigParser`? – lemonhead Jul 27 '15 at 20:42
  • I did. And now this is weird - the six.__file__ and ConfigParser.__file__ are in the same packages folder ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python'), while the configparser is in a different folder: '/Library/Python/2.7/site-packages/configparser.pyc'. However, the ImportError is over configparser - not camelcased. – fstopzero Jul 27 '15 at 20:48
  • ah ok, I see where the error is happening now. It's very weird, because the `six.moves` should dynamically create a descriptor for configparser pointing to ConfigParser when run in python2.7. Can you post the results of `python -c "import six; print six.PY2; print [(a.name,a.mod) for a in six._moved_attributes if a.name=='configparser']"`? – lemonhead Jul 27 '15 at 21:06
  • This is the result: [('configparser', 'ConfigParser')] – fstopzero Jul 27 '15 at 21:13
  • okay then, now try: `python -c "import six; mod = [a for a in six._moved_attributes if a.name=='configparser'][0]; print mod._resolve()"` – lemonhead Jul 27 '15 at 21:27
  • Are the quotes necessary? I'm getting an invalid syntax with them. Also, thanks so much for your time on this. – fstopzero Jul 27 '15 at 21:33
  • yep, they are if you are trying to run this from the command line. You could do the same thing in an interactive python session without the outermost quotes – lemonhead Jul 27 '15 at 21:34
  • hmm...that seems right. And you're sure `python -c "import six; print six.moves.configparser"` definitely does not work? Can you post the whole traceback you get? – lemonhead Jul 27 '15 at 21:38
  • Result (this time from command line): – fstopzero Jul 27 '15 at 21:40
  • oh... so then what happens if you run `python -c "from six.moves.configparser import ConfigParser; print ConfigParser"` – lemonhead Jul 27 '15 at 21:41
  • Traceback (most recent call last): File "", line 1, in ImportError: No module named configparser – fstopzero Jul 27 '15 at 21:42
  • very befuddling... okay, try: `python -c "import six.moves.configparser; print(dir(six.moves.configparser))"` – lemonhead Jul 27 '15 at 21:46
  • Traceback (most recent call last): File "", line 1, in ImportError: No module named configparser – fstopzero Jul 27 '15 at 21:47
  • oh wait a sec, are you running this from a directory that has a six/moves/ structure by any chance? – lemonhead Jul 27 '15 at 21:47
  • I'm not sure what that means. I'm just running Flask, afaik, with the standard structure. – fstopzero Jul 27 '15 at 21:48
  • okay, last try: `python -c "import six; print dir(six.moves)"` – lemonhead Jul 27 '15 at 21:50
  • ['__doc__', '__name__'] – fstopzero Jul 27 '15 at 21:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84410/discussion-between-lemonhead-and-fstopzero). – lemonhead Jul 27 '15 at 21:53
  • Well, for whatever reason when I just scrapped it and started over, everything works. Who knows what that strange error was. Thank you for your help. – fstopzero Jul 28 '15 at 18:07

2 Answers2

6

For anyone following along, this was likely caused by an old (broken) version of the six module, e.g. see https://github.com/Parsely/streamparse/issues/113, for instance

which caused six.moves to be almost empty (contained no configparser module)

The fix was to upgrade the version of six used.

lemonhead
  • 5,328
  • 1
  • 13
  • 25
4

I am using CENTOS RHEL 7 with Python 2.7.5 & pip version 8.1.2 and and fixed it doing:

cd /home/user/
sudo pip install configparser

And it was done for me. But Previously i had installed:

cd /home/user/
pip install --user pytz requests tqdm tzlocal python-dateutil

No problems at all.

to know your python version and to know configparser check up:

python --version

python -c 'import six; print(six.__version__)'

python -c 'import six.moves; print(dir(six.moves))'

I had configparser installed previously but I was not working so I think you must install all the dependencies and libs of python pip on your /home/user_directory

Dharman
  • 30,962
  • 25
  • 85
  • 135