AttributeError: 'module' object has no attribute 'TwilioRestClient' - python

Trying to integrate django 1.10 application with twilio 6.0.0 and django-twilio==0.8.0
referring link
showing Error in my shell..
7 def send_twilio_message(to_number, body):
----> 8 client = twilio.rest.TwilioRestClient(
9 settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
10
AttributeError: 'module' object has no attribute 'TwilioRestClient'

Updated with the solution:
version twilio==6.0.0 (current one ) has different directory structure,so it effect importing structure Below is the updated importing structure ..
from django.conf import settings
import twilio
import twilio.rest
from twilio.rest import Client
def send_twilio_message(to_number, body):
client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
return client.api.account.messages.create(
body=body,
to=to_number,
from_=settings.PHONE_NUMBER
)

The tutorial you are following was written for an older version of the twilio sdk than 6.0.
You could try to look for a newer tutorial to follow, or you could try to adjust the tutorial. The migration guide might help with this.
You final option is to install an older, unsupported version of the twilio library that works with the tutorial, e.g.
pip install twilio==5.7

Related

AttributeError: 'ElasticLoadBalancingv2' object has no attribute 'set_desired_capacity'

import boto3
import json
import time
client = boto3.client('elbv2')
desired_capacity=8
client.set_desired_capacity(
AutoScalingGroupName='Test-Web',
DesiredCapacity=desired_capacity,
HonorCooldown=True)
and
boto3==1.7.1
When I run this script I get a
File "deploy_staging_web.py", line 6, in <module>
client.set_desired_capacity(
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 601, in __getattr__
self.__class__.__name__, item)
AttributeError: 'ElasticLoadBalancingv2' object has no attribute 'set_desired_capacity'
I intended to use python to scale aws instances up and down.
I'm not inside any virtual environment at the moment.
why is it being thrown, and how do I get across it?
It is even mentioned here on the official documentation : https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/autoscaling.html#AutoScaling.Client.set_desired_capacity
The official document is for the latest version, not your too old version. Upgrade your boto3 package to the latest. The most recent version is 1.9.243.
The problem turns out to be a silly one.
boto3 has moved around the various functions.
set_desired_capacity is no longer part of 'elbv2' .
It is part of 'autoscaling' https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/autoscaling.html#AutoScaling.Client.set_desired_capacity
While 'describe_target_health' is still part of the former https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/elbv2.html?highlight=elb#ElasticLoadBalancingv2.Client.describe_target_health.
Updating
client = boto3.client('elbv2')
to
client = boto3.client('autoscaling')
has solved my problem.

ImportError: cannot import name types

I'm trying the app I cloned from following Github.
https://github.com/sfujiwara/qa-system-sample.git
(This app uses python flask, Google Natural Language API and so on.)
But, I cannot run this on Google App Engine.Following Error occurs.
Error:
ImportError: cannot import name types
at <module> (/base/data/home/apps/b~qa-system-sample2/20171223t122211.406411375873194303/lib/google/cloud/language_v1/__init__.py:17)
at <module> (/base/data/home/apps/b~qa-system-sample2/20171223t122211.406411375873194303/lib/google/cloud/language.py:17)
at <module> (/base/data/home/apps/b~qa-system-sample2/20171223t122211.406411375873194303/factoid.py:5)
at <module> (/base/data/home/apps/b~qa-system-sample2/20171223t122211.406411375873194303/main.py:9)
at LoadObject (/base/alloc/tmpfs/dynamic_runtimes/python27/54c5883f70296ec8_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py:85)
at _LoadHandler (/base/alloc/tmpfs/dynamic_runtimes/python27/54c5883f70296ec8_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py:299)
at Handle (/base/alloc/tmpfs/dynamic_runtimes/python27/54c5883f70296ec8_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py:240)
This occurs at from google.cloud.language_v1 import typesin lib/google/cloud/language_v1/_ _init__.py.
Although, There is types.py in /lib/google/cloud/language_v1.
I'm a beginner of python, so I couldn't find out a reason of this error.Please tell me the way to resolve this error.Thanks in advance.
I added enum into the library. It fixes this issue (although another import issue occurred)
libraries:
- name: enum
version: latest

AttributeError: module 'elasticsearch' has no attribute 'helpers'

I can't access the helpers of elasticsearch while trying to push data in bulk mode. Package installed :
pip freeze
elasticsearch==5.3.0
When in my code I try to call this method :
import elasticsearch
client = elasticsearch.Elasticsearch([config['ES']['host']],
connection_class=elasticsearch.RequestsHttpConnection,
http_auth=(config['ES']['userName'], config['ES']['password']),
port=int(config['ES']['hostPort']),
use_ssl=True,
verify_certs=False)
elasticsearch.helpers.bulk(client, body)
I got the following error :
AttributeError: module 'elasticsearch' has no attribute 'helpers'
I'm using Python 3.5.1 and I have no problem reading data from my es cluster (without the use of helpers of course)
As #Jon Clements♦ correctly said,
import elasticsearch.helpers
Is the correct way to import (therefore also the following works)
from elasticsearch.helpers import scan
Enjoy

AttributeError for SUDS in GAE

I am trying to include the SUDS library in a Python project through Google App Engine.
My code tries the following:
from suds.client import Client
from suds.wsse import *
And, once I've deployed on GAE, I encounter the following error:
File ".../myfile.py", line 13, in <module>
from suds.client import Client
File ".../suds/__init__.py", line 154, in <module>
import client
File ".../suds/client.py", line 25, in <module>
import suds.metrics as metrics
AttributeError: 'module' object has no attribute 'suds'
I've been looking around for a little while, and it seems like SUDS is workable with GAE. I added the fixes outlined here, but that doesn't seem to be the problem. It seems like App Engine doesn't even get to that point.
Any info or suggestions?
I'm using Python 2.7 and SUDS 0.4.
Have you tried doing a simple import suds.client? Also make sure you include the suds folder in your application as App Engine doesn't include it by default. I hope that helps.

access twitter HomeTimeline of user

I am trying to develop a gtk3 desktop application using python to perform the basic twitter functions like accessing the home timeline of a user, making tweets etc.
I am using python-twitter library, but am unable to find the API call for the purpose. I checked and saw there were a few patches , but they dont seem to work. the rest of the functions I am able to accomplish using the library.
I need help!!!
[edit]
this is the error i am facing when i tried using a fork of the python-twitter library, as given on: http://github.com/jaytaylor/python-twitter-api
Error:
>>api.getUserTimeline('gaurav_sood91')
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "twitter.py", line 2646, in getUserTimeline
self._checkForTwitterError(data)
File "twitter.py", line 3861, in _checkForTwitterError
if data.has_key('next_cursor'):
AttributeError: 'list' object has no attribute 'has_key'
Using the python-twitter module from code.google.com, documentation here.
Accessing user timelines:
import twitter
api = twitter.Api()
statuses = api.GetUserTimeline('#gaurav_sood91')
print [s.text for s in statuses]
Posting tweets:
import twitter
api = twitter.Api(consumer_key='consumer_key',
consumer_secret='consumer_secret',
access_token_key='access_token',
access_token_secret='access_token_secret')
status = api.PostUpdate('This is my update text.')
Edit for applying GetHomeTimeline patch:
Disclaimer: I'm on Windows, so you may need to change these steps a bit.
Download python-twitter
Extract to folder
Download 0002-Support-for-home-timeline.patch file from issue 152
Copy/move patch file to root of extracted python-twitter directory (there should be a file named twitter.py in this dir)
Run command: patch twitter.py 0002-Support-for-home-timeline.patch, you should get a message that patch succeeded
In same directory, run command: python setup.py install
Run interactive python shell: import twitter, dir(twitter.Api)
You should see the GetHomeTimeline method listed.
Update for GetHomeTimeline:
Found patch in issue 152 that works well using OAuth and JSON parse method that is now part of Status class. Sample code:
import twitter
api = twitter.Api(consumer_key='consumer_key',
consumer_secret='consumer_secret',
access_token_key='access_token',
access_token_secret='access_token_secret')
statuses = api.GetHomeTimeline()
print [s.text for s in statuses]

Categories