Missing .copy() on zlib decompressobj in Python - python

According to the Python documentation of the zlib module, decompressobj objects have a copy() function to clone the decompressor's state. This equivalent functionality is also mentioned for the compressing side on StackOverflow here.
However, when I try to use it, it simply doesn't exist:
C:\>C:\Python27\python.exe
Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import zlib
>>> meow = zlib.decompressobj()
>>> purr = meow.copy()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: copy
Is this a bug in Python? I tried searching for this subject, but too many false hits get in the way.

Never mind. It's a known bug that was fixed in newer versions of 2.7. I just needed to check the bug list. For some reason, Google didn't index that bug page.

Related

NameError: name 'create_engine' is not defined in SQLAlchemy

I am attempting to create a python script that connects to an SQLITE database and using SQLAlchemy to help with this.
I am still very early, but am trying to create a connection to a new database but keep getting this "create_engine" is not defined in SQLAlchemy error.
To try to simplify I opened a python terminal to try it... see below:
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlalchemy
>>> engine = create_engine('sqlite:///test.db')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'create_engine' is not defined
>>> print(sqlalchemy.__version__)
1.3.18
>>>
At this point I don't even know where to go looking for the problem.
I did a pip uninstall sqlalchemy then pip install sqlalchemy hoping this might help.
Thanks to Gord Thompson. Changing this line:
engine = create_engine('sqlite:///test.db')
to:
engine = db.create_engine('sqlite:///test.db')
made it work!!

sympy import error in IronPython 2.7

I'm trying to use the sympy library in my python script, but I'm getting an error when I try to import it. How do I fix this?
The library you are using seems to make use of an internal API to get information of the current stack/live objects.
In order to provide the required information and interfaces you have to run IronPython with the -X:FullFrames argument.
Should you plan on hosting IronPython from C# this answer explains the necessary steps.
Instead of the previous situation/error
C:\Program Files (x86)\IronPython 2.7>ipy
IronPython 2.7.5 (2.7.5.0) on .NET 4.0.30319.42000 (32-bit)
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys._getframe(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '_getframe'
you will get the expected behavior
C:\Program Files (x86)\IronPython 2.7>ipy -X:FullFrames
IronPython 2.7.5 (2.7.5.0) on .NET 4.0.30319.42000 (32-bit)
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys._getframe(0)
<frame object at 0x000000000000002B>

Undefined symbol "afinfo" when importing python-iptables package "iptc"

I am trying to utilize the python-iptables package to list iptables rules in a web app. When I add the iptc package to my environment, I get the below error. I used yum 'provides' to find where the libxtables.so.4 file comes from and found that the iptables and iptables-devel packages were the appropriate choice in CentOS 6.4 x64. I upgraded those packages but it did not change the error.
Does anyone have any suggestions about how I can resolve this?
pgrace#ny-misc01:~/repos/python-iptables/libxtwrapper$ python
Python 2.6.6 (r266:84292, Sep 11 2012, 08:34:23)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import iptc
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib64/python2.6/site-packages/iptc/__init__.py", line 10, in
from ip4tc import Table, Chain, Rule, Match, Target, Policy, IPTCError
File "/usr/lib64/python2.6/site-packages/iptc/ip4tc.py", line 11, in
from xtables import (XT_INV_PROTO, NFPROTO_IPV4, XTablesError, xtables,
File "/usr/lib64/python2.6/site-packages/iptc/xtables.py", line 744, in
class xtables(object):
File "/usr/lib64/python2.6/site-packages/iptc/xtables.py", line 757, in xtables
_xtables_afinfo = ct.c_void_p.in_dll(_lib_xtables, "afinfo")
ValueError: /lib64/libxtables.so.4: undefined symbol: afinfo
>>>
See this: https://github.com/ldx/python-iptables/issues/25
This is a known problem, old versions of libxtables declared afinfo as static, thus it is not accessible for python-iptables. There is a possible workaround, though - please keep an eye on the ticket, it will be updated as soon as there has been progress.
Another solution is to update iptables on your machine.
Disclaimer: I am the maintainer of python-iptables.
Update: this should be fixed now.

Error when importing programs IDLE

Im new to Python but Im getting on pretty well, however I cannot seem to import save programs into IDLE. Could someone assist me where that is concerned, please. This is one of the errors no matter how simple the program is:
>>> import dinner
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
import dinner
File "C:\Python25\dinner.py", line 1
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32
^
SyntaxError: invalid syntax
This is the error you would get if your dinner.py program actually started with the line
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32
But that's not a line of valid Python code, that's the message that the interpreter gives on startup. For example, mine says
~/coding$ python
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
and it's then that I start typing things like print 2+2, etc. You can open dinner.py in IDLE and remove any parts which look like this welcome message at the start.
Incidentally, I see you're using Python 2.5.4. This is pretty old now, and a lot of neat things have been added since. I would suggest switching to 2.7.3 instead.

"from _json import..." - python

I am inspecting the JSON module of python 3.1, and am currently in /Lib/json/scanner.py. At the top of the file is the following line:
from _json import make_scanner as c_make_scanner
There are five .py files in the module's directory: __init__ (two leading and trailing underscores, it's formatting as bold), decoder, encoder, scanner and tool. There is no file called "json".
My question is: when doing the import, where exactly is "make_scanner" coming from?
Yes, I am very new to Python!
It's coming from a C-compiled _json.pyd (or _json.so, etc, etc, depending on the platform) that lives elsewhere on the sys.path. You can always find out where that is in your specific Python installation by importing the module yourself and looking at its __file__, e.g.:
>>> import _json
>>> _json.__file__
'/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload/_json.so'
As you see, in my installation of Python 2.6, _json comes from the lib-dynload subdirectory of lib/python2.6, and the extension used on this platform is .so.
It may be coming from a file, or it may be built-in. On Windows, it appears to be built-in.
Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import _json
>>> _json.__file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
and there is no _json.pyd or _json.dll in the offing.
If you want to see the source, having a binary file on your machine or not is irrelevant -- you'll need the SVN browser.

Categories