[Mac] Python 환경 – virtualenv

Published onesixx on

GCC설치

Python설치전에 GCC를 설치해야 하는데, 
XCode를 설치하거나, 더 경량 버전인 Command Line Tools 또는  OSX-GCC-Installer 패키지에서도 얻을 수 있다. (그냥 쉽게 Xcode를 설치한다) 

그렇지만, 이미 XCode를 설치했으면 OSX-GCC-Installer는 설치하면 안된다.
두개를 모두 설치하면 원인을 밝히 어려운 문제를 일으킬 수 있다.

설치확인

⇒ gcc -v

패키지 관리자 :: Homebrew

 설치

OS X는 많은 UNIX 유틸리티를 갖고 있지만, 훌륭한 패키지 매니저(cf. Ubuntu의 apt-get)가 없기 때문에  Homebrew 를 설치한다. 설치는 홈페이지에 아래 코드 copy해서 실행

⇒ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

환경변수

Homebrew가 설치됐으면 Homebrew 디렉토리를 PATH 환경 변수의 최상단에 넣자.
~/.bash_profile 파일의 마지막 줄에 다음과 같이 덧붙이면 된다.

export PATH="/usr/local/bin:$PATH"
export PATH="/usr/local/sbin:$PATH"

설치확인

⇒ brew -v
Homebrew 0.9.9 (git revision 96cb; last commit 2016-06-17)
Homebrew/homebrew-core (git revision f7be; last commit 2016-06-19)

*참고 : 디렉토리 

/usr/local/brew에 설치한 소프트웨어의 key들이 있고, 
/usr/local/Cellar에 설치한 packages들의 파일이 있다.  
/usr/local/bin에는 /usr/local/Cellar에 설치한 packages들의 symlink가 있다.

Python3 설치

➜  python

WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.

Python 2.7.16 (default, Feb 29 2020, 01:55:37)
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
➜  brew update
➜  brew install python3

python 3 설치확인

➜  python3
Python 3.7.7 (default, Mar 22 2020, 05:14:51)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

pip is installed automatically

➜  pip3 --version
pip 20.0.2 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

➜  which pip3
/usr/local/bin/pip3

VirtualEnv

  • 로컬에 다양한 파이썬 환경 구축
  • 프로젝트 별 패키지 의존성 해결

설치

➜  pip3 install virtualenv

➜  virtualenv --version
virtualenv 20.0.17 from /usr/local/lib/python3.7/site-packages/virtualenv/__init__.py

특정폴더(testPrj)에 virtualEnv (pypy)만들기

➜  cd ~/sixxDoc/RCODE/testPrj
➜  virtualenv pypy
created virtual environment CPython3.7.7.final.0-64 in 548ms
  creator CPython3Posix(dest=/Users/onesixx/sixxDoc/RCODE/testPrj/pypy, clear=False, global=False)
  seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/Users/onesixx/Library/Application Support/virtualenv/seed-app-data/v1.0.1)
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

➜  ll
total 0
drwxr-xr-x  5 onesixx  staff   160B Apr 17 00:52 pypy
➜  cd pypy
➜  ll
total 8
drwxr-xr-x  21 onesixx  staff   672B Apr 17 00:52 bin
drwxr-xr-x   3 onesixx  staff    96B Apr 17 00:52 lib
-rw-r--r--   1 onesixx  staff   422B Apr 17 00:52 pyvenv.cfg

activate virtualEnv (pypy)

➜  source ./bin/activate

확인
|➜  which python
/Users/onesixx/sixxDoc/RCODE/testPrj/pypy/bin/python

해당 virtualEnv (pypy)에 python패키지 설치

➜  pip3 install numpy pandas matplotlib

Collecting numpy
  Downloading numpy-1.18.2-cp37-cp37m-macosx_10_9_x86_64.whl (15.1 MB)
     |████████████████████████████████| 15.1 MB 10.2 MB/s
Collecting pandas
  Downloading pandas-1.0.3-cp37-cp37m-macosx_10_9_x86_64.whl (10.0 MB)
     |████████████████████████████████| 10.0 MB 9.6 MB/s
Collecting matplotlib
  Downloading matplotlib-3.2.1-cp37-cp37m-macosx_10_9_x86_64.whl (12.4 MB)
     |████████████████████████████████| 12.4 MB 3.4 MB/s
Collecting python-dateutil>=2.6.1
  Downloading python_dateutil-2.8.1-py2.py3-none-any.whl (227 kB)
     |████████████████████████████████| 227 kB 8.9 MB/s
Collecting pytz>=2017.2
  Downloading pytz-2019.3-py2.py3-none-any.whl (509 kB)
     |████████████████████████████████| 509 kB 9.4 MB/s
Collecting kiwisolver>=1.0.1
  Downloading kiwisolver-1.2.0-cp37-cp37m-macosx_10_9_x86_64.whl (60 kB)
     |████████████████████████████████| 60 kB 4.6 MB/s
Collecting cycler>=0.10
  Downloading cycler-0.10.0-py2.py3-none-any.whl (6.5 kB)
Collecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1
  Downloading pyparsing-2.4.7-py2.py3-none-any.whl (67 kB)
     |████████████████████████████████| 67 kB 4.0 MB/s
Collecting six>=1.5
  Downloading six-1.14.0-py2.py3-none-any.whl (10 kB)
Installing collected packages: numpy, six, python-dateutil, pytz, pandas, kiwisolver, cycler, pyparsing, matplotlib
Successfully installed cycler-0.10.0 kiwisolver-1.2.0 matplotlib-3.2.1 numpy-1.18.2 pandas-1.0.3 pyparsing-2.4.7 python-dateutil-2.8.1 pytz-2019.3 six-1.14.0
Categories: Python Basic

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x