I'm using python 2.3.2. In script I want to split a string based on a delimeter. So calling rpartition()
. But python is showing the following error
AttributeError: 'str' object has no attribute 'rpartition'
But the python interpreter is executing:
>>> cmd
'CHG-CELL-PARAM:CELL_IDX=0fff,NYL=43;3'
>>> cmdStr=cmd.rpartition(";")
>>> cmdStr
('CHG-CELL-PARAM:CELL_IDX=0fff,NYL=43', ';', '3')
>>>
While in interpreter 'str' object has:
>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
'__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find',
'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',
'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip',
'swapcase', 'title', 'translate', 'upper', 'zfill']
>>>
the same output in my script gives:
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind', 'rindex', 'rjust', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
The str function is contained in interpreter, but not in my script. I have only 1 python installed and both script and interpreter use the same python exe. Is there anything I missed?
According to the documentation, str.rpartition
was introduced in Python 2.5 . This means that your interactive interpreter is at least that new, even if your script is running in an insanely old version (2.3.2 dates from 2003). In fact, since your strings in the interactive interpreter have .format
, that means it is at least 2.6. The interactive interpreter should tell you its version when you open it - eg,
lvc@tiamat:~$ python
Python 3.2.3 (default, Apr 23 2012, 23:14:44)
[GCC 4.7.0 20120414 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
What is most likely happening is either:
./myscript.py
, and the shebang line is set to such a non-system PythonThe easiest way to test this is to explicitly run it in the system version of Python by doing python myscript.py
in a terminal. Also, in the second case, you might try copying and pasting the shebang command (the first line, minus the initial #!
) into your terminal and see what Python version you end up with.