프로그래밍/python

파이썬 여러 버전을 설치했을 때 팁

콘파냐 2017. 3. 12. 20:13
반응형

현 시점에서 파이썬으로 개발을 할 때 고려해야할 사항 중에 어떤 버전을 사용할 것인가 결정해야 한다. 2.x버전과 3.x버전을 엄연히 다르다. 일반적으로 버전의 젤 첫 번째 숫자는 호환성에 대한 것이므로 2와 3은 호환성이 없다고 봐야한다. 2.x버전의 경우 향 후 5년간만 지원하고 그 뒤에는 3.x버전만 지원되기 때문에 사실상 2.7버전을 사용해야할 이유는 없다. 그럼에도 2.x 버전이 필요한 이유는 지금까지 사용해왔기 때문에 지금까지 구축되온 구조를 한번에 

3.x버전으로 바꾸는 것은 힘들기 때문이다. 파이썬을 사용하다 보면 느끼겠지만 아직까지는 쓸모있는 많은 도구들이 2.7에서 제대로 작동하고 3.x에서는 불완전 또는 작동하지 않는 경우를 많이 봐왔다. 그래서 가끔 2.7에서 개발을 해야할 필요도 생기게 되는데 2.x 버전과 3.x 버전을 번갈아 사용하기 위해서 환경변수를 매번 바꾸기는 귀찮을 것이다. python2.7 또는 python3.5라고 바꿔도 되겠지만 위 방법 보다는 py라는 론쳐를 사용해 보자.



파이썬 3.3 이후 버전을 설치했다면 windows 폴더에 py.exe라는 론쳐가 설치된다. 자세한 사용법은 다음과 같다.

Launcher arguments:


-2     : Launch the latest Python 2.x version

-3     : Launch the latest Python 3.x version

-X.Y   : Launch the specified Python version

-X.Y-32: Launch the specified 32bit Python version


The following help text is from Python:


usage: C:\Python27\python.exe [option] ... [-c cmd | -m mod | file | -] [a

Options and arguments (and corresponding environment variables):

-B     : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE

-c cmd : program passed in as string (terminates option list)

-d     : debug output from parser; also PYTHONDEBUG=x

-E     : ignore PYTHON* environment variables (such as PYTHONPATH)

-h     : print this help message and exit (also --help)

-i     : inspect interactively after running script; forces a prompt even

         if stdin does not appear to be a terminal; also PYTHONINSPECT=x

-m mod : run library module as a script (terminates option list)

-O     : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x

-OO    : remove doc-strings in addition to the -O optimizations

-R     : use a pseudo-random salt to make hash() values of various types b

         unpredictable between separate invocations of the interpreter, as

         a defense against denial-of-service attacks

-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew

-s     : don't add user site directory to sys.path; also PYTHONNOUSERSITE

-S     : don't imply 'import site' on initialization

-t     : issue warnings about inconsistent tab usage (-tt: issue errors)

-u     : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x

         see man page for details on internal buffering relating to '-u'

-v     : verbose (trace import statements); also PYTHONVERBOSE=x

         can be supplied multiple times to increase verbosity

-V     : print the Python version number and exit (also --version)

-W arg : warning control; arg is action:message:category:module:lineno

         also PYTHONWARNINGS=arg

-x     : skip first line of source, allowing use of non-Unix forms of #!cm

-3     : warn about Python 3.x incompatibilities that 2to3 cannot triviall

file   : program read from script file

-      : program read from stdin (default; interactive mode if a tty)

arg ...: arguments passed to program in sys.argv[1:]


Other environment variables:

PYTHONSTARTUP: file executed on interactive startup (no default)

PYTHONPATH   : ';'-separated list of directories prefixed to the

               default module search path.  The result is sys.path.

PYTHONHOME   : alternate <prefix> directory (or <prefix>;<exec_prefix>).

               The default module search path uses <prefix>\lib.

PYTHONCASEOK : ignore case in 'import' statements (Windows).

PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.

PYTHONHASHSEED: if this variable is set to 'random', the effect is the sam

   as specifying the -R option: a random value is used to seed the hashes

   str, bytes and datetime objects.  It can also be set to an integer

   in the range [0,4294967295] to get hash values with a predictable seed.


2.7버전과 3.5버전만 설치되었다면 간단히

>py -2

>py -3

이렇게 실행할 수 있다.


python 3.3과 python 3.5가 같이 설치되어 있다면

>py -3.3

>py -3.5

와 같이 실행하면 된다.


pip을 사용할 때는 -m 옵션을 주어 py 명령라인에서 pip을 실행하면 된다. 

>py -3.3 -m pip install packagename


윈도우에서만 되므로 리눅스의 경우는 해당되지 않는다.

반응형