importerror watson_developer_cloud Python - python

I have pip installed watson-developer-cloud, on python v3.5
I am simply trying to run one of the example codes: alchemy_data_news_v1.py
Link:https://github.com/watson-developer-cloud/python-sdk/tree/master/examples
import json
from watson_developer_cloud import AlchemyLanguageV1
alchemy_data_news = AlchemyDataNewsV1(api_key='api-key')
results = alchemy_data_news.get_news_documents(start='now-7d', end='now',
time_slice='12h')
print(json.dumps(results, indent=2))
results = alchemy_data_news.get_news_documents(
start='1453334400',
end='1454022000',
return_fields=['enriched.url.title',
'enriched.url.url',
'enriched.url.author',
'enriched.url.publicationDate'],
query_fields={
'q.enriched.url.enrichedTitle.entities.entity':
'|text=IBM,type=company|'})
print(json.dumps(results, indent=2))
I have also tried utilizing my own personal api-key and the result is the same:
File "c:\users\Joseph Sansevero\desktop\test.py", line 2, in
watson_developer_cloud import AlchemyLanguageV1 ImportError: No module
named watson_developer_cloud

Change your import statement to
from watson_developer_cloud import AlchemyLanguageV1
Alchemy language is a different api than AlchemyNews.
Head over to https://www.ibm.com/watson/developercloud/alchemydata-news/api/v1/?python#methods and you'll see the example has AlchemyNews imported.
Also make sure you install these packages using before running your code.

Related

ModuleNotFoundError: No module named 'haystack.document_store.elasticsearch'; 'haystack.document_store' is not a package

I am building a Q&A webapp with elasticsearch. I have just successfully installed farm-haystack==1.3.0 and farm with requirements.txt.
Now when I import the library, it pops up an error:
from haystack.document_store.elasticsearch import ElasticsearchDocumentStore
# from haystack.document_stores.elasticsearch import ElasticsearchDocumentStore
document_store = ElasticsearchDocumentStore(host="localhost", username="", password="", index="document")
error:
ModuleNotFoundError: No module named 'haystack.document_store.elasticsearch'; 'haystack.document_store' is not a package
It would be great if anyone can tell me how to fix it. I have made sure my haystack.document_store.elasticsearch is not document_store's'.
According to the haystack documentation https://haystack.deepset.ai/overview/migration, since version 1.0, ElasticsearchDocumentStore can be directly accessed from haystack.document_stores.
from haystack.document_stores import ElasticsearchDocumentStore
Also note that the plural of document_store's' is necessary for versions after 1.0 as well. That is the "s" is needed at the end of "document_stores".

ModuleNotFoundError: No module named 'SessionState

I am trying to make use of the streamlit SessionState, when I import SessionState. I get the following error: ModuleNotFoundError: No module named 'SessionState'
when using he SessionState
Here is a snipnet of my code:
from multiprocessing import Process
import streamlit as st
import SessionState
import time
import os
import signal
st.sidebar.title("Controls")
start = st.sidebar.button("Start")
stop = st.sidebar.button("Stop")
state = SessionState.get(pid=None)
Has anyone encountered this and how did you fix it? There are no resources online
https://docs.streamlit.io/en/stable/changelog.html?highlight=SessionState#version-0-54-0
Seems like you have to download this gist and put it into your project in order to use SessionState

Cannot find reference 'discovery' in '__init__.py' Google custom search API

while following this tutorial : https://www.youtube.com/watch?v=IBhdLRheKyM
I ran into a problem and i've tried several things like replacing apiclient.discovery with googleapiclient.discovery.
Didn't work.
type(resource) and len(result['items'] return void.
PyCharm states that there an unresolved reference.
Cannot find reference 'discovery' in '__init__.py'
i installed the module using the pip tool. (pip install google-api-python-client) which prompted no issues whatsoever.
Example code:
from apiclient.discovery import build
api_key = "api_key"
resource = build("customsearch", 'v1', developerKey=api_key).cse()
result = resource.list(q='alfa romeo', cx="searchengineID").execute()
type(resource)
len(result['items'])
>> Process finished with exit code 0
edited out the api_key and cx as it's sensitive information.
'init.py' source:
"""Retain apiclient as an alias for googleapiclient."""
from six import iteritems
import googleapiclient
from googleapiclient import channel
from googleapiclient import discovery
from googleapiclient import errors
from googleapiclient import http
from googleapiclient import mimeparse
from googleapiclient import model
try:
from googleapiclient import sample_tools
except ImportError:
# Silently ignore, because the vast majority of consumers won't use it and
# it has deep dependence on oauth2client, an optional dependency.
sample_tools = None
from googleapiclient import schema
__version__ = googleapiclient.__version__
_SUBMODULES = {
'channel': channel,
'discovery': discovery,
'errors': errors,
'http': http,
'mimeparse': mimeparse,
'model': model,
'sample_tools': sample_tools,
'schema': schema,
}
import sys
for module_name, module in iteritems(_SUBMODULES):
sys.modules['apiclient.%s' % module_name] = module
Hmm. Pasting that exact code (replacing the API key and CX) worked for me into a shell worked for me in both Python 2.7 and 3.5.
Normally if apiclient.discovery.build() fails, it will raise an exception, rather than returning an invalid value.
What do you mean when you say "type(resource) and len(result['items'] return void"? As void isn't a type in Python, did you mean <class 'NoneType'>?
Also, what does type(result) return?

Python cannot import from another file name not defined

I am trying to import a custom function from another python file but keep getting an error NameError: name 'testme' is not defined. I confirmed that I am importing the file correctly according to this SO post and that the function is top level. What else can I try to fix this?
My main python file is:
import sys
import dbconn
#from dbconn import testme #<----did not work
dev=True
if(dev):
categId='528'
pollIds=[529,530,531]
else:
categId=str(sys.argv[1])
pollIds=[529,530,531]
df=testme(categIds)#callServer(categId,pollIds)
df
if(not categId.isdigit):
print('categ id fail. expected digit got: '+categId)
.....
and dbconn.py:
import pymysql #pip3 install PyMySQL
import pandas as pd
from scipy.stats.stats import pearsonr
from scipy import stats
def testme(categIds):
try:
df=categIds
except Exception as e:
print("broke")
return categIds
Not sure if it makes a difference but I am running the main python file from within a Jupyter notebook, and have a compiled version of dbconn.py in the same directory
In response to the suggestions I tried:
df=dbconn.testme(categIds)
got the error:
module 'dbconn' has no attribute 'testme'
You Have to follow these fox exact import
1)import <package>
2)import <module>
3)from <package> import <module or subpackage or object>
4)from <module> import <object>
in your case, you have tried
from dbconn import testme
you have to use only packages in from section and module in import section
like >>
from testme import dbconn

import ansible.module_utils in 2.2.1.0 as part of inventory module

Importing UTILS classes into Inventory - can it be done?
I have created a custom LDAP data importer as part of creating my inventory class. The LDAP schema we have wasn't similar enough to the LDAP plugin provided in samples.
My class is called ldapDataModule; the class is in:
/home/agt/ansible/agt_module_utils/ldapDataModule.py
My "$HOME/.ansible.cfg" file has the following:
module_utils = /home/agt/ansible/agt_module_utils
When running my Ansible inventory module, I get the following output:
ansible ecomtest37 -m ping
ERROR! Attempted to execute "/sites/utils/local/ansible/hosts" as
inventory script: Inventory script (/sites/utils/local/ansible/hosts) had
an execution error: Traceback (most recent call last):
File "/sites/utils/local/ansible/hosts", line 22, in
from ansible.module_utils import ldapDataModule
ImportError: No module named module.utils
The include statement inside hosts appears like this:
import copy
import ldap
import re
import sys
import operator
import os
import argparse
import datetime
import os.path
try:
import json
except:
import simplejson as json
from ansible.module_utils import ldapDataModule
class agtInventory(object):
RECOMENDATIONS?
I was able to do the following as a "work around". I'd still like to hear from Ansible guru's on proper use of "module_utils" variable from ansible.cfg
sys.path.insert(0, '/home/agt/ansible/agt_module_utils')
from ldapDataModule import ldapDataModule

Categories