Python can't import urllib2 (gives an error) - python

I just started with programming, I wanted to import the urllib resource but when I just use this code:
import urllib2
It worked 2 months ago (took a break).
It gives me this error:
Traceback (most recent call last):
File "test.py", line 1, in <module>
import urllib2
File "/usr/lib/python2.7/urllib2.py", line 94, in <module>
import httplib
File "/usr/lib/python2.7/httplib.py", line 1157, in <module>
import ssl
File "/usr/lib/python2.7/ssl.py", line 58, in <module>
import textwrap
File "/usr/lib/python2.7/textwrap.py", line 10, in <module>
import string, re
File "/home/younes/Python/string.py", line 11
for total:
^
I used Python 2 for that, also when using Python 3 it doesn't find the module. (it also doesn't find the urllib3 module?)
I have reinstalled Python but it didn't work.
I am using Ubuntu 14.04.

The file /home/younes/Python/string.py is preventing import of the standard library module string which in turn prevent import of modules depending on the string module. In addition to that, the file has an syntax error.
Remove the file or rename the file. If there's /home/younes/Python/string.pyc, also remove/rename it.

You have a file called string.py which shadows the builtin string module. Also, consider starting with a newer Python 3 instead of the old Python 2.

if use python3 , from urllib import requests or import urllib.request and usage:
urllib.request.urlopen("http:xxx")

Related

Install module cbpro throws multiple errors in several other projects (python 3.10, pycharm)

I have made a few projects which run fine. A new project required me to install pip install cbpro (a module for dealing with coinbase cryptocurrency API).
After installing it, even running the simplest code throws several errors:
Input:
import cbpro
import pandas as pd
c = cbpro.PublicClient()
data = pd.DataFrame(c.get_products())
data.tail().T
Errors:
Traceback (most recent call last):
File "/Users/me/PycharmProjects/stonks/coinbase_interface.py", line 1, in <module>
import cbpro
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/cbpro/__init__.py", line 1, in <module>
from cbpro.authenticated_client import AuthenticatedClient
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/cbpro/authenticated_client.py", line 10, in <module>
import requests
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/requests/__init__.py", line 63, in <module>
from . import utils
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/requests/utils.py", line 29, in <module>
from .cookies import RequestsCookieJar, cookiejar_from_dict
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/requests/cookies.py", line 174, in <module>
class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):
AttributeError: module 'collections' has no attribute 'MutableMapping'
Secondly, when I then go to other projects that were working well, I get additional errors. Of note, those projects start with:
import pandas as pd
import requests
import json
Errors:
Traceback (most recent call last):
File "/Users/me/PycharmProjects/stonks/historical_crypto_pull.py", line 2, in <module>
import requests
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/requests/__init__.py", line 63, in <module>
from . import utils
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/requests/utils.py", line 29, in <module>
from .cookies import RequestsCookieJar, cookiejar_from_dict
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/requests/cookies.py", line 174, in <module>
class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):
AttributeError: module 'collections' has no attribute 'MutableMapping'
Things I've tried:
If I uninstall cbpro, then uninstall and reinstall requests, I can undo the error and am back where I started. However, I'm interested in knowing why I am encountering these errors because I'd like to actually use cbpro and know how to solve this issue in the future.
The libraries I was using were not compatible with Python 3.10. I am still at a loss for how I would inherently know that based on the errors thrown, but since then my solution was to install Python 3.9 and see if I still get the same errors when running 3.9.
(Then I also read that it might be a better idea for my current purposes to use a slightly older version of Python, so I moved over to 3.9 completely and haven't had the same issue again.)

"ImportError: no suitable image found" using BeautifulSoup and Python3.8

I am using BeautifulSoup and Python3.8 and all of a sudden, I am getting the following error. All the codes of BeautifulSoup have the same issue which worked till yesterday. I did not update anything.
The full error log is shown below:
/Users/me/PycharmProjects/bankOfAmeri/venv/bin/python /Users/me/PycharmProjects/myfewsteps/onlyTitles.py
Traceback (most recent call last):
File "/Users/me/PycharmProjects/myfewsteps/onlyTitles.py", line 1, in <module>
import requests
File "/Users/me/PycharmProjects/myfewsteps/venv/lib/python3.8/site-packages/requests/__init__.py", line 43, in <module>
import urllib3
File "/Users/me/PycharmProjects/myfewsteps/venv/lib/python3.8/site-packages/urllib3/__init__.py", line 7, in <module>
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File "/Users/me/PycharmProjects/myfewsteps/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 7, in <module>
from socket import error as SocketError, timeout as SocketTimeout
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 52, in <module>
import os, sys, io, selectors
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/selectors.py", line 11, in <module>
import math
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload/math.cpython-38-darwin.so, 2): no suitable image found. Did find:
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload/math.cpython-38-darwin.so: code signature in (/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload/math.cpython-38-darwin.so) not valid for use in process using Library Validation: Library load disallowed by System Policy
Process finished with exit code 1
Any solution to share which I can apply and resolve.
At the bottom line it states that you have restrictions on your system policy.
If you changed any settings within your environment then this is the
most likely reason you are receiving and error.
I would also suggest if you have not already, using an installer to try and
uninstall/reinstall requests.
Finally, you mentioned you did not update python. Although it's unlikely this could be
the problem. Try updating python and see if that works. If updating python does not
work, it's always good to be up to date anyway.
Hope this helped :)

mathplotlib six.moves.urllib.request import error

I just had this problem while trying to import mathplotlib.
This was the error I was getting:
Traceback (most recent call last):
File "C:/xxx/solver.py", line 7, in <module>
import matplotlib
File "C:\Users\xxx\Anaconda2\lib\site-packages\matplotlib\__init__.py", line 129, in <module>
from six.moves.urllib.request import urlopen
ImportError: cannot import name urlopen
Here is the solution I used to fix this issue. It took me a while to figure this out and I couldn't find any references online that helped me, so I am posting the solution here.
I replaced the offending line in matplotlib/init.py
from six.moves.urllib.request import urlopen
with:
from urllib.request import urlopen
which allowed me to see the real error:
import socket
File "C:\PROJECTS\xxx\socket.py", line 7, in <module>
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
AttributeError: 'module' object has no attribute 'AF_INET'
So the problem was that I had named one of my modules "socket.py" and this was causing an error, which was masked by the six.moves importing mechanism. This file was stacked fairly deep in my project tree, but it happened to be at the same level as the script I was trying to run, which is presumably why it was being imported at the root level.
I was using PyCharm on windows for all of this.

Import one module (requests), get error "cannot import name Random" [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 6 years ago.
I have a Python script that uses requests. It worked fine for a long time. Now out of the blue I get the following error. I tried reinstalling requests but that didn't fix it. The only thing I can think of that caused the error is I have been running a Django development server, so maybe I got hacked? Please help.
code:
import requests
...
error:
Traceback (most recent call last):
File "myfile.py", line 1, in <module>
import requests
File "/Users/myuser/.virtualenvs/mysite/lib/python2.7/site-packages/requests/__init__.py", line 58, in <module>
from . import utils
File "/Users/myuser/.virtualenvs/mysite/lib/python2.7/site-packages/requests/utils.py", line 12, in <module>
import cgi
File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cgi.py", line 50, in <module>
import mimetools
File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/mimetools.py", line 6, in <module>
import tempfile
File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py", line 35, in <module>
from random import Random as _Random
ImportError: cannot import name Random
You have masked the built-in library module with a local file named random.py. Rename that file.
Python looks up modules to import along a path, and if you put a module in a location that's looked at before the standard library, you can end up masking a built-in like you did here.

help with python urllib2 import error

In my script, I've imported urrlib2 and the script was working fine. After reboot, I get the following error:
File "demo.py", line 2, in <module>
import urllib2
File "/usr/lib/python2.6/urllib2.py", line 92, in <module>
import httplib
File "/usr/lib/python2.6/httplib.py", line 78, in <module>
import mimetools
File "/usr/lib/python2.6/mimetools.py", line 6, in <module>
import tempfile
File "/usr/lib/python2.6/tempfile.py", line 34, in <module>
from random import Random as _Random
ImportError: cannot import name Random
And when I do import random separately, it works fine. Any ideas what might be wrong?
I'm using ubuntu 9.10 (up to date). thanks
The usual answer is that you've got a file called random.py in the current directory when the script is running. tempfile would be accidentally importing that random and not the stdlib random module.
Check that random is the stdlib's module and not some arbitrary module with the same name from sys.path.
>>> inspect.getabsfile(random)

Categories

Resources