I have searched some web,but did not fit to my situation. it's running fine on jupyter notebook and shell,but not on pycharm 2017.1.3.the version is Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
from urllib import request
with request.urlopen('https://api.douban.com/v2/book/2129650') as f:
data = f.read()
print('Status:', f.status, f.reason)
for k, v in f.getheaders():
print('%s: %s' % (k, v))
print('Data:', data.decode('utf-8'))
stack trace for the error
"D:\Program Files (x86)\Anaconda3\python.exe" D:/pyProject/liao/usual/urllib.py
Traceback (most recent call last):
File "D:/pyProject/liao/usual/urllib.py", line 8, in <module>
from urllib import request
File "D:\pyProject\liao\usual\urllib.py", line 8, in <module>
from urllib import request
ImportError: cannot import name 'request'
Process finished with exit code 1
I was having a similar problem. Please rename your file to something else as the import is finding your file name urllib.py before finding the urllib package.
Related
When I tried to use the new module requests_html using the example of its website,I found the console displays information in the title.
I have successfully installed requests_html using pip install requests_html
I have updated the python to python3.7 (64-bit)
The messages of console:
Traceback (most recent call last):
File "C:/Users/owlish/PycharmProjects/python34/requests.py", line 2, in <module>
from requests_html import HTMLSession
File "C:\Users\owlish\AppData\Local\Programs\Python\Python37\lib\site-packages\requests_html.py", line 10, in <module>
import requests
File "C:\Users\owlish\PycharmProjects\python34\requests.py", line 2, in <module>
from requests_html import HTMLSession
ImportError: cannot import name 'HTMLSession' from 'requests_html' (C:\Users\owlish\AppData\Local\Programs\Python\Python37\lib\site-packages\requests_html.py)
code:
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://python.org/')
I expect it to work without an error,like the examples https://html.python-requests.org/.
With AhmedHawary's help,I found the reason for the error:I had a file named requests.py , which confilcted with the keywords . It worked fine after I renamed the file name.
Using sockjs with Tornado. On server run, this is the trace returned :
python server.py
Traceback (most recent call last):
File "server.py", line 10, in <module>
from sockjs.tornado import SockJSRouter
File "/Users/mohit/anaconda/envs/py34/lib/python3.4/site-packages/sockjs/__init__.py", line 20, in <module>
from sockjs.route import get_manager, add_endpoint
File "/Users/mohit/anaconda/envs/py34/lib/python3.4/site-packages/sockjs/route.py", line 11, in <module>
from sockjs.transports import handlers
File "/Users/mohit/anaconda/envs/py34/lib/python3.4/site-packages/sockjs/transports/__init__.py", line 3, in <module>
from .jsonp import JSONPolling
File "/Users/mohit/anaconda/envs/py34/lib/python3.4/site-packages/sockjs/transports/jsonp.py", line 8, in <module>
from .base import StreamingTransport
File "/Users/mohit/anaconda/envs/py34/lib/python3.4/site-packages/sockjs/transports/base.py", line 2, in <module>
from aiohttp import errors
ImportError: cannot import name 'errors'
I would recommend you to upgrade python version to
Python 3.5+ and utilize PEP-492 aka async/await. If you are using Python 3.4 please replace await with yield from and async def with #coroutine e.g.:
async def coro(...):
ret = await f()
should be replaced by:
#asyncio.coroutine
def coro(...):
ret = yield from f()
from aiohttp documentaion:
Dependencies
Python 3.4.2+
chardet, multidict, async_timeout, yarl
Optionally you may install the cChardet and aiodns libraries (highly recommended for sake of speed).
$ pip install cchardet
$ pip install aiodns
when i run the the given code in prepare_dataset.py whose code is below as
from urllib.request
import urlopen
import json as simplejson
def main():
# Download and prepare datasets
list_generator = video_list_generator.Generator("playlists.json", youtube_api_client.Client("AIzaSyD_UC-FpXbJeWzPfscLz9RhqSjKwj33q6A"))
video_list = list_generator.get_video_list("piano")
downloader = youtube_video_downloader.Downloader()
downloader.download_from_list(video_list)
if __name__ == '__main__':
main()
as python prepare_dataset.py in command-line then i get these errors
Traceback (most recent call last):
File "prepare_dataset.py", line 1, in <module>
from urllib.request import urlopen
ImportError: No module named request
How can i get to run the above file any idea guys?
QUESTION
I am returning an ImportError: No module named 'cStringIO'. Unfortunately cStringIO doesn't exist anymore and I need to use StringIO as a replacement. How can I do this?
import edgar
import ftplib
from io import StringIO
ftp = ftplib.FTP(edgar.FTP_ADDR)
ftp.login()
try:
edgar.download_all(ftp, "/tmp")
except Exception as e:
print(e)
finally:
ftp.close()
OUTPUT
Traceback (most recent call last):
File "/usr/local/lib/ana/lib/python3.4/site- packages/edgar/downloader.py", line 5, in <module>
from cStringIO import StringIO
ImportError: No module named 'cStringIO'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/aranjohn/PycharmProjects/edgar/secEd.py", line 1, in <module>
import edgar
File "/usr/local/lib/ana/lib/python3.4/site- packages/edgar/__init__.py", line 1, in <module>
from .downloader import FTP_ADDR, file_list, download, download_all
File "/usr/local/lib/ana/lib/python3.4/site- packages/edgar/downloader.py", line 7, in <module>
from StringIO import StringIO
ImportError: No module named 'StringIO'
Process finished with exit code 1
StringIO no longer exists in 3.x. Use either io.StringIO for text or io.BytesIO for bytes.
I've installed xlutils and am trying the following code:
from xlrd import open_workbook
input_file = open_workbook('DataTestFile.xlsx')
for s in input_file.sheets():
print('Sheet:',s.name)
for row in range(s.nrows):
values = []
for col in range(s.ncols):
values.append(s.cell(row,col).value)
print(','.join(values))
print()
(as per tutorial book found here)
I'm getting an error indicating that the licences module is not found:
Traceback (most recent call last):
File "C:\Users\xxxxxxx\workspace_python\xxxxxxx\TestPack\test_file_excel.py", line 7, in <module>
from xlrd import open_workbook
File "C:\Python34\lib\site-packages\xlrd\__init__.py", line 9, in <module>
import licences
ImportError: No module named 'licences'
However, I can confirm that the file licences.py is present in the same folder as file _ init _.py quoted above:
How come it's not recognized?