Python 3.2 won't import cookielib - python

I have looked everywhere for this and just cannot find the answer. I have checked my python version and it is version 3.2 . When I try to import cookielib I receive:
ImportError: No module named cookielib
I have seen that in Python 3.0 it was renamed to
http.cookiejar and that it would auto import cookielib.
I thought that maybe there was some wild error in my python configuration so I thought I should try and import http.cookiejar like this import http.cookiejar. That did not work all and I get and error:
EOFError: EOF read where not expected.
This is not the error I had expected becuase import http.cookies imports just fine.
Does anybody have a solution to this problem? What am I overlooking?
Full Error:
Traceback (most recent call last):
File "C:\Users\Spencer\Downloads\selenium-2.20.0.tar\selenium-2.20.0\selenium-2.20.0\test", line 1, in <module>
import urllib.request, urllib.parse, http.cookiejar
EOFError: EOF read where not expected

The automatic renaming business only applies if you use 2to3. Therefore, you have to import http.cookiejar.
The error EOFError: EOF read where not expected is only ever thrown by Python marshalling. Most likely, this is caused by a race condition fixed in Python 3.3, where multiple processes tried to write concurrently to the pyc file. Deleting all .pyc files may be a workaround.

try:
import cookielib
except:
import http.cookiejar
cookielib = http.cookiejar

The cookielib module has been renamed to http.cookiejar in Python 3.0. The 2to3 tool will automatically adapt imports when converting your sources to 3.0.

My initial guess is that you have a corrupted library file. Inside your Python installation, look at lib/python3.2/http/cookiejar.py and scroll down to the end. Mine (Python 3.2.2) ends in the save() method definition with
finally:
f.close()
If you see anything else, your installation is probably broken and I'd recommend reinstalling it.

Related

Python ImportError: cannot import name urlencode

I've seen this question asked before but none of the answers seem to work for me.
I am using python version 2.7.13
In my code I have ..
import base64
import httplib2
Now when I run it on my own pc it works fine, but when I run it on my works p.c. behind a firewall I get ..
Traceback (most recent call last):
File "mail4.py", line 2, in
import httplib2
File "C:\Python27\lib\site-packages\httplib2__init__.py", line 39, in
import urllib
File "C:\mypy\urllib.py", line 1, in
import requests
File "C:\Python27\lib\site-packages\requests__init__.py", line 43, in
import urllib3
File "C:\Python27\lib\site-packages\urllib3__init__.py", line 8, in
from .connectionpool import (
File "C:\Python27\lib\site-packages\urllib3\connectionpool.py", line 35, in
from .request import RequestMethods>
File "C:\Python27\lib\site-packages\urllib3\request.py", line 10, in >
from .packages.six.moves.urllib.parse import urlencode
ImportError: cannot import name urlencode
I've tried setting proxies.
I've tried uninstalling and re-installing six
Also
from urllib.parse import urlencode
All to no avail ?
You are (in this case) apparently running Python 2.7.x, however parse module is in urllib package in Python 3.x. In Python 2.x its name was (just) urlparse. Based on that, I'd guess different configuration (mismatch in versions used between Code and/or 3rd party packages and/or python interpreter is behind the problem you're seeing).
EDIT: sorry, I should have also noticed you are looking for urlencode on the last bit. In python 2.7 that is a function in urllib module: from urllib import urlencode.
I fixed this issue with installing Werkzeug to <1.0, which removed the contrib module
pip install Werkzeug==0.16.1

ImportError : cannot import name unwrap

I have been trying to run my codes :
(neaweather.py)
import requests
from bs4 import BeautifulSoup
import urllib3
r = requests.get('http://www.nea.gov.sg/api/WebAPI/?
dataset=2hr_nowcast&keyref=<keyrefno>')
soup = BeautifulSoup(r.content, "xml")
soup.find('validTime').string
However, when I run the codes, this is the error I got :
Error on CMD
I used to have a file called "urllib.py" (located on C:\scripts along with the file which im running neaweather.py) which clashes with the python modules however I have deleted the file. I have also deleted the file "urllib.pyc" from C:\scripts as well.
I have also deleted files for python3 which I have installed previously as im using python 2.7.12
I tried googling about this error and I saw a comment saying that .pyc files has something to do with this error. Is this true as I have already deleted the file "urllib.pyc" on C:\scripts
I'm not sure on how to solve this error, anyone?
This is not a duplicate of another similar error as I donot have a file name that clashes with python built in modules anymore as I have deleted it.
Thanks
From a Python prompt:
>>> import urllib
>>> print urllib.__file__
Make sure the file is part of the Python distribution and not your own script.

StringIO and String Import error on installing VIM-Latex via Pathogen

I tried to install VIM-Latex using the Pathogen plugin on my machine which is running OSX Lion 10.7.5. I copied the downloaded VIM-Latex plugin files into my ~/.vim/bundle directory.
I also edited the .vimrc according to the instructions specified here.
However, I'm getting the following errors on trying to open a tex document in MAC Vim:
File "/Users/username/.vim/bundle/vim-latex-1.8.23-20130116.788-git2ef9956/ftplugin/latex- suite/outline.py", line 12, in <module>
import StringIO
ImportError: No module named StringIO
Error detected while processing /Users/username/.vim/bundle/vim-latex-1.8.23-20130116.788-git2ef9956/ftplugin/latex-suite/main.vim:
and
File "/Users/username/.vim/bundle/vim-latex-1.8.23-20130116.788-git2ef9956/ftplugin/latex-suite/pytools.py", line 1, in <module>
import string, vim, re, os, glob
ImportError: No module named string
Also, I ran a basic python script from the terminal that imported StringIO and string, and both seem to be getting imported just fine.
I'm not sure where the problem is here. Since I'm really new both with VIM and Installing Plugins, I'm not sure how I should go about debugging this issue, and so, any help will be precious!
Thanks!
StringIO and string are not available in Python 3.x. To make this code work, you have to run it with Python 2.x, e.g. Python 2.7.

AttributeError: 'module' object has no attribute 'request'

When I run the following code in Python 3.3:
import urllib
tempfile = urllib.request.urlopen("http://yahoo.com")
I get the following error:
I did this too to verify:
What am I doing wrong?
The urllib module has been split into parts and renamed in Python 3 to urllib.request, urllib.parse, and urllib.error.
Import urllib.request instead of urllib.
import urllib.request
Interestingly, I noticed some IDE-depending behavior.
Both Spyder and PyCharm use the same interpreter on my machine : in PyCharm I need to do
import urllib.request
while in Spyder,
import urllib
does fine
If this is on PyCharm, as was mine, make sure your file name isn't urllib.py.
In visual code , u have to write import urllib.request instead of just import urllib.
Also, whenever errors such as module x has no attribute y occurs, it's because you have named the current file same as the package you are trying to import.
So, the way import in python works is that it first searches the current dir, and if it finds the module/package 'x' u were looking for , it assumes that it has found the target file, and searches for 'y'. And since u haven't defined 'y', the aforementioned error occurs.

Scapy.all import * does not work

So, I wrote a little script in Ubuntu for scapy.
#!/usr/bin/env python
import sys
#from scapy.all import *
try
import scapy
except ImportError:
del scapy
from scapy import all as scapy
i= IP()
t= TCP()
i.dst='192.168.56.100'
t.dport=22
pakket=i/t
answered,unanswered=sr(pakket)
answered.nsummary()
i wrote the 'try' because of another topic here (tried it as a solution).
My current output with this code is the following
Traceback (most recent call last):
File "./scapy.py", line 5, in <module>
import scapy
File "/home/nicholas/scapy.py", line 9, in <module>
i=IP()
NameError: name 'IP' is not defined
when trying it just with from scapy.all import * withouth the 'try'.
Traceback (most recent call last):
File "./scapy.py", line 3, in <module>
from scapy.all import *
File "/home/nicholas/scapy.py", line 3, in <module>
from scapy.all import *
ImportError: No module named all
I tried different ways of importation found on Google but it still doesn't work. Could anyone please tell me what I'm doing wrong? (don't mind the indentation of this post)
From looking at scapy source, the scapy package doesn't appear to import anything or define an __all__ in __init__. As a result, you need to explicitly import scapy.all (or from scapy import all) before you can from scapy.all import anything else from it, as it won't be in sys.modules yet. Note that this only has to happen once in your program flow though, as after the interpreter imports the module, it will be available to all code that executes from then on, regardless of where it is. Take a look at the Python docs on modules and how import, and specifically importing a package, works for more details.
Edit:
I think I see the problem now, I just was paying attention to the wrong part of your stack trace. Pretty sure what you are dealing with here is a name collision. Your file is named scapy.py, so when you import scapy from the context of that file, you are actually importing the file itself as a module. Since your file does not have a submodule named all (it can't, since it's not a package), you get the import error you are seeing. Try switching the name of your file to something that does not conflict with any packages or modules you wish to import inside it, and see if that works out better.
By the way, note in your stack traces that your import is actually essentially recursively calling your one file. That should be a clue that something has gone haywire in the import process.
I had a similar problem on OSX, I installed the scapy package pip install scapy and then I was trying to execute my test file scapy.py The error I got was :
python scapy.py
Traceback (most recent call last):
File "scapy.py", line 1, in <module>
from scapy.all import *
File "/Users/**/Desktop/scapy-test/scapy.py", line 1, in <module>
from scapy.all import *
ModuleNotFoundError: No module named 'scapy.all'; 'scapy' is not a package
In my case, it was the file name itself that caused the problem it can't be called scapy.py. I change it to test.py and all worked, it had nothing to do with the package location just the file name.
I like to add something to #Daniel answer. Your real problem is not scapy package. Your real problem is in your python file name. Don't ever use library name or its contents as your file name.
In your case, your file name is scapy.py. After that you import scappy. In here you accidentally call your python file as object in your code there for your compiler can't understand which type(file or library) to call. There for that error was appeared.
I saw this when I had a scapy.py in the current directory. scapy.all import * seems to look in the current directory first.
The correct import with current versions would be:
from scapy.all import *

Categories

Resources