source

python 쉘이 32비트인지 64비트인지 확인하려면 어떻게 해야 하나요?

goodcode 2022. 9. 4. 15:01
반응형

python 쉘이 32비트인지 64비트인지 확인하려면 어떻게 해야 하나요?

셸 내부에서 셸이 어떤 모드인지 알 수 있는 방법이 필요합니다.

주로 OS X 사용자이지만 다른 플랫폼에 대해서도 알고 싶습니다.

플랫폼 모듈을 살펴보았지만 "실행 파일에 사용되는 비트 아키텍처와 링크 포맷에 대하여"만 알 수 있는 것 같습니다. 그러나 바이너리는 64비트로 컴파일되어 있습니다(OS X 10.6에서 실행 중이기 때문에 여기서 설명하는 방법을 사용하여 32비트 모드로 이행하더라도 항상 64비트를 보고하는 것 같습니다.

가지 은 을 입니다.sys.maxsize다음 문서에 기재되어 있습니다.

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

Windows 에서는, 다음과 같은 형식의 명령어를 실행합니다.

python -c "import sys;print(\"%x\" % sys.maxsize, sys.maxsize > 2**32)"

sys.maxsizePython 2.6 。오래된 시스템에 대한 테스트가 필요한 경우 이 조금 더 복잡한 테스트는 모든 Python 2 및 3 릴리스에서 작동합니다.

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

은 "" "" " " " " " " " " " " 를 사용하고 수 .platform.architecture()이걸 위해서.유감스럽게도, 특히 OS X 유니버설 바이너리의 경우, 그 결과가 항상 신뢰할 수 있는 은 아닙니다.

$ 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

터미널/명령줄에서 Python 인터프리터를 시작할 때 다음과 같은 행이 표시될 수도 있습니다.

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

서 ★★★★★[MSC v.1500 64 bit (AMD64)]64년 파이톤내 특정 설정에 대해 작동한다.

기본적으로 Matthew Marshall의 답변(Std.Library의 구조 포함):

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

ctype을 사용하여 void 포인터의 크기를 가져옵니다.

import ctypes
print ctypes.sizeof(ctypes.c_voidp)

32비트는 4개, 64비트는 8개입니다.

python 콘솔 열기:

import platform
platform.architecture()[0]

플랫폼에 따라 '64비트' 또는 '32비트'가 표시됩니다.

또는 (OS X 바이너리의 경우):

import sys
sys.maxsize > 2**32 
# it should display True in case of 64bit and False in case of 32bit

Centos 리눅스

Python (하고 있습니다)1) Python 인터프리터를 기동 (2.6.6을 사용하고 있습니다)
2)했습니다.2) 음음음 。

import platform
print(platform.architecture())

그리고 그것은 나에게

(64bit, 'ELF')

비프로그래밍 솔루션의 경우 Activity Monitor를 참조하십시오.64비트 프로세스의 아키텍처를 「Intel(64비트)」라고 리스트 하고 있습니다.

모든 것을 그룹화하는 중...

그 점을 고려하면:

  • OSX에 대한 질문입니다(오래된 Python 버전이 있는 오래된(및 금이 ) VM이 있습니다).
  • 나의 주된 환경은 윈이다.
  • Win에는 32비트 버전만 설치되어 있습니다(Lnx에는 "파손된" 버전을 구축했습니다).

Python 3와 Python 2를 사용하여 3가지 플랫폼 모두에서 예를 들어 보겠습니다.

  1. [Python 3]를 선택합니다.Docs]: sys.max size 값 - 와 비교0x100000000(2 ** 32): 64비트의 경우 더 크고, 32비트의 경우 더 작음:
    • OSX 9 x 64:
      • Python 2.7.10 x 64:
        >>> 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)
        
    • Ubuntu 16 x 64:
      • Python 3.5.2 x64:
        >>> 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)
        
      • Python 3.6.4 x86:
        >>> 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)
        
    • 10 x 64:
      • Python 3.5.4 x 64:
        >>> 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)
        
      • Python 3.6.2 x86:
        >>> 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)
        


  1. [Python 3]를 사용합니다.Docs]: structure.calcsize(format)사용하여 (포인터) 형식으로 생성되는 오브젝트 크기를 결정합니다.즉, 포인터 크기를 결정합니다(sizeof(void*)):
    • OSX 9 x 64:
      • Python 2.7.10 x 64:
        >>> import struct
        >>> struct.calcsize("P") * 8
        64
        
    • Ubuntu 16 x 64:
      • Python 3.5.2 x64:
        >>> import struct
        >>> struct.calcsize("P") * 8
        64
        
      • Python 3.6.4 x86:
        >>> import struct
        >>> struct.calcsize("P") * 8
        32
        
    • 10 x 64:
      • Python 3.5.4 x 64:
        >>> import struct
        >>> struct.calcsize("P") * 8
        64
        
      • Python 3.6.2 x86:
        >>> import struct
        >>> struct.calcsize("P") * 8
        32
        


  1. [Python 3]를 사용합니다.Docs]: ctypes - Python용 외부 함수 라이브러리.또한 포인터의 크기를 결정하는 것으로 요약됩니다.sizeof(void*)주의사항으로 ctype은 ${PYThon_SRC_DIR}/Lib/ctypes/_init_.py」( #15)를 통해 #2를 사용합니다.
    • OSX 9 x 64:
      • Python 2.7.10 x 64:
        >>> import ctypes
        >>> ctypes.sizeof(ctypes.c_void_p) * 8
        64
        
    • Ubuntu 16 x 64:
      • Python 3.5.2 x64:
        >>> import ctypes
        >>> ctypes.sizeof(ctypes.c_void_p) * 8
        64
        
      • Python 3.6.4 x86:
        >>> import ctypes
        >>> ctypes.sizeof(ctypes.c_void_p) * 8
        32
        
    • 10 x 64:
      • Python 3.5.4 x 64:
        >>> import ctypes
        >>> ctypes.sizeof(ctypes.c_void_p) * 8
        64
        
      • Python 3.6.2 x86:
        >>> import ctypes
        >>> ctypes.sizeof(ctypes.c_void_p) * 8
        32
        


  1. [피톤3]문서]: platform.architecture(sys 파일)실행 파일, bits='', link=')!! OSX에서는 신뢰성이 떨어진다!!!multi arch 실행 파일(또는 .dylib) 형식(경우에 따라서는 #2 사용):
    • OSX 9 x 64:
      • Python 2.7.10 x 64:
        >>> import platform
        >>> platform.architecture()
        ('64bit', '')
        
    • Ubuntu 16 x 64:
      • Python 3.5.2 x64:
        >>> import platform
        >>> platform.architecture()
        ('64bit', 'ELF')
        
      • Python 3.6.4 x86:
        >>> import platform
        >>> platform.architecture()
        ('32bit', 'ELF')
        
    • 10 x 64:
      • Python 3.5.4 x 64:
        >>> import platform
        >>> platform.architecture()
        ('64bit', 'WindowsPE')
        
      • Python 3.6.2 x86:
        >>> import platform
        >>> platform.architecture()
        ('32bit', 'WindowsPE')
        


  1. 어설픈 회피책(게인) - 외부 명령어([man7]): [Python 3]를 통한 파일(1).Docs]: os.system(명령어).#4.의 제한이 적용됩니다(가끔 작동하지 않을 수도 있습니다).
    • OSX 9 x 64:
      • Python 2.7.10 x 64:
        >>> 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
        
    • Ubuntu 16 x 64:
      • Python 3.5.2 x64:
        >>> 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
        
      • Python 3.6.4 x86:
        >>> 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
        
    • 10 x 64:
      • 파일 유틸리티가 존재하지 않습니다.사용할 수 있는 다른rd 3가지 Party 툴이 있습니다만, 그것들을 고집하지 않습니다.


특정 획득:

  1. [ Python 3 ]를 통해 env vars ( %PROCESSOR _ ARCHITECTURE % ( or other )를 확인합니다.문서]: os.environment:
    • 10 x 64:
      • Python 3.5.4 x 64:
        >>> import os
        >>> os.environ["PROCESSOR_ARCHITECTURE"]
        'AMD64'
        
      • Python 3.6.2 x86:
        >>> import os
        >>> os.environ["PROCESSOR_ARCHITECTURE"]
        'x86'
        


  1. [피톤3]Docs]: sys.version(인터프리터를 기동할 때도 1행으로st 표시)
    • #1을 체크합니다.

32비트의 경우 32를 반환하고 64비트의 경우 64를 반환합니다.

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

platform.architecture() 메모에는 다음과 같습니다.

참고: Mac OS X(및 다른 플랫폼)에서는 실행 파일이 여러 아키텍처를 포함하는 범용 파일일 수 있습니다.

현재 인터프리터의 "64비트"를 얻으려면 sys.maxsize Atribute를 쿼리하는 것이 신뢰성이 높아집니다.

import sys
is_64bits = sys.maxsize > 2**32

해요.python -VV커맨드 라인에 표시됩니다.버전이 반환됩니다.

Windows 10의 경우

아래 그림과 같이 > python을 입력하여 cmd termial을 열고 python interpreter를 실행합니다.

잉그

처음에 인터프리터 정보에 AMD64가 포함되어 있으면 64비트, 그렇지 않으면 32비트입니다.

이것을 시험해 보세요.

import platform
platform.architecture()

abe32의 답변에 따르면

import sys
n_bits = 32 << bool(sys.maxsize >> 32)

n_bits는 32비트 또는 64비트를 가집니다.

struct.calcsize("P")단일 포인터를 저장하는 데 필요한 바이트 크기를 반환합니다.32비트 시스템에서는 4바이트를 반환합니다.64비트 시스템에서는 8바이트를 반환합니다.

다음은 라음음음음음음음음음 。32과 32비트 python을 실행하고 있는64 python64비트 python을 실행하고 있는:

파이썬 2

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

파이썬 3

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

cmd 단위로 python을 친 후

import sys
print(sys.version)

3.5.1 (v3.5.1:37a07cee5969, 2015년 12월 6일 01:54:25) [MSC v.1900 64비트(AMD64)]

플랫폼 아키텍처는 신뢰할 수 있는 방법이 아닙니다.대신 우리:

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

platform.architecture()이치노

★★★★★★★★★★★의 편리한 테스트sys.maxsize > 2**32. Py2.6 이 py .

Py2, Py2:struct.calcsize('P') == 8ctypes.sizeof(ctypes.c_void_p) == 8

옵션인 gcc를 사용하여 빌드할 수 있습니다.-mx3264비트 아키텍처 애플리케이션이지만 32비트 포인터를 기본값으로 사용합니다(메모리와 속도를 절약합니다).=는C크기를 수 'sys.maxsize = ssize_t').2**31 - 1( 코드와 이 있어, 「 모드 64비트 모드판별하는 하게 할 필요가 .또한 코드와 데이터의 포인터 크기가 다른 시스템이 있어 "32비트 또는 64비트 모드"를 식별하는 목적이 정확히 무엇인지 명확히 할 필요가 있습니다.

언급URL : https://stackoverflow.com/questions/1405913/how-do-i-determine-if-my-python-shell-is-executing-in-32bit-or-64bit

반응형