how to address Python module inspect not having function getcallargs - python

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

Related

None difference for Python versions

I've read a lot about Python versions differences but never met those
Python 2.7.18 (default, Mar 8 2021, 13:02:45)
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> None.__eq__("abc")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute '__eq__'
and
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> None.__eq__("abc")
NotImplemented
I found it already being in Python 3.2
Python 3.2.6 (default, Jan 18 2016, 19:21:14)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> None.__eq__("abc")
NotImplemented
What I wan't to know if it is told somewhere in docs or somewhere. Didn't find anything here. Any sources?

Python failing in sub directory, but working in home directory

I have an Ubuntu machine (18.04) on a VM. When I run python in the home directory, everthing responds normally, however when I run from a sub directory it is failing to import modules in the standard library.
The sequence below illustrates the problem
anon#anon-VirtualBox:~$ python
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>import os
>>> os.environ['PATH']
'/bin:/home/anon/anaconda2/bin:/home/anon/bin:/home/anon/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin'
>>> import logging
>>> exit()
Which is the expected behaviour.
However when I go into a subdirectory, the same operation fails
anon#anon-VirtualBox:~$ cd GitHub/bikeano
anon#anon-VirtualBox:~/GitHub/bikeano$ python
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['PATH']
'/bin:/home/anon/anaconda2/bin:/home/anon/bin:/home/anon/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin'
>>> import logging
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "logging.py", line 5, in <module>
import logging.handlers
ImportError: No module named handlers
>>>
I do not understand what is happening here. Are there other environment variables which could affect this?
Also, this is occuring on a Virtualbox VM, and the same .vdi works normally on another machine? The host is Windows 10 on both machines.

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).

Return value of 'print' function?

I'm python beginner.
I am wondering what the return value of the 'print' function is.
I tried type(print(3)) and didn't work.
I tried to find the api document but I could only find the pprint function.
print() returns None in Python3.
$ python3
Python 3.3.2+ (default, Feb 28 2014, 00:52:16)
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> type(print(3))
3
<class 'NoneType'>
In Python2 print is a statement, so doesn't return anything...
$ python2
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> type(print(3))
File "<stdin>", line 1
type(print(3))
^
SyntaxError: invalid syntax
...unless you use from __future__ import print_function
$ python2
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import print_function
>>> type(print(3))
3
<type 'NoneType'>

String formatting error

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.

Categories

Resources