Get rid of urllib not defined error - python

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.

Related

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

Import stackapi module recognization

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)

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

Pylinting a python 2 fallback

I have a python module with a python 2 fallback set up through try/catch.
try:
from urllib.parse import urlencode
except ImportError:
from urlib import urlencode
When I pylint the file I get no name 'urlencode' in module 'urllib' and similar errors. Is there anyway to specify python 2 linting for a block, disable all linting for a block, or am I stuck hand squelching all errors?
I settled on a nicer way of doing this by disabling the linting errors at the beginning of a python 2 block, and then re enabling them at the end.
# pylint: disable=no-name-in-module, import-error
from urllib import urlencode
from urllib2 import urlopen
# pylint: enable=no-name-in-module, import-error

python urllib error

so I have this code:
def crawl(self, url):
data = urllib.request.urlopen(url)
print(data)
but then when I call the function, it returns
data = urllib.request.urlopen(url)
AttributeError: 'module' object has no attribute 'request'
what did I do wrong? I already imported urllib..
using python 3.1.3
In python3, urllib is a package with three modules request, response, and error for its respective purposes.
Whenever you had import urllib or import urllib2 in Python2.
Replace them with
import urllib.request
import urllib.response
import urllib.error
The classs and methods are same.
BTW, use 2to3 tool if you converting from python2 to python3.
urllib.request is a separate module; import it explicitly.

Categories

Resources