Ich brauche eine Möglichkeit, um festzustellen, in welchem Modus sich die Shell innerhalb der Shell befindet.
Ich habe versucht, das Plattform - Modul zu betrachten, aber es scheint nur "über die Bitarchitektur und das für die ausführbare Datei verwendete Verknüpfungsformat" zu erzählen: Die Binärdatei ist jedoch als 64-Bit kompiliert (ich bin es) Laufen auf OS X 10.6), so scheint es immer 64-Bit zu melden, obwohl ich die Methoden verwende hier beschrieben 32-Bit-Modus erzwingen).
AKTUALISIERT: Eine Möglichkeit ist, sys.maxsize
Wie dokumentiert anzusehen hier :
$ 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
Wurde in Python 2.6 eingeführt. Wenn Sie einen Test für ältere Systeme benötigen, sollte dieser etwas kompliziertere Test auf allen funktionieren Python 2 und 3 Veröffentlichungen:
$ python-32 -c 'import struct;print( 8 * struct.calcsize("P"))'
32
$ python-64 -c 'import struct;print( 8 * struct.calcsize("P"))'
64
Übrigens könnten Sie versucht sein, platform.architecture()
dafür zu verwenden. Leider sind die Ergebnisse nicht immer zuverlässig, insbesondere bei OS X-Universal-Binärdateien .
$ 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
Wenn Sie den Interpreter Python in der Terminal-/Befehlszeile starten, sehen Sie möglicherweise auch eine Zeile wie:
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Wobei [MSC v.1500 64 bit (AMD64)]
64-Bit-Python bedeutet. Funktioniert für mein spezielles Setup.
Grundsätzlich eine Variante von Matthew Marshalls Antwort (mit struct aus der std.library):
import struct
print struct.calcsize("P") * 8
Versuchen Sie es mit ctypes, um die Größe eines leeren Zeigers zu erhalten:
import ctypes
print ctypes.sizeof(ctypes.c_voidp)
Es wird 4 für 32 Bit oder 8 für 64 Bit sein.
Öffne python console:
import platform
platform.architecture()[0]
je nach Plattform sollte "64bit" oder "32bit" angezeigt werden.
Alternativ ( bei OS X-Binärdateien ):
import sys
sys.maxsize > 2**32
# it should display True in case of 64bit and False in case of 32bit
Auf meinem Centos Linux-System habe ich Folgendes ausgeführt:
1) Startete den Python Interpreter (ich benutze 2.6.6)
2) Führen Sie den folgenden Code aus:
import platform
print(platform.architecture())
und es gab mir
(64bit, 'ELF')
Eine nicht programmgesteuerte Lösung finden Sie im Aktivitätsmonitor. Es listet die Architektur von 64-Bit-Prozessen als „Intel (64-Bit)“ auf.
Alles gruppieren ...
Bedenkt, dass:
Ich werde dies auf allen 3 Plattformen mit Python 3 und Python 2 veranschaulichen.
0x100000000
(2 ** 32
): Größer für 64bit, kleiner für 32bit: >>> import sys >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform) 'Python 2.7.10 (default, Oct 14 2015, 05:51:29) \n[GCC 4.8.2] on darwin' >>> hex(sys.maxsize), sys.maxsize > 0x100000000 ('0x7fffffffffffffff', True)
>>> import sys >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform) 'Python 3.5.2 (default, Nov 23 2017, 16:37:01) \n[GCC 5.4.0 20160609] on linux' >>> hex(sys.maxsize), sys.maxsize > 0x100000000 ('0x7fffffffffffffff', True)
>>> import sys >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform) 'Python 3.6.4 (default, Apr 25 2018, 23:55:56) \n[GCC 5.4.0 20160609] on linux' >>> hex(sys.maxsize), sys.maxsize > 0x100000000 ('0x7fffffff', False)
>>> import sys >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform) 'Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32' >>> hex(sys.maxsize), sys.maxsize > 0x100000000 ('0x7fffffffffffffff', True)
>>> import sys >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform) 'Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32' >>> hex(sys.maxsize), sys.maxsize > 0x100000000 ('0x7fffffff', False)
sizeof(void*)
): >>> import struct >>> truct.calcsize("P") * 8 64
>>> import struct >>> truct.calcsize("P") * 8 64
>>> import struct >>> truct.calcsize("P") * 8 32
>>> import struct >>> truct.calcsize("P") * 8 64
>>> import struct >>> truct.calcsize("P") * 8 32
sizeof(void*)
). Als Hinweis: ctypes verwendet # 2. (nicht unbedingt dafür Aufgabe) über "$ {PYTHON_SRC_DIR}/Lib/ctypes/__ init __. py" (um Zeile # 15 ): >>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 64
>>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 64
>>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 32
>>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 64
>>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 32
>>> import platform >>> platform.architecture() ('64bit', '')
>>> import platform >>> platform.architecture() ('64bit', 'ELF')
>>> import platform >>> platform.architecture() ('32bit', 'ELF')
>>> import platform >>> platform.architecture() ('64bit', 'WindowsPE')
>>> import platform >>> platform.architecture() ('32bit', 'WindowsPE')
>>> import os >>> os.system("file {0:s}".format(os.path.realpath(sys.executable))) /opt/OPSWbuildtools/2.0.6/bin/python2.7.global: Mach-O 64-bit executable x86_64
>>> import os >>> os.system("file {0:s}".format(os.path.realpath(sys.executable))) /usr/bin/python3.5: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=59a8ef36ca241df24686952480966d7bc0d7c6ea, stripped
>>> import os >>> os.system("file {0:s}".format(os.path.realpath(sys.executable))) /home/cfati/Work/Dev/Python-3.6.4/python: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=5c3d4eeadbd13cd91445d08f90722767b0747de2, not stripped
>>> import os >>> os.environ["PROCESSOR_ARCHITECTURE"] 'AMD64'
>>> import os >>> os.environ["PROCESSOR_ARCHITECTURE"] 'x86'
platform.architecture()
Notizen sagen:
Hinweis: Unter Mac OS X (und möglicherweise auf anderen Plattformen) können ausführbare Dateien universelle Dateien sein, die mehrere Architekturen enthalten.
Um die „64-Bit-Qualität“ des aktuellen Interpreters zu ermitteln, ist es zuverlässiger, das Attribut sys.maxsize abzufragen:
import sys
is_64bits = sys.maxsize > 2**32
struct.calcsize("P")
gibt die Größe der Bytes zurück, die zum Speichern eines einzelnen Zeigers erforderlich sind. Auf einem 32-Bit-System würden 4 Bytes zurückgegeben. Auf einem 64-Bit-System würden 8 Bytes zurückgegeben.
Das Folgende würde also 32
Wenn Sie 32-Bit ausführen python und 64
Wenn Sie 64-Bit-Python ausführen:
Python 2
import struct;print struct.calcsize("P") * 8
Python
import struct;print(struct.calcsize("P") * 8)
C:\Users\xyz>python
Python 2.7.6 (default, Nov XY ..., 19:24:24) **[MSC v.1500 64 bit (AMD64)] on win
32**
Type "help", "copyright", "credits" or "license" for more information.
>>>
nach dem Drücken von python in cmd
import sys
print(sys.version)
3.5.1 (v3.5.1: 37a07cee5969, 6. Dezember 2015, 01:54:25) [MSC v.1900 64 Bit (AMD64)]
Plattformarchitektur ist nicht der zuverlässige Weg. Stattdessen uns:
$ Arch -i386 /usr/local/bin/python2.7
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform, sys
>>> platform.architecture(), sys.maxsize
(('64bit', ''), 2147483647)
>>> ^D
$ Arch -x86_64 /usr/local/bin/python2.7
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform, sys
>>> platform.architecture(), sys.maxsize
(('64bit', ''), 9223372036854775807)