Asked  7 Months ago    Answers:  5   Viewed   30 times

I need a way to tell what mode the shell is in from within the shell.

While I'm primarily an OS X user, I'd be interested in knowing about other platforms as well.

I've tried looking at the platform module but it seems only to tell you about "about the bit architecture and the linkage format used for the executable": the binary is compiled as 64bit though (I'm running on OS X 10.6) so it seems to always report 64bit even though I'm using the methods described here to force 32bit mode).



One way is to look at sys.maxsize as documented here:

$ python-32 -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)'
('7fffffff', False)
$ python-64 -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)'
('7fffffffffffffff', True)

sys.maxsize was introduced in Python 2.6. If you need a test for older systems, this slightly more complicated test should work on all Python 2 and 3 releases:

$ python-32 -c 'import struct;print( 8 * struct.calcsize("P"))'
$ python-64 -c 'import struct;print( 8 * struct.calcsize("P"))'

BTW, you might be tempted to use platform.architecture() for this. Unfortunately, its results are not always reliable, particularly in the case of OS X universal binaries.

$ arch -x86_64 /usr/bin/python2.6 -c 'import sys,platform; print platform.architecture()[0], sys.maxsize > 2**32'
64bit True
$ arch -i386 /usr/bin/python2.6 -c 'import sys,platform; print platform.architecture()[0], sys.maxsize > 2**32'
64bit False
Tuesday, June 1, 2021
answered 7 Months ago

Check out the IPy module. If has a function iptype() that seems to do what you want:

>>> from IPy import IP
>>> ip = IP('')
>>> ip.iptype()
Tuesday, June 29, 2021
answered 6 Months ago

from python docs:

When you run a Python module with


the code in the module will be executed, just as if you imported it, but with the __name__ set to "__main__". That means that by adding this code at the end of your module:

if __name__ == '__main__':
    # Running as a script

you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the “main” file

Tuesday, August 3, 2021
answered 4 Months ago

Look at the first line of the pip3 script.

The first line (starting with #! should point to the same executable as the symbolic link for python 3:

> head -n 1 /usr/local/bin/pip

> ls -ld /usr/local/bin/python3
lrwxr-xr-x  1 root  wheel  9 Dec 25 22:37 /usr/local/bin/python3@ -> python3.6

If this is not the case, deinstall pip and install it again with the correct Python version.


If you really want to make sure that you're using the the right Python with pip, then call it as a module like this:

python3.7 -m pip list

If you get the error No module named pip, then pip is not installed for this version of python.

Sunday, August 22, 2021
answered 4 Months ago

First, it's generally considered bad practice to rely on system python for user land code if you can avoid it. You need to assume that system utilities require a specific version of system python, and your user land code may then be locked to that python version forever, which is not wise (unless you're writing system utilities, in which case just use /bin/python, but then you wouldn't be asking this question...).

Secondly, I am unclear why you need 2.7.10 instead of 2.7.13. All pythons with the same minor revision number (2.7) should always be compatible. If you needed 2.6, that would be a different story since that's a change in minor version. Code written for 2.7.x should all be compatible.

However, assuming your use case really does require using a specific Python version - getting to an actual solution now - be sure sure you really upgraded system python to begin with. If you enter the command: which python, do you get /usr/bin/python (system) or /usr/local/bin/python (brew installed user-land python). For example, /usr/bin/python -V gives me 2.7.10 even though python -V gives me 2.7.13 (via brew).

It's possible that you installed the latest python 2.7.x via brew which puts /usr/local/bin/python as a symlink in your $PATH, or you perhaps have a python alias pointing somewhere you don't want. Verify your $PATH order.

You can reset your homebrew python by removing it (brew uninstall python), or by changing the symlink (ln -s -f /usr/bin/python /usr/local/bin/python). However, using virtualenv removes the need for much of these sorts of gymnastics.

If you want to monkey with prior versions of Python installed via homebrew, this answer should help: How to install older formula using Brew?

One final option: if you absolutely must have a specific python version, pyenv can help.

brew install pyenv
pyenv install 2.7.10
pyenv global 2.7.10
Thursday, October 7, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :