Yesterday I installed feedparser (on OSX 10.5) and it worked fine, but now it stopped working.
This is the script (copied from feedparser documentation)
import feedparser
d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
d['feed']['title']
u'Sample Feed'
It tells me this:
Traceback (most recent call last):
File "example.py", line 3, in <module>
import feedparser
File "example.py", line 2, in <module>
d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
AttributeError: 'module' object has no attribute 'parse'
But also an actual script using feedparser stopped working, same error.
The point is when there is a script named feedparser.py, python will considered it as a module to import with higher priority than the module installed.
Issue is with Name of file. Python confuses between name of file and module name.
Related
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'm having issues running ortools on Linux. I downloaded it from google's site (https://developers.google.com/optimization/) and installed it using "make install," but when I go to use it in python I get the following:
Traceback (most recent call last):
File "regular.py", line 42, in <module>
from ortools.constraint_solver import pywrapcp
File "/home/m3/summer/ortools_examples/examples/python/ortools.py", line 2, in <module>
ImportError: No module named linear_solver
It looks like despite installing ortools, its still not in my python path correctly, so when I call it in the file, it doesn't find anything and returns an error, right? Any advice on how to solve this?
I am using mechanize for the first time. If I type the line from mechanize import Browserin python shell (interpreter) it doesn't give any error but when run the same code as a part of a .py file it gives the following error:
Traceback (most recent call last):
File "/home/namit/Codes/BS4/mechanize.py", line 1, in <module>
import mechanize
File "/home/namit/Codes/BS4/mechanize.py", line 4, in <module>
mech = mechanize.Browser()
AttributeError: 'module' object has no attribute 'Browser'
Change your filename from mechanize.py. Python is importing your file as the module, instead of importing the mechanize library.
Rename your filename to something other than mechanize.py
Giving files names same as that of the modules imported causes the file to be imported instead of the intended module.
For some reason, anytime I try to import a python library, a particular old error message keeps popping up.
However, everything works fine when working with shell.
I know this is not a real programming question, but I'm stuck and would appreciate any help.
Any ideas on how to fix this?
Error message
>>> import nltk
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
import nltk
File "C:\Python27\lib\site-packages\nltk\__init__.py", line 91, in <module>
from internals import config_java
File "C:\Python27\lib\site-packages\nltk\internals.py", line 22, in <module>
except ImportError: from xml.etree import ElementTree
ImportError: No module named etree
Does "C:\Python27\lib\xml\etree\ElementTree.py" exist in your computer?
Since IDLE and shell behaves differently, you may try the two lines in IDLE and Shell:
import sys
sys.path
Then check the difference of env path
I try to run first example from python wiki and when I try to run it:
$ python BaseHttpServer.py
I get an error AttributeError: 'module' object has no attribute 'BaseHTTPRequestHandler'.
I tested it on Python 2.7.3 on Linux Mageia 2 64-bit:
Traceback (most recent call last):
File "BaseHTTPServer.py", line 9, in <module>
import BaseHTTPServer
File "/home/vanveber/BaseHttpServer/BaseHTTPServer.py", line 14, in <module>
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
AttributeError: 'module' object has no attribute 'BaseHTTPRequestHandler'
And it on Python 2.7.3 on Windows 7 64-bit
Traceback (most recent call last):
File "BaseHTTPServer.py", line 11, in <module>
from BaseHTTPServer import BaseHTTPRequestHandler
File "C:\BaseHttpServer\BaseHTTPServer.py", line 11, in <module>
from BaseHTTPServer import BaseHTTPRequestHandler
ImportError: cannot import name BaseHTTPRequestHandler
BUT!
BaseHttpServer is a class from standard Python library.
If I write and run this code from Python GUI on Windows it works correctly!
What is a problem and why?!
Solution: Rename the python file.
Explanation: BaseHTTPServer is a module in the standard library. When you have a python file called BaseHTTPServer.py in your local directory, you will hide the standard library module, and you can no longer import it, because the statement
import BaseHTTPServer
will not import the standard library module, but the local BaseHTTPServer.py module.