tinify-python
tinify-python copied to clipboard
AttributeError: module 'flask.sessions' has no attribute 'Session'
I'm using tinify with flask to compress images after uploading them, but I get the following error: AttributeError: module 'flask.sessions' has no attribute 'Session'
Here's the full trace:
Traceback (most recent call last):
File "D:\projects\docspert\venv\Lib\site-packages\flask\app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "D:\projects\docspert\venv\Lib\site-packages\flask\app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "D:\projects\docspert\venv\Lib\site-packages\flask\app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "D:\projects\docspert\venv\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "D:\projects\docspert\venv\Lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "D:\projects\docspert\venv\Lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "D:\projects\docspert\venv\Lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "D:\projects\docspert\venv\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "D:\projects\docspert\venv\Lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "D:\projects\docspert\venv\Lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "D:\projects\docspert\admin\core\auth_sys.py", line 93, in f
return function(*args, **kwargs)
File "D:\projects\docspert\admin\url_rules\affiliates\webinars\__root__.py", line 150, in post
'error': str(error)
File "D:\projects\docspert\venv\Lib\site-packages\tinify\__init__.py", line 79, in from_file
return Source.from_file(path)
File "D:\projects\docspert\venv\Lib\site-packages\tinify\source.py", line 14, in from_file
return cls._shrink(f.read())
File "D:\projects\docspert\venv\Lib\site-packages\tinify\source.py", line 26, in _shrink
response = tinify.get_client().request('POST', '/shrink', obj)
File "D:\projects\docspert\venv\Lib\site-packages\tinify\__init__.py", line 60, in get_client
self._client = Client(self._key, self._app_identifier, self._proxy)
File "D:\projects\docspert\venv\Lib\site-packages\tinify\client.py", line 25, in __init__
self.session = requests.sessions.Session()
AttributeError: module 'flask.sessions' has no attribute 'Session'
Here's the code:
file.save(os.path.join(app.config['FILES_WEBINARS_POSTERS'], postersrc))
source = tinify.from_file(os.path.join(app.config['FILES_WEBINARS_POSTERS'], postersrc)) # the error is from this line
source.to_file(os.path.join(app.config['FILES_WEBINARS_POSTERS'], "optimized.jpg"))
The file uploading is working properly and files get uploaded but the optimized images don't get uploaded.
How can I fix this?
Hi there,
I don't see any obvious issues with the code.
Are you able to do a simple compression with a hardcoded path to an image?
source = tinify.from_file("unoptimized.jpg")
source.to_file("optimized.jpg")
Does this work in your app (using an actual path to an image) or does that fail as well?
Would it be possible for you to send more code so we can have a closer look?
Hi Simon,
I used the simple hardcoded compression code like this (same as your code):
source = tinify.from_file('unpotimized.png')
source.to_file('optimized.png')
I also made sure that unpotimized.png
is in my current working directory by checking os.getcwd()
, and I still had the same error again: AttributeError: module 'flask.sessions' has no attribute 'Session'
.
This hardcoded code is totally separate from my file uploading code now.
Here's the full code:
if 'postersrc' in request.files and request.files['postersrc'].filename != '':
file = request.files['postersrc']
if secure_filename(file.filename):
postersrc = secure_filename(file.filename)
if os.path.splitext(postersrc)[1].lower() in ALLOWED_EXT:
postersrc = str(int(time.time())) + '_' + postersrc
file.save(os.path.join(app.config['FILES_WEBINARS_POSTERS'], postersrc))
source = tinify.from_file('unpotimized.png')
source.to_file('optimized.png')
else:
return json.dumps({
'status': '0',
'title': 'An error has occured',
'message': 'Please upload an image.',
})
else:
return json.dumps({
'status': '0',
'title': 'An error has occured',
'message': 'Your request did not complete due to invalid file.',
})
Hi,
The Tinify Python module uses the requests
module to communicate with the API over https. In your implementation it looks like there is a conflict between the requests
module and Flask itself. In the trace you posted it shows that the call to requests.sessions.Session()
resolves requests.sessions
to flask.sessions
instead.
We unfortunately have no experience with Flask, so it's difficult for us to further help you. The few lines of Tinify code you're using are perfectly fine.
Have you already tried to run the Tinify code outside of Flask? Any example on https://tinypng.com/developers/reference/python would do. If that works then we can be sure that the conflict is either caused by Flask or your custom implementation in Flask.
Closing this issue due to inactivity.