How do I get more info about a python error? - python

I'm trying to run a python script on Ubuntu 16.04; the script runs fine on Ubuntu 14.04, but I keep getting kind of a vague object has no attribute error.
It seems this exception class is still active so not sure what the problem is. I've installed python-urllib3 and python3-urllib3 (even though python on the 16.04 system is a symlink to python-2.7) with no luck.
This is my error and line 507 from the code. Any way to get more info about the error?
Traceback (most recent call last):
File "./jsontest.py", line 507, in <module>
except urllib.error.URLError as e:
AttributeError: 'module' object has no attribute 'error'
#!/usr/bin/python
import urllib
import re
import json
import sys
import getopt
...
# line 507
except urllib.error.URLError as e:
print "fail: ", e.reason
...
update: As noted by #a_guest. Had to make this change. Why it runs fine on 14.04 I don't know. Maybe this was the difference bewteen 2.7.6 and 2.7.12:
except urllib2.error.URLError as e:

You linked the documentation for Python 3 however you seem to be using Python 2. urllib on Python 2 doesn't have that error module, just as the error states.
urllib2 on the other hand has this class, so you can use urllib2.URLError instead.

That error means that the urllib module contains nothing called error.
My urllib doesn't have an error submodule.

Note that Ubuntu 16.04 /usr/bin/python is a symlink to python2. If you want to use python 3.x, you should change the first line of your script from
#!/usr/bin/python
to
#!/usr/bin/python3
(Note: the "#!" at the beginning of this line is called "shebang".)
Alternatively you can create a virtual environment so that when you activate it, it will run python 3.

Related

Running python code give NameError: name 'ABCMeta' is not defined every time

I'm using Python 3.7.3 with PyCharm. When I tried to import abc package into my project I got this error followed with Python quit unexpectedly alert dialog:
Fatal Python error: init_sys_streams: can't initialize sys standard streams
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/io.py", line 52, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/abc.py", line 166, in <module>
NameError: name 'ABCMeta' is not defined
Abort trap: 6
At first I didn't pay much attention to the error and thought I made a mistake in the code. So I commented import abc line. But the error didn't disappear. Moreover, each time I was running python3 programs I was getting the same error. Even in terminal:
Previously I had Anaconda installed, but uninstalled it later.
In my project I'm using pipenv
I also have python 2 but I don't use it. I tried to run it and there was no such error.
Previously I tried to install vptyhon in many ways. I didn't know about pip then, so I could mess up the framework.
Removing /Library/Frameworks/Python.framework file and reinstalling python 3.7.3 resolved the error

Layer [<class 'scapy.layers.snmp.SNMP'>] not found

I'm trying to bruteforce (a rootme challenge don't worry :p) an SNMPv3 authentication password using this python script : https://github.com/cysboy/SnmpCrack/blob/master/SnmpCrack.py
Nevertheless, I have an error loading a scapy snmp module line 72 on this code
snmp = pkt[SNMP]
I printed the error by adding
except Exception as e :
print("Continuing")
print(e)
continue
In the followed try catch.
I get this error : Layer [<class 'scapy.layers.snmp.SNMP'>] not found
So I tried to search where this class should have been declared.
I found a file under /usr/lib/python2.7/dist-packages/scapy/layers/snmp.py where the class is defined.
I tried to import this file manually and other modules with
import sys
sys.path.insert(0, "/usr/lib/python2.7/dist-packages/scapy/layers/")
import snmp
from scapy.all import *
from scapy.layers import *
from scapy.layers.snmp import * #SNMP, SNMPresponse, SNMPvarbind
But I don't really know what I'm doing exactly.
Is there a way to be sure this file is included as a scapy submodule / class ?
I'm running under the latest Kali 64 bit. I tried with python3 aswell without success.
I'm running this script with python 2.7.14+
Thanks if you can help me with that :)
Have a good day
Quite old question already.
As you can see in the code source,
https://github.com/secdev/scapy/blob/master/scapy/layers/snmp.py
the fields do exist in recent versions.
Try uninstalling scapy from apt, and install it via github or via
pip install scapy
It may be a problem of versioning...

Python: ImportError: No module named connector

When I execute a Python script I receive:
Traceback (most recent call last):
File "/Users/.../Documents/development/python/migrate_upper.py", line 3, in <module>
import mysql.connector
ImportError: No module named connector
I am executing like that:
$ python migrate_upper.py
It worked 1 month ago, I haven't worked with Python since then. I spent 2 hours trying to understand what is wrong but I got lost with PYTHONPATH, pip and other hints.
However when I send the script to Python shell:
$ python < migrate_upper.py
everything works. I think that this is not the proper way to execute python scripts. How can I get the script working without the Python shell?
I resolved it with the help of Ethan's comment.
I had a folder mysql beneath ../development/python. I can't remember why I put it there. I guess python tried to import that folder instead of ../site-packages/mysql.

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

I know that such error might appear if I named the file same as importing lib, but there are must be is something different, because the error persist even if try run that code below through the console:
# -*- coding: cp1251
import _mssql
connSQLserver = _mssql.connect(
server="localhost",
port=1433,
user="admin",
password="****",
database="master")
This is output:
AttributeError Traceback (most recent call last)
<ipython-input-18-8ecbd448023e> in <module>()
2 import _mssql
3
----> 4 connSQLserver = _mssql.connect(
5 server="localhost",
6 port=1433,
AttributeError: 'module' object has no attribute 'connect'
I'm trying to connect to locally installed sql server 2014, using pymssql-2.1.1-cp27-none-win_amd64.
I tried even import pymssql , nothing changed. According to official documentation, both library might be used. I use python 2.7.1 installed as a part of Anaconda, windows 7. Connection string might be wrong.
What may cause the error?
upd:
# -*- coding: cp1251
import _mssql
print _mssql
returns
<module '_mssql' (built-in)>
print _mssql.__file__ returns AttributeError: 'module' object has no attribute '__file__'
The issue is simply that .connect(...) must be capitalized like so .Connect(...).
When you install a python module as root, sometimes, the access rights to the modules files are limited for others than root.
Juste give recurse right as root to everyone:
by example for python 2.7 module, installed through pip2.7
chmod -R a+rx /usr/local/lib/python2.7
You can check this is a permission problem by launching program through strace...
I'm not sure what gave you the idea to import _pymssql; the leading underscore should have given you the clue that this is a private utility module. Just import pymssql.
Try _mssql.MSSQLConnection
from

Python 3.2 won't import cookielib

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.

Categories

Resources