How do I determine if my python shell is executing

2018-12-31 14:18发布

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

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

标签: python macos
13条回答
不流泪的眼
2楼-- · 2018-12-31 14:37
import sys
print(sys.version)

3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)]

查看更多
谁念西风独自凉
3楼-- · 2018-12-31 14:40

struct.calcsize("P") returns size of the bytes required to store a single pointer. On a 32-bit system, it would return 4 bytes. On a 64-bit system, it would return 8 bytes.

So the following would return 32 if you're running 32-bit python and 64 if you're running 64-bit python:

Python 2

import struct;print struct.calcsize("P") * 8

Python 3

import struct;print(struct.calcsize("P") * 8)
查看更多
不流泪的眼
4楼-- · 2018-12-31 14:42

On my Centos Linux system I did the following:

1) Started the Python interpreter (I'm using 2.6.6)
2) Ran the following code:

import platform
print(platform.architecture())

and it gave me

(64bit, 'ELF')
查看更多
浪荡孟婆
5楼-- · 2018-12-31 14:44

Basically a variant on Matthew Marshall's answer (with struct from the std.library):

import struct
print struct.calcsize("P") * 8
查看更多
谁念西风独自凉
6楼-- · 2018-12-31 14:46

For a non-programmatic solution, look in the Activity Monitor. It lists the architecture of 64-bit processes as “Intel (64-bit)”.

查看更多
冷夜・残月
7楼-- · 2018-12-31 14:47

When starting the Python interpreter in the terminal/command line you may also see a line like:

Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32

Where [MSC v.1500 64 bit (AMD64)] means 64-bit Python. Works for my particular setup.

查看更多
登录 后发表回答