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.
Related
I have problem with requests package. In past requests was working, but today for no reason it stopped working. I am just importing requests and it cause error.
Code:
import requests
Error:
Traceback (most recent call last):
File "d:\programovani\Python\SMSemail\test.py", line 1, in
import requests
File "C:\Users\vitek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests_init_.py", line 43, in
import urllib3
File "C:\Users\vitek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\urllib3_init_.py", line 11, in
from . import exceptions
File "C:\Users\vitek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\urllib3\exceptions.py", line 3, in
from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
File "C:\Users\vitek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\urllib3\packages\six.py", line 234, in create_module
return self.load_module(spec.name)
File "C:\Users\vitek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\urllib3\packages\six.py", line 209, in load_module
mod = mod._resolve()
File "C:\Users\vitek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\urllib3\packages\six.py", line 118, in _resolve
return _import_module(self.mod)
File "C:\Users\vitek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\urllib3\packages\six.py", line 87, in import_module
import(name)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\http\client.py", line 71, in
import email.parser
File "d:\programovani\Python\SMSemail\email.py", line 1, in
from requests import get
ImportError: cannot import name 'get' from partially initialized module 'requests' (most likely due to a circular import) (C:\Users\vitek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests_init.py)
Screenshot:
What I've tried:
import requests as re
Reinstalling package.
Note: File name is different
What is causing this error? Thank you.
The relevant parts of the traceback and my interpretation:
You're trying to run test.py. One of the imports in it is import requests:
Traceback (most recent call last):
File "d:\programovani\Python\SMSemail\test.py", line 1, in import requests
This results in a (normal) longer chain of imports. Somewhere along the line in the standard library's http.client module there's an import in line 71: import email.parser
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\http\client.py", line 71, in import email.parser
Now trouble starts, because you have a file email.py in the same folder as test.py:
File "d:\programovani\Python\SMSemail\email.py", line 1, in from requests import get ImportError: cannot import name 'get' from partially initialized module 'requests' (most likely due to a circular import)
This alone should be enough to derail the program. So I'm a bit puzzled about the error message you get. I pretty much got the same as you when I tried to reconstruct your situation (one file test.py with import requests and another one named email.py in the same folder) but it ended with:
ModuleNotFoundError: No module named 'email.parser'; 'email' is not a package
You have a from requests import get in email.py, so there is in fact a potential (unintended) circular import (program starts with importing requests and on the way to do that imports (parts of) request again -> circular):
File "d:\programovani\Python\SMSemail\email.py", line 1, in from requests import get
Solution: Renaming email.py should do the job, if I'm not mistaken.
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.)
I have a python script that uses the lib awswrangler. Today my scrpit started to give errors in the import of the library and I don't know what is happening.
I'm running the script in a docker container with image python: 3.8
Example:
import awswrangler as wr
print(wr.__version__)
Error:
Traceback (most recent call last):
File "src/avec/automation/TaskBaseUserPass.py", line 1, in <module>
from awswrangler.pandas import Pandas
File "/usr/local/lib/python3.8/site-packages/awswrangler/__init__.py", line 17, in <module>
from awswrangler.pandas import Pandas # noqa
File "/usr/local/lib/python3.8/site-packages/awswrangler/pandas.py", line 45, in <module>
class Pandas:
File "/usr/local/lib/python3.8/site-packages/awswrangler/pandas.py", line 273, in Pandas
def _read_csv_once_remote(send_pipe: mp.connection.Connection, session_primitives: "SessionPrimitives",
AttributeError: module 'multiprocessing' has no attribute 'connection'
I have been experienced the same issue today when trying to import awswrangler. For me, downgrading the following dependencies helped:
pip install fsspec==0.6.3 PyAthena==1.10.2 s3fs==0.4.0
It seems that one or more of them were causing the problem.
If your code uses multiprocessing.connection.Listener or multiprocessing.connection.Client, then you should use:
import multiprocessing.connection
If you just use
import multiprocessing
.. then your code might get an ImportError or not. It depends on other modules. If an other module imports multiprocessing.connection, then it will work.
But I guess you don't want random behavior, and that's why you should import multiprocessing.connection.
I managed to run version 3.6, the library has a problem with mp.connection.Connection in current python versions
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 :)
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.