Import stackapi module recognization - python

I installed stackapi using pip successsfully
I try to run the following code:
from StackAPI import StackAPI, StackAPIError
try:
SITE = StackAPI('stackoverflow', key=SETTINGS['xxxxxxx'])
SITE.max_pages=100
SITE.page_size=1000000000
post = SITE.fetch('posts', ids=[59115355, 2901002], sort='activity', order='desc')
except StackAPIError as e:
print(e.message)
But I receive this error:
from StackAPI import StackAPI, StackAPIError
ModuleNotFoundError: No module named 'StackAPI'

Edit: this was part of the issue, see further details in the comments below. Leaving the question since it has more information.
Mind the capitalization, the correct import line would be:
from stackapi import StackAPI, StackAPIError

The alias used in the import function should be outside the actual code as shown below:
from StackAPI import StackAPI, StackAPIError as e
try:
SITE = StackAPI('stackoverflow', key=SETTINGS['xxxxxxx'])
SITE.max_pages=100
SITE.page_size=1000000000
post = SITE.fetch('posts', ids=[59115355, 2901002], sort='activity', order='desc')
except:
print(e.message)

Related

How to fix this "ImportError: cannot import name 'TweepError' from ..."?

The line given below:
from tweepy import API, Stream, OAuthHandler, TweepError
generates ImportError such as:
ImportError: cannot import name 'TweepError' from 'tweepy'
I have tried: from tweepy.errors import TweepError ,however it stills generates error as:
ImportError: cannot import name 'TweepError' from 'tweepy.errors'.
What is it that I am missing here?
TweepError has been replaced with TweepyException since the 4.0.0 (see the changelog).
So use from tweepy.errors import TweepyException (or from tweepy import TweepyException).
And replace TweepError with TweepyException in your code (or with a more specific exception).

How to cascade try imports in a readable way

I want to try to import a list of JSON parsing libraries in python, with precedence for item in the order of which they're tried. If I have the following json libraries ajson, bjson, ... I'd have to write something like
try:
import ajson as json
except ImportError:
try:
import bjson as json
except ImportError:
try:
import cjson as json
except ImportError:
...
which is very unreadable. Is there a better way to do this like an if statement?
You could define a function that encapsulates this fallback behavior:
import importlib
def import_fallback(*modules):
for m in modules:
try:
return importlib.import_module(m)
except ImportError:
continue
raise ImportError("All fallback imports failed: {}".format(modules))
# You can use it like this
json = import_fallback("ajson", "bjson", "cjson")
Edit: There might be a downside from doing this. Some linters or static code analyzers might not know that you are importing these modules by running this function
Instead of producing exceptions and catching them with try and except, you can simply filter the module names with the importlib.util.find_spec function, which returns None if the given module name is not found, and then import the found module with importlib.import_module:
json = importlib.import_module(
next(filter(importlib.util.find_spec, ("ajson", "bjson", "cjson")))
)
You can iterate over the libraries you want to import using importlib
import importlib
libs = ["os", "sys", "json"]
for lib in libs:
try:
globals()[lib] = importlib.import_module(lib)
break #if you don't want to keep importing once you imported a library successfully
except:
pass
This seems to work:
for lib in ['ajson','bjson','cjson']:
try:
__import__(lib)
print('Imported ' + lib)
break
except ImportError:
continue

Get rid of urllib not defined error

Keep getting the same thing every time I try to run the program, urllib is not defined. How do I get rid of it?
from urllib.request import urlopen
from urllib.error import HTTPError
aname = 'http://website.com'
try:
htm = urlopen(aname + '/').read()
except urllib.error.HTTPError as e:
print(e)
Yes, I do have another problem I sure wish I could figure out as well, I can't get bs4 to install correctly, it keep trying to install to Python 2.7, which I don't have any 2.7 interpreter installed, only 3.4.3 is installed on the computer. I have a strange feeling that might be causing me some other issues as well with some some other programs.
Just because you have the line from urllib.error import HTTPError or the from urllib.request import urlopen doesn't mean that urrlib is available as a name in your script.
This is specified in the documentation for the import statement specifically in the section dealing with the from form; see the examples there to see what becomes available and what doesn't.
Therefore, when you except:
except urllib.error.HTTPError as e:
it fails when trying to find the name urrlib. Either just use HTTPError, as imported and bound to your namespace, in the except clause:
except HTTPError as e:
or, if you need the name for urllib available in your namespace, import urllib instead of from urllib.error import HTTPError and use the original except clause.
As for the installation issue, try using pip3 instead of pip.

Python: ImportError: cannot import name 'BeautifulSoup'

I have always used R and now trying to switch to Python.
I'm using Pycharm and found this error when running the following code:
import pandas as pd
from bs4 import BeautifulSoup
example1 = BeautifulSoup(train["review"][0],"lxml")
print (example1.get_text())
When I run it I have:
ImportError: cannot import name 'BeautifulSoup'
But I don't have any problem using the console. The rest of the code works fine both with the Run command/terminal and console.
Thank you for your help
Oh,I think you should check your file name or folder name.If there is a name that is already used in bs4 module,you will got ImportError.
Hope this helps.

How to Import Exceptions from Package (Openpyxl)?

I am trying to catch a sheet error exception for the package I am using (Openpyxl). I tried importing the exception like so from openpyxl.utils import SheetTitleException but I get the error "ImportError: cannot import name SheetTitleException". When I tried importing it just with from openpyxl.utils import *, I get the error NameError: global name 'SheetTitleException' is not defined.
I'm sure I'm importing it incorrectly, but I'm not sure where I'm going wrong.
Here is the documentation on exceptions for Openpyxl.
And here is the code I am using to catch the exception:
try:
bdws = bdwb[finalBDSheetName]
except SheetTitleException:
messageBox("Invalid sheet title. Check your sheet title and try again.")
return
The title of the page you linked to says "openpyxl.utils.exceptions".
Therefore you should be doing:
from openpyxl.utils.exceptions import SheetTitleException
If it's anything like other module exception handling that i've done it should be
from openpyxl.utils.exceptions import SheetTitleException
then to use it
except SheetTitleException as e:
# do something

Categories

Resources