I'm trying to use IBM watson for sentiment analysis. but it is crashing on the import: from ibm_watson import NaturalLanguageUnderstandingV1
The whole code snippet is
import json
import constants from ibm_watson import NaturalLanguageUnderstandingV1 from ibm_cloud_sdk_core.authenticators
import IAMAuthenticator from ibm_watson.natural_language_understanding_v1 import Features, SentimentOptions
class SentimentAnalysis:
def __init__(self):
authenticator = IAMAuthenticator(constants.IBM_WATSON_KEY)
this.natural_language_understanding = NaturalLanguageUnderstandingV1(
version='2020-08-01',
authenticator=authenticator
)
this.natural_language_understanding.set_service_url(constants.IBM_WATSON_URL)
def analyse_sentiments(self, data):
response = this.natural_language_understanding.analyze(
text=data,
features=Features(sentiment=SentimentOptions())).get_result()
assert isinstance(response, object)
return response
My python version is Python 2.7.16
Installed IBM watson using pip install --upgrade "ibm-watson>=4.6.0"
The error I'm seeing is
/usr/bin/python /Users/rabbal1892/Desktop/DeepInsight/nextcontent-etl/scrapers/sentiment_analysis.py Traceback (most recent call last): File "/Users/rabbal1892/Desktop/DeepInsight/nextcontent-etl/scrapers/sentiment_analysis.py", line 3, in <module>
from ibm_watson import NaturalLanguageUnderstandingV1 File "/Users/rabbal1892/Library/Python/2.7/lib/python/site-packages/ibm_watson/__init__.py", line 16, in <module>
from ibm_cloud_sdk_core import IAMTokenManager, DetailedResponse, BaseService, ApiException File "/Users/rabbal1892/Library/Python/2.7/lib/python/site-packages/ibm_cloud_sdk_core/__init__.py", line 34, in <module>
from .base_service import BaseService File "/Users/rabbal1892/Library/Python/2.7/lib/python/site-packages/ibm_cloud_sdk_core/base_service.py", line 68
service_url: str = None,
^ SyntaxError: invalid syntax
I'll appreciate any help. Thanks.
The ibm-watson project description page mentions it's only tested on Python V3.x versions.
There is a lot of Python v3 syntax that is not compatible with Python v2.
Since you mention you have Python v2, you should Python v3 instead if you want to using the ibm-watson library as is, you will have to use Python v3.
Related
I have this code:
from google.cloud import speech_v1
from google.cloud.speech_v1 import enums
import os
import importlib
# Import the enums module from the google.cloud.speech_v1 package
enums = importlib.import_module("google.cloud.speech_v1.enums")
# Set your Google Cloud project and service account credentials
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "creds.json"
# Create a client for the Google Cloud Speech-to-Text API
stt_client = speech_v1.SpeechClient()
# Transcribe the audio data
response = stt_client.recognize(
audio=speech_v1.types.RecognitionAudio(uri="gs://focus-0/speech-to-text-sample.wav"),
config=speech_v1.types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.FLAC,
sample_rate_hertz=48000,
language_code="en-US"
)
)
# Print the transcribed text
for result in response.results:
print("Transcription: {}".format(result.alternatives[0].transcript))
When I run it, I get this:
Traceback (most recent call last):
File "/Users/dir/git/fp-scrapers/speech/1-STT.py", line 5, in <module>
from google.cloud.speech_v1 import enums
ImportError: cannot import name 'enums' from 'google.cloud.speech_v1' (/opt/homebrew/lib/python3.9/site-packages/google/cloud/speech_v1/__init__.py)
I have tried several ways to import enums, but none of them have worked.
Does anyone see what I'm doing wrong?
enums and types have been removed in the 2.x versions of the library
Mentioned in this github.Refer to this migration guide. You can refer to this quick start for setup instructions and an updated client library
Before:
from google.cloud import speech
encoding = speech.enums.RecognitionConfig.AudioEncoding.LINEAR16
audio = speech.types.RecognitionAudio(content=content)
After:
from google.cloud import speech
encoding = speech.RecognitionConfig.AudioEncoding.LINEAR16
audio = speech.RecognitionAudio(content=content)
I was trying to use Targetting search API from facebook business SDK API.
ImportError: No module named facebookads.adobjects.targetingsearch
Using Python 2.7.12
~
Was trying to execute this piece of code:
from facebookads.adobjects.targetingsearch import TargetingSearch
params = {
'q': 'un',
'type': 'adgeolocation',
'location_types': ['country'],
}
resp = TargetingSearch.search(params=params)
print(resp)
Actual result :
Traceback (most recent call last):
File "test.py", line 2, in <module>
from facebookads.adobjects.targetingsearch import TargetingSearch
ImportError: No module named facebookads.adobjects.targetingsearch
Facebook Marketing API docs a bit outdated. You should replace the import from:
from facebookads.adobjects.targetingsearch import TargetingSearch
to:
from facebook_business.adobjects.targetingsearch import TargetingSearch
Also, before requesting targeting data, you should initialize FacebookAdsApi with your generated access token:
from facebook_business.api import FacebookAdsApi
FacebookAdsApi.init(access_token=access_token)
I can't run my script I'm using python3 and I install pyrebase and his dependencies
I got this below exception when I try to run my script on linux ubuntu
Traceback (most recent call last):
File "scrapping2fb.py", line 9, in <module>
import pyrebase
File "/usr/local/lib/python3.4/dist-packages/pyrebase/__init__.py", line 1, in <module>
from .pyrebase import initialize_app
File "/usr/local/lib/python3.4/dist-packages/pyrebase/pyrebase.py", line 19, in <module>
from requests.packages.urllib3.contrib.appengine import is_appengine_sandbox
Can some one help me
Thank you
The script that i try to run
from urllib.request import urlopen ,URLError,HTTPError,Request
from socket import timeout
from bs4 import BeautifulSoup
from time import sleep
import mysql.connector
from datetime import datetime
import pyrebase
def is_exist_firebase_db_AR(siteName,title):#(siteName,title):
global config
global email
global password
firebase = pyrebase.initialize_app(config)
db=firebase.database()
auth = firebase.auth()
user = auth.sign_in_with_email_and_password(email, password)
all_items = db.child("items_ar").get(user['idToken'])
if(all_items.each() is not None):
for item in all_items.each():
if(siteName in item.val().get("nomSite") and title in item.val().get("titre")):
return 1
return 0
This is a problem with the pyrebase package.
Since commit 8e17600ef60de4faf632acb55d15cb3c178de9bb which went into v2.16.0, requests no longer bundle urllib3.
The package pyrebase is relying on this implementation detail, and, like all things that rely on implementation details eventually do, was broken.
I'm getting this error when trying to import a module from the Prov package.
Here is the contents of my file:
#!/usr/bin/env
import sys
egg_path='/Library/Python/2.7/site-packages/prov-1.5.0-py2.7.egg/prov'
sys.path.append(egg_path)
#... rest of code
import model as prov
def main():
# Create a new provenance document
d1 = ProvDocument() # d1 is now an empty provenance document
# Declaring namespaces for various prefixes used in the example
d1.add_namespace('now', 'http://www.provbook.org/nownews/')
d1.add_namespace('nowpeople', 'http://www.provbook.org/nownews/people/')
d1.add_namespace('bk', 'http://www.provbook.org/ns/#')
# Entity: now:employment-article-v1.html
e1 = d1.entity('now:employment-article-v1.html')
# Agent: nowpeople:Bob
d1.agent('nowpeople:Bob')
And here is the output:
Traceback (most recent call last):
File "prov.py", line 6, in <module>
import model as prov
File "/Library/Python/2.7/site-packages/prov-1.5.0-py2.7.egg/prov/model.py", line 25, in <module>
from prov import Error, serializers
ImportError: cannot import name Error
Any ideas or fixes? I installed Prov using easy_install prov.
You need to rename your module file prov.py. It prevents import of the third-party library because the module name conflicts.
Make sure prov.pyc is removed.
I found the error. The name of my file that I was trying to import into was also called prov.py . It was a circular dependency issue.
Thank you guys for such quick responses!
I was installing jira-python like written in the docs
$ pip install jira-python
but after installation I try to run the example:
from jira.client import JIRA
options = {
'server': 'https://jira.atlassian.com'
}
jira = JIRA(options)
projects = jira.projects()
keys = sorted([project.key for project in projects])[2:5]
issue = jira.issue('JRA-1330')
import re
atl_comments = [comment for comment in issue.fields.comment.comments
if re.search(r'#atlassian.com$', comment.author.emailAddress)]
jira.add_comment(issue, 'Comment text')
issue.update(summary="I'm different!", description='Changed the summary to be different.')
issue.delete()
getting the following error:
**Traceback (most recent call last):
File "jira.py", line 4, in <module>
from jira.client import JIRA
File "/home/ubuntu/jira.py", line 4, in <module>
from jira.client import JIRA
ImportError: No module named client**
Any idea about the problem here? I tried it also on an Amazon instance, but same problem...
seems like the reason was that my test file was named jira.py :) thanks for your help Inbar!