Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ImportError: cannot import name __version__

I'm trying to use requests and requests_oauthlib, and right now am just trying the dead simple Twitter verify credentials example they use in the documentation for requests_oauthlib to confirm I've got the basics working. I did a "pip install requests requests_oauthlib" to get the modules. In a terminal window I can "import requests" no problem but when I try "import requests_oauthlib" I get this:

>>> import requests_oauthlib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/requests_oauthlib/__init__.py", line 1, in
  <module>
    from .oauth1_auth import OAuth1
  File "/usr/lib/python2.7/site-packages/requests_oauthlib/oauth1_auth.py", line 10, in  
  <module>
    from requests.utils import to_native_string
  File "/usr/lib/python2.7/site-packages/requests/utils.py", line 23, in <module>
    from . import __version__
ImportError: cannot import name __version__

Line 23 of utils.py does indeed look like this:

from . import __version__

I'm using Python 2.7.5 on Fedora and am currently banging my head against this wall after multiple tries at getting it working, any help would be greatly appreciated...

like image 785
hyssop Avatar asked Aug 04 '14 14:08

hyssop


2 Answers

Check the __init__.py at root dir. openpyxl read these information from .constrants.json file. However, PyInstaller somehow can't make it right. I would you write a __version__.py file by yourself and replace them in the __init__.py.

Another simpler way is to change __init__.py like this:

import json
import os


# Modified to make it work in PyInstaller
#try:
#    here = os.path.abspath(os.path.dirname(__file__))
#    src_file = os.path.join(here, ".constants.json")
#    with open(src_file) as src:
#        constants = json.load(src)
#        __author__ = constants['__author__']
#        __author_email__ = constants["__author_email__"]
#        __license__ = constants["__license__"]
#        __maintainer_email__ = constants["__maintainer_email__"]
#        __url__ = constants["__url__"]
#        __version__ = constants["__version__"]
#except IOError:
#    # packaged
#    pass

__author__ = 'See AUTHORS'
__author_email__ = '[email protected]'
__license__ = 'MIT/Expat'
__maintainer_email__ = '[email protected]'
__url__ = 'http://openpyxl.readthedocs.org'
__version__ = '2.4.0-a1'

"""Imports for the openpyxl package."""
from openpyxl.compat.numbers import NUMPY, PANDAS
from openpyxl.xml import LXML

from openpyxl.workbook import Workbook
from openpyxl.reader.excel import load_workbook

print('You are using embedded openpyxl... 2.4.0-a1 ...')
like image 138
Dipterix Avatar answered Sep 21 '22 20:09

Dipterix


I used the openpyxl in my project,when I make the exe by py2exe,compile is ok but when I run the compiled exe I met the same problem.

ImportError: cannot import name __version__

Try to modify the init.py in the root of the openpyxl paceage folder, don't read the version from constants.json file,just write like __version__ = '2.4.1'. I solved by this way.

like image 44
sunday Avatar answered Sep 23 '22 20:09

sunday