Python Forum
A trouble with requests - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: A trouble with requests (/thread-16380.html)



A trouble with requests - Truman - Feb-25-2019

# The following will return a list of 2-grams found in the Wikipedia article on the Python programming language:

import requests
from bs4 import BeautifulSoup

def getNgrams(input, n):
	input = input.split('')
	output = []
	for i in range(len(input)-n+1):
		output.append(input[i:i+n])
	return output
	
html = requests.get("https://en.wikipedia.org/wiki/Python_(programming_language)")
bsObj = BeautifulSoup(html.content, 'html.parser')
content = bsObj.find("div", {"id":"mw-content-text"}).get_text()
ngrams = getNgrams(content, 2)
print(ngrams)
print("2-grams count is: "+str(len(ngrams))) 
Error:
Traceback (most recent call last): File "C:\Python36\kodovi\ngram.py", line 3, in <module> import requests File "C:\Python36\lib\site-packages\requests\__init__.py", line 43, in <modul > import urllib3 File "C:\Python36\lib\site-packages\urllib3\__init__.py", line 8, in <module> from .connectionpool import ( File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 11, in < odule> from .exceptions import ( File "C:\Python36\lib\site-packages\urllib3\exceptions.py", line 2, in <modul > from .packages.six.moves.http_client import ( File "C:\Python36\lib\site-packages\urllib3\packages\six.py", line 203, in lo d_module mod = mod._resolve() File "C:\Python36\lib\site-packages\urllib3\packages\six.py", line 115, in _r solve return _import_module(self.mod) File "C:\Python36\lib\site-packages\urllib3\packages\six.py", line 82, in _im ort_module __import__(name) File "C:\Python36\lib\http\client.py", line 71, in <module> import email.parser File "C:\Python36\kodovi\email.py", line 3, in <module> import smtplib File "C:\Python36\lib\smtplib.py", line 47, in <module> import email.utils ModuleNotFoundError: No module named 'email.utils'; 'email' is not a package
Any idea why these errors appear?


RE: A trouble with requests - Larz60+ - Feb-25-2019

something funky going on.
You shouldn't have any problem importing requests unless something trashed memory.
try opening an interactive python and simply importing requests


RE: A trouble with requests - Truman - Feb-26-2019

Do you mean in IDLE? It doesn't give anything.


RE: A trouble with requests - Larz60+ - Feb-26-2019

I didn't mean idle, I meant to start python from command line or bash shell by typing python, but IDLE will do.
So if you don't get any message from 'IDLE' then something else is causing requests to fail on import in your new program.
If you're using an IDE, I'd exit from the IDE, reboot the OS and try again.
If it works first time after that, I would suspect that something in your code (or some other code) is tromping on memory.
Unfortunately this type of 'bug' is very hard to find as it could have been caused by something else you ran an hour ago, and only surfaced an hour later.


RE: A trouble with requests - snippsat - Feb-26-2019

Find a file you have called email.py and rename to eg my_email.py.
Then try Requests again.


RE: A trouble with requests - Truman - Feb-26-2019

Thank you snippsat, good eye, now everything works fine.