AttributeError: module 'urllib3' has no attribute 'urlretrieve' - python

I'm trying the code in this link for doing word2vec by keras.
I receive error on this line:
filename, _ = urllib.urlretrieve(url + filename, filename)
the error is:
AttributeError: module 'urllib' has no attribute 'urlretrieve'
for solving it I installed and imported urllib3 and change that line to:
filename, _ = urllib3.urlretrieve(url + filename, filename)
but I receive again with that error:
AttributeError: module 'urllib3' has no attribute 'urlretrieve'
How can I fix it?

Extending from comments section:
As stated by documentation, you can access urlretrieve like this
urllib.request.urlretrieve
https://docs.python.org/3.4/library/urllib.request.html#urllib.request.urlretrieve

The answer above is good enough, just want to remind that, if you get error
module 'urllib' has no attribute 'request',
just try import urllib.request , I use python 3.7
import urllib.request
urllib.request.urlretrieve(url);

Related

AttributeError: module 'transposer' has no attribute 'transpose'

I am running a script which needed transposer, I import that module, but it got this error:
....line 184, in jj=transposer.transpose(i=args.v+ '/vcf_to_str/'+args.o+"TransposedStruct.str",d="\t",)
AttributeError: module 'transposer' has no attribute 'transpose'
Would you please help me to solve that? Thanks

AttributeError: module 'collections' has no attribute 'Iterable'

I am using the "pdftables" library to extract tables from a pdf.
This is my code:
import pdftables
pg = pdftables.get_pdf_page(open("filename.pdf","rb"),253)
print(pg)
table = pdftables.page_to_tables(pg)
print(table)
I am getting this error and I am not sure what's causing it.
Traceback (most recent call last):
File "c:\Users\gayak\OneDrive\Documents\PDF to Database\PDF_to_Tables_3.py", line 9, in <module>
table = pdftables.page_to_tables(pg)
File "C:\Users\gayak\AppData\Local\Programs\Python\Python310\lib\site-packages\pdftables\pdftables.py", line 485, in page_to_tables
box_list = LeafList().populate(page, flt).purge_empty_text()
File "C:\Users\gayak\AppData\Local\Programs\Python\Python310\lib\site-packages\pdftables\tree.py", line 98, in populate
for obj in children(pdfpage):
File "C:\Users\gayak\AppData\Local\Programs\Python\Python310\lib\site-packages\pdftables\tree.py", line 75, in children
if isinstance(obj, collections.Iterable):
AttributeError: module 'collections' has no attribute 'Iterable'
The version of python I am using is python 3.10.4
I used pip install pdftables.six to get the library
If you don't want to change the source code, there is an easier way. Just use this in your script after importing.
import collections
collections.Iterable = collections.abc.Iterable
As the Error says, the attribute isn't valid. When using collection.Iterable then it not finds the Iterable attribute. This can have different reasons but in this case, after looking on the package files on Github, i noticed that you are missing the abc keyword. I not tried this solution, but I'm 95% sure that using collections.abc.Iterable will do the thing.
import collections
from _collections_abc import Iterable
collection.Iterable = Iterable
This should work 🙂
A simple fix that works for python3.10:
Under directory
/usr/lib/python3.10/collections/__ init__.py
Note: The path might change depending
Add this line of code:
from _collections_abc import Iterable

Arbitrarily modify and import a python module after importing

First of all, a little bit of context I was using importlib to modify an import and was trying to find a solution for modifying an imported file.
I saw an answer suggesting that for arbitrarily modifying an imported module you can use this code
def modify_and_import(module_name, package):
spec = importlib.util.find_spec(module_name, package)
source = spec.loader.get_source(module_name)
new_source = tokens.tokenize(source)
module = importlib.util.module_from_spec(spec)
codeobj = compile(new_source, module.__spec__.origin, 'exec')
exec(codeobj, module.__dict__)
sys.modules[module_name] = module
return module
Now I ran it with the following call
o = modify_and_import('o', "./o.xran")
but, running it through an exec gives me the following error
AttributeError: 'NoneType' object has no attribute 'loader'
Then I tried to print 'spec' but got None so, I think spec is returning while a file named o.xran exists.
Ok, after a day I finally made it work and since I was only working with local files, I didn't need to use imp at all. Here's my final code
with open(module_name) as f:
source = f.read()
source = tokens.tokenize(source)
module = types.ModuleType(module_name)
exec(source, module.__dict__)
sys.modules[module_name] = module
return module
Thanks to anyone who answered or tried to contribute.
bro,open the modified module in your text editor,in the same window in which your project is opened,then in your project import it using,'from import *'..hope it will works..thanx

ImportError: cannot import name 'loads' from 'json' (unknown location)

Previos title was:
AttributeError: module 'json' has no attribute 'loads'
I changed it because it looks similar to this but at the link that i provided, the problem seems that the person was having a file called json.py that was tricking the import to think that is importing a local file and not the json from standard library. My problem is that I don't have any local file called json.py;
I am wondering if it has to do anything related to PATH or the structure of my project. Any suggestion might help.
error traceback:
File "D:\Me\IdeaProjects\src\app\repositories\user_repository.py", line 14, in get_user
user = json.loads(file.read())
I am running the code in Windows 10, and intelliJ ide.
Python version: 3.7.4
Tried the code from the official documentation this:
import json
def as_complex(dct):
if '__complex__' in dct:
return complex(dct['real'], dct['imag'])
return dct
json.loads('{"__complex__": true, "real": 1, "imag": 2}', object_hook=as_complex)
And got this error as well:
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
json.loads('{"__complex__": true, "real": 1, "imag": 2}',object_hook=as_complex)
AttributeError: module 'json' has no attribute 'loads'
When I try to import loads explicitly i get this error:
ImportError: cannot import name 'loads' from 'json' (unknown location)
I had python installed in Admin account in window 10 and it was installed with Admin privileges, but when i used in another account I could not use the packages, however installing the python in the current account did fix the problem.
Try to import loads explicitly:
import json
from json import loads

Dynamic Importing in Python (Dotted statments)

I'm having trouble with the following code:
def get_module(mod_path):
mod_list = mod_path.split('.')
mod = __import__(mod_list.pop(0))
while mod_list:
mod = getattr(mod, mod_list.pop(0))
return mod
When I do get_module('qmbpmn.common.db_parsers') I get the error message:
AttributeError: 'module' object has no attribute 'db_parsers'.
However: import qmbpmn.common.db_parsers works perfectly fine.
When using __import__ to import submodules, you must pass the parent package as the fromlist argument:
>>> __import__("os.path")
<module 'os' from '/usr/lib/python2.6/os.pyc'>
>>> __import__("os.path", fromlist=["os"])
<module 'posixpath' from '/usr/lib/python2.6/posixpath.pyc'>
__import__ works with the dotted module path, so this should work
def get_module(mod_path):
return __import__(mod_path)
or more simply
get_module = __import__
Perhaps I am misunderstanding the problem
importing a package does not automatically import all the submodules into it's namespace. For example
import qmbpmn
does not mean that
qmbpmn.common.db_parsers
will automatically resolve

Categories

Resources