String formatting error - python

Using the code print('{0} is not'.format('That that is not')) in Python 3.1.1, I get the following error:
AttributeError: 'str' object has no attribute 'format'
when I delete the line Netbeans automatically inserted at the beginning:
from distutils.command.bdist_dumb import format
which itself causes an error of
ImportError: cannot import name format
What am I doing wrong here?

You must be running an older version of Python. This does work in Python 3.1.1+:
$ python3
Python 3.1.1+ (r311:74480, Nov 2 2009, 14:49:22)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> '{0} is not'.format('That that is not')
'That that is not is not'
You will, however, get this error in Python 2.5.4:
$ python2.5
Python 2.5.4 (r254:67916, Jan 20 2010, 21:44:03)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> '{0} is not'.format('That that is not')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'format'
This feature seems to have been backported to Python 2.6, so you won't get this error there. You must be running Python < 2.6.

Related

Module attribute mysteriously appears

$ python
Python 3.8.10 (default, Nov 14 2022, 12:59:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> urllib.request
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'urllib' has no attribute 'request'
>>> urllib.request
<module 'urllib.request' from '/usr/lib/python3.8/urllib/request.py'>
Why is urllib.request recognized as an attribute the second time I try to access it?

Python: os.environ KeyError after export

Why python can't get evironment variable after export?
How to fix this?
λ: export AUTOTEST=/home/anton/eltx/scripts/gitlab/
~
λ: python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['AUTOTEST']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/UserDict.py", line 40, in __getitem__
raise KeyError(key)
KeyError: 'AUTOTEST'
>>>
>>> os.environ.get('AUTOTEST', 'none_value')
'none_value'
>>>
I found the mistake. There was an alias for running python

string.maketrans unicode handling: pypy vs CPython

Pypy doesn't seem to handle string.maketrans() when arguments are unicode, however CPython does:
$ python
Python 2.7.5 (default, Oct 11 2013, 14:51:32)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.maketrans(ur"-/[] ", ur"_____")
'\x00\x01\x02\x03\x04\x05\x06...'
$ pypy
Python 2.7.13 (c925e73810367cd960a32592dd7f728f436c125c, Jun 08 2017, 19:14:08)
[PyPy 5.8.0 with GCC 6.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> import string
>>>> string.maketrans(ur"-/[] ", ur"_____")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../pypy-5.8-linux_x86_64-portable/lib-python/2.7/string.py", line 78, in maketrans
buf[ord(fromstr[i])] = tostr[i]
TypeError: 'unicode' object cannot be interpreted as an index
Didn't find anything relevant on http://pypy.readthedocs.io/en/latest/cpython_differences.html.
Is this a bug of CPython or PyPy?
That's a "bug", i.e. an unexpected difference. Fixed in 7fe0041fccaa (see line 78 of https://bitbucket.org/pypy/pypy/raw/default/lib-python/2.7/string.py).

how to address Python module inspect not having function getcallargs

I am trying to use the module inspect in two different environments. In one of the environments, everything is fine. In the other, inspect appears to be missing the function getcallargs. I am not sure what's going wrong. I'm also not sure how to check the version of inspect that is being used in each environment. How can I get inspect to work in the problematic environment?
The environment that works fine is as follows:
user1#computer1:~>python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> print inspect.getcallargs
<function getcallargs at 0x7ff122c0a578>
The environment that breaks is as follows:
(virtual_environment)-bash-4.1$ python
Python 2.6.6 (r266:84292, Jan 23 2014, 10:39:35)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> print inspect.getcallargs
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'getcallargs'
In Python 2.6 the inspect module does not have the getcallargs function.
https://docs.python.org/release/2.6/library/inspect.html
Python 2.7 does have getcallargs
https://docs.python.org/2/library/inspect.html

'module' object has no attribute 'getusersitepackages'

I am going through the python tutorial and seem to be missing something very basic. The tutorial has:
>>> import site
>>> site.getusersitepackages()
'/home/user/.local/lib/python3.2/site-packages'
But I get the following:
$ python
Python 2.6.8 (unknown, Jun 9 2012, 11:30:32)
[GCC 4.5.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> site.getusersitepackages()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'getusersitepackages'
As it says above, I am running Python 2.6.8 on cygwin.
Is this installation error?
According to the docs, this feature/function was added in python2.7 -- You're still on python 2.6

Categories

Resources