Flask rest API throwing 500 error with 'Target WSGI script can't load as cannot be loaded as Python module' with below logs and in application .wsgi file all looks good.
from flask import Flask, request, g
File "/usr/local/lib/python2.7/site-packages/flask/__init__.py", line 17, in <module>
from werkzeug.exceptions import abort
File "/usr/local/lib/python2.7/site-packages/werkzeug/__init__.py", line 152, in <module>
__import__('werkzeug.exceptions')
File "/usr/local/lib/python2.7/site-packages/werkzeug/exceptions.py", line 71, in <module>
from werkzeug.wrappers import Response
File "/usr/local/lib/python2.7/site-packages/werkzeug/wrappers.py", line 26, in <module>
from werkzeug.http import HTTP_STATUS_CODES, \\
File "/usr/local/lib/python2.7/site-packages/werkzeug/http.py", line 28, in <module>
from urllib.request import parse_http_list as _parse_list_header
ImportError: No module named request
When I debug through werkzeug/http.py file I came across the below code and in python command line from urllib2 import parse_http_list is working fine but in code, it is going to exception block.
from urllib2 import parse_http_list as _parse_list_header
except ImportError: # pragma: no cover
from urllib.request import parse_http_list as _parse_list_header
The module urllib.request is for Python 3. You are using python 2.
From Werkzeug 2.0.0 changelogs:
Drop support for Python 2 and 3.5
Either downgrade Werkzeug or upgrade to latest python 3 release.
Related
I have problem with requests package. In past requests was working, but today for no reason it stopped working. I am just importing requests and it cause error.
Code:
import requests
Error:
Traceback (most recent call last):
File "d:\programovani\Python\SMSemail\test.py", line 1, in
import requests
File "C:\Users\vitek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests_init_.py", line 43, in
import urllib3
File "C:\Users\vitek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\urllib3_init_.py", line 11, in
from . import exceptions
File "C:\Users\vitek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\urllib3\exceptions.py", line 3, in
from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
File "C:\Users\vitek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\urllib3\packages\six.py", line 234, in create_module
return self.load_module(spec.name)
File "C:\Users\vitek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\urllib3\packages\six.py", line 209, in load_module
mod = mod._resolve()
File "C:\Users\vitek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\urllib3\packages\six.py", line 118, in _resolve
return _import_module(self.mod)
File "C:\Users\vitek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\urllib3\packages\six.py", line 87, in import_module
import(name)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\http\client.py", line 71, in
import email.parser
File "d:\programovani\Python\SMSemail\email.py", line 1, in
from requests import get
ImportError: cannot import name 'get' from partially initialized module 'requests' (most likely due to a circular import) (C:\Users\vitek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests_init.py)
Screenshot:
What I've tried:
import requests as re
Reinstalling package.
Note: File name is different
What is causing this error? Thank you.
The relevant parts of the traceback and my interpretation:
You're trying to run test.py. One of the imports in it is import requests:
Traceback (most recent call last):
File "d:\programovani\Python\SMSemail\test.py", line 1, in import requests
This results in a (normal) longer chain of imports. Somewhere along the line in the standard library's http.client module there's an import in line 71: import email.parser
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\http\client.py", line 71, in import email.parser
Now trouble starts, because you have a file email.py in the same folder as test.py:
File "d:\programovani\Python\SMSemail\email.py", line 1, in from requests import get ImportError: cannot import name 'get' from partially initialized module 'requests' (most likely due to a circular import)
This alone should be enough to derail the program. So I'm a bit puzzled about the error message you get. I pretty much got the same as you when I tried to reconstruct your situation (one file test.py with import requests and another one named email.py in the same folder) but it ended with:
ModuleNotFoundError: No module named 'email.parser'; 'email' is not a package
You have a from requests import get in email.py, so there is in fact a potential (unintended) circular import (program starts with importing requests and on the way to do that imports (parts of) request again -> circular):
File "d:\programovani\Python\SMSemail\email.py", line 1, in from requests import get
Solution: Renaming email.py should do the job, if I'm not mistaken.
This question already has answers here:
Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError
(2 answers)
Closed 6 years ago.
I have a problem, when try to use requests lib
import requests
r = requests.get('http://www.python.org/')
print(r)
After that, I got the following error
Traceback (most recent call last):
File "C:/Users/admin/Documents/alex/test.py", line 3, in <module>
import requests
File "C:\Program Files\python3\lib\site-packages\requests\__init__.py", line 53, in <module>
from .packages.urllib3.contrib import pyopenssl
File "C:\Program Files\python3\lib\site-packages\requests\packages\__init__.py", line 27, in <module>
from . import urllib3
File "C:\Program Files\python3\lib\site-packages\requests\packages\urllib3\__init__.py", line 8, in <module>
from .connectionpool import (
File "C:\Program Files\python3\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 35, in <module>
from .connection import (
File "C:\Program Files\python3\lib\site-packages\requests\packages\urllib3\connection.py", line 44, in <module>
from .util.ssl_ import (
File "C:\Program Files\python3\lib\site-packages\requests\packages\urllib3\util\__init__.py", line 20, in <module>
from .retry import Retry
File "C:\Program Files\python3\lib\site-packages\requests\packages\urllib3\util\retry.py", line 15, in <module>
log = logging.getLogger(__name__)
AttributeError: module 'logging' has no attribute 'getLogger'
I do not understand why is it at all. Please, help me.
Your problem is one of the following
Your installation of Python is terribly broken
When C:\Program Files\python3\lib\site-packages\requests\packages\urllib3\util\retry.py tries to import logging it imports wrong file. This may happen because
There is file called logging.py in the directory you are running your test.py from. In this case, you will need to rename it so that logging from Python library is imported, not yours.
There is file called logging.py in one of directories from Python Path and it gets found before the logging.py the module actually needs
To check what logging gets imported, write the following simple program
import logging
import os.path
print os.path.abspath(logging.__file__)
Whatever is printed is path to your logging file. If it is not along the line of ...\Python\\Python36\\lib\\logging\\__init__.py, the wrong file is imported and you got to replace/rename it
I have some python code, where i try to use third-party module (tweepy):
...
import tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
...
It's works well when i start it on the pc, but if i try to use this with google app engine, i get an error:
File "X:\courant\main.py", line 6, in <module>
import tweepy
File "X:\courant\lib\tweepy\__init__.py", line 14, in <module>
from tweepy.api import API
File "X:\courant\lib\tweepy\api.py", line 12, in <module>
from tweepy.binder import bind_api
File "X:\courant\lib\tweepy\binder.py", line 11, in <module>
import requests
File "X:\courant\lib\requests\__init__.py", line 58, in <module>
from . import utils
File "X:\courant\lib\requests\utils.py", line 26, in <module>
from .compat import parse_http_list as _parse_list_header
File "X:\courant\lib\requests\compat.py", line 42, in <module>
from .packages.urllib3.packages.ordered_dict import OrderedDict
File "X:\courant\lib\requests\packages\__init__.py", line 83, in load_module
raise ImportError("No module named '%s'" % (name,))
ImportError: No module named 'requests.packages.urllib3'
Tweepy is installed in /lib directory on GAE project and the link to /lib is added in code:
import site
import os.path
site.addsitedir(os.path.join(os.path.dirname(__file__), 'lib'))
urllib3 exists in lib\requests\packages\urllib3 but i still get this error:
ImportError: No module named 'requests.packages.urllib3'
What am i doing wrong? I have no idea..
Update Requests to >= v2.20.0
This is no longer an issue as of requests v2.20.0 which supports urllib3 v1.24 - fixing this issue.
If, like me, you installed Rasbian (or whatever OS you're using) and it did not install the latest version of requests run
pip install requests -U
to fix the issue
I solved this issue by adding urllib3 to /lib (instead of using the one that comes with requests) and changing the line in compat.py from:
from .packages.urllib3.packages.ordered_dict import OrderedDict
to:
from urllib3.packages.ordered_dict import OrderedDict
After that there are other libraries that you'll need to install like oauth and ssh (should be added by editing app.yaml, as Google already ships it).
Trying to test with dev_appserver.py won't work, but it will work in production if you have billing enabled (it uses Sockets).
With Windows, I am following this Flask tutorial when I came across the following error:
C:\Users\Gregory Gundersen\Documents\Research\flask-test>python run.py
Traceback (most recent call last):
File "run.py", line 2, in <module>
from app import app
File "C:\Users\Gregory Gundersen\Documents\Research\flask-test\app\__init__.py
", line 1, in <module>
from flask import Flask
File "C:\Python27\lib\site-packages\flask\__init__.py", line 21, in <module>
from .app import Flask, Request, Response
File "C:\Python27\lib\site-packages\flask\app.py", line 26, in <module>
from . import json
File "C:\Python27\lib\site-packages\flask\json.py", line 25, in <module>
from itsdangerous import json as _json
File "C:\Python27\lib\site-packages\itsdangerous.py", line 14, in <module>
import hmac
File "C:\Python27\lib\hmac.py", line 8, in <module>
from operator import _compare_digest as compare_digest
ImportError: cannot import name _compare_digest
There are SO questions and answers, but they are for OS X/Django. Has anyone see or resolved this issue for PC/Flask before?
You appear to have half the changes made for issue 21306 (backporting hmac.compare_digest to 2.7).
Your hmac module has the lines:
from operator import _compare_digest as compare_digest
at the top, but your sys.version_info shows you are running Python 2.7.6; quoting our quick chat session:
Me: Next simple check:
import sys
print(sys.version_info)
You: sys.version_info(major=2, minor=7, micro=6, releaselevel='final', serial=0)
The hmac version you have is for Python 2.7.7 and up, however!
You'll want to reinstall your Python; download 2.7.8 and reinstall it to make sure you have the correct binary executable for your standard library files.
This can occur when you have updated python without rebuilding your virtualenv. In this case, just regenerate your virtualenv.
I'm attempting to run a Django app that uses Celery and I'm receiving the following error:
import djcelery
File "/Library/Python/2.7/site-packages/djcelery/__init__.py", line 25, in <module>
from celery import current_app as celery # noqa
File "/Library/Python/2.7/site-packages/celery/__compat__.py", line 135, in __getattr__
module = __import__(self._object_origins[name], None, None, [name])
File "/Library/Python/2.7/site-packages/celery/_state.py", line 18, in <module>
from celery.utils.threads import LocalStack
File "/Library/Python/2.7/site-packages/celery/utils/__init__.py", line 24, in <module>
from celery.exceptions import CPendingDeprecationWarning, CDeprecationWarning
File "/Library/Python/2.7/site-packages/celery/exceptions.py", line 15, in <module>
from billiard.exceptions import ( # noqa
ImportError: No module named exceptions
billiard is installed and the exceptions module does exist, and I'm able to import billiard.exceptions via Python's interactive prompt without issues. It seems that celery is failing to import billiard. Any ideas?