ImportError : cannot import name unwrap - python

I have been trying to run my codes :
(neaweather.py)
import requests
from bs4 import BeautifulSoup
import urllib3
r = requests.get('http://www.nea.gov.sg/api/WebAPI/?
dataset=2hr_nowcast&keyref=<keyrefno>')
soup = BeautifulSoup(r.content, "xml")
soup.find('validTime').string
However, when I run the codes, this is the error I got :
Error on CMD
I used to have a file called "urllib.py" (located on C:\scripts along with the file which im running neaweather.py) which clashes with the python modules however I have deleted the file. I have also deleted the file "urllib.pyc" from C:\scripts as well.
I have also deleted files for python3 which I have installed previously as im using python 2.7.12
I tried googling about this error and I saw a comment saying that .pyc files has something to do with this error. Is this true as I have already deleted the file "urllib.pyc" on C:\scripts
I'm not sure on how to solve this error, anyone?
This is not a duplicate of another similar error as I donot have a file name that clashes with python built in modules anymore as I have deleted it.
Thanks

From a Python prompt:
>>> import urllib
>>> print urllib.__file__
Make sure the file is part of the Python distribution and not your own script.

Related

ImportError: cannot import name 'EXTRACTOR_FILENAME' from 'talon.signature'

I am having trouble getting an import statement to work. I am attempting to use this package:
https://github.com/mailgun/talon
I am running the following command:
from talon.signature import EXTRACTOR_FILENAME, EXTRACTOR_DATA
I get the following error:
ImportError: cannot import name 'EXTRACTOR_FILENAME' from 'talon.signature' (system path to file)
While troubleshooting I don't see EXTRACTOR_FILENAME or EXTRACTOR_DATA defined anywhere. I did a search in directory for all files. Is there some sort of convention in python where EXTRACTOR_FILENAME maps to a specific class?
UPDATE: Figured it out, just something as simple as manually defining the 2 constants. The docs weren't exactly clear or I missed it.
For your project the import looks like this:
import talon
from talon import quotations
Put those statements on the top of your file, and it should work.
if you don't have the packages on your system type this in your terminal:
pip install talon
The Github repo also explains this

ImportError: cannot import name 'convert_from_path'

I am trying to convert pdf to image. So I'm doing it using the pdf2image library. But somehow I get this error
ImportError: cannot import name 'convert_from_path'
keeps showing up. When I try to run the same code in command prompt it seems to work. But in Sublime editor this error keeps showing up.
From your error message, you seem to have a file named pdf2image.py in the same directory as your main script.
File "/home/raheeb/Downloads/Telegram Desktop/New python/pdf_conversion.py" ...
from pdf2image.exceptions import convert_from_path
File "/home/raheeb/Downloads/Telegram Desktop/New python/pdf2image.py" ...
from pdf2image import convert_from_path ^^
||
||
You need to rename that, because your main script is importing from that pdf2image.py instead of the actual pdf2image module which I assume is what you have installed and should be the one you actually need.
As to why it imports that instead of the true module, you need to read the Module Search Path from the Python docs. Basically, it first searches for modules in the same directory as your script, before it searches from the installation environment.

ModuleNotFoundError: No module named 'urllib.request'; 'urllib' is not a package on visual studio code [duplicate]

This question already has answers here:
Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError
(2 answers)
Closed 3 years ago.
i am using visual studio code to run code,
it is working fine for most of the imports, except when i try to import the urllib module.
consider this code:
import urllib.request
x = urllib.request.urlopen('https://www.google.com/')
print(x.read())
when i run it on idle it works just fine, but if i run it on visual studio code i get an error:
File "c:\MyPythonScripts\dictionary Python\urllib.py", line 2, in
import urllib.request
ModuleNotFoundError: No module named 'urllib.request'; 'urllib' is not a package
i have searched everywhere trying to find a solution to this issue, since i am also having the same problem when i import the shelve module as well.
any idea would be highly appreciated, or should i even stop using Visual studio code altogether ?
Why is using requests preferred:
import requests
x = requests.get("https://www.google.com/")
print(x.content)
OUTPUT:
b'<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en">
<head><meta content="Search the world\'s information, including webpages, images,
videos and more. Google has many special features to help you find exactly
what you\'re looking for." name="description"><meta content="noodp" . . .
Try to type:
from urllib.request import urlopen

AttributeError: 'module' object has no attribute 'request'

When I run the following code in Python 3.3:
import urllib
tempfile = urllib.request.urlopen("http://yahoo.com")
I get the following error:
I did this too to verify:
What am I doing wrong?
The urllib module has been split into parts and renamed in Python 3 to urllib.request, urllib.parse, and urllib.error.
Import urllib.request instead of urllib.
import urllib.request
Interestingly, I noticed some IDE-depending behavior.
Both Spyder and PyCharm use the same interpreter on my machine : in PyCharm I need to do
import urllib.request
while in Spyder,
import urllib
does fine
If this is on PyCharm, as was mine, make sure your file name isn't urllib.py.
In visual code , u have to write import urllib.request instead of just import urllib.
Also, whenever errors such as module x has no attribute y occurs, it's because you have named the current file same as the package you are trying to import.
So, the way import in python works is that it first searches the current dir, and if it finds the module/package 'x' u were looking for , it assumes that it has found the target file, and searches for 'y'. And since u haven't defined 'y', the aforementioned error occurs.

how to import the new installed module whie an old same module exists

everybody!
the built-in pycurl module doesn't define Curl object
conn = pycurl.Curl()
Error message:
"pycurl" module has no attribute Curl
so I download the latest pycurl and install it,but it only works when I ran the code in interactive python interpreter,and I got the same error when I ran it as a script file.
It seems that when I run it as a script file,the old pycurl module will be included.
How can I import the new pycurl module or remove the old pycurl module?Thanks in advance.
PS.
in pydoc,the pycurl(old) module under '.' is clickable,redirecting to a page only saying "module has no attribute Curl",while the one(new) under '/usr/local/lib/python2.7/dist-packages' is gray and unclickable
It's a matter of when it is found. The basic import handler used in Python looks at the paths specified in sys.path in order to find a module, and after it has found something matching pycurl it will use it and not look further.
So, you need to either put your own copy of pycurl higher in the module search path, or in your own script do something like sys.path.insert(1, '/path/to/my-pycurl') with the path to the directory containing the pycurl package.

Categories

Resources