[Python] virtualenv 구성 맛보기

ITWeb/개발일반 2018. 5. 3. 12:07

먼저 무작정 하다 보면 에러 부터 경험 할때가 많이 있습니다.

Anaconda 를 설치 해 놓고 잊고 있었는데 에러 부터 나와서 뭐지 했내요.


python virtualenv 구성 시 아레와 같은 에러 발생시 해결 방법



$ virtualenv venv

New python executable in /Users/xxx/Work/apps/venv/bin/python

Installing setuptools, pip, wheel...

  Complete output from command /Users/xxx/Work/apps/venv/bin/python - setuptools pip wheel:

  Traceback (most recent call last):

  File "<stdin>", line 4, in <module>

  File "/Users/xxx/anaconda2/lib/python2.7/tempfile.py", line 32, in <module>

    import io as _io

  File "/Users/xxx/anaconda2/lib/python2.7/io.py", line 51, in <module>

    import _io

ImportError: dlopen(/Users/xxx/Work/apps/venv/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyCodecInfo_GetIncrementalDecoder

  Referenced from: /Users/xxx/Work/apps/venv/lib/python2.7/lib-dynload/_io.so

  Expected in: flat namespace

 in /Users/xxx/Work/apps/venv/lib/python2.7/lib-dynload/_io.so

----------------------------------------

...Installing setuptools, pip, wheel...done.

Traceback (most recent call last):

  File "/Users/xxx/anaconda2/bin/virtualenv", line 11, in <module>

    sys.exit(main())

  File "/Users/xxx/anaconda2/lib/python2.7/site-packages/virtualenv.py", line 712, in main

    symlink=options.symlink)

  File "/Users/xxx/anaconda2/lib/python2.7/site-packages/virtualenv.py", line 953, in create_environment

    download=download,

  File "/Users/xxx/anaconda2/lib/python2.7/site-packages/virtualenv.py", line 904, in install_wheel

    call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)

  File "/Users/xxx/anaconda2/lib/python2.7/site-packages/virtualenv.py", line 796, in call_subprocess

    % (cmd_desc, proc.returncode))

OSError: Command /Users/xxx/Work/apps/venv/bin/python - setuptools pip wheel failed with error code 1


Reference)

https://github.com/certbot/certbot/issues/2850


$ pip uninstall virtualenv

$ conda install virtualenv


# 아래와 같이 하면 실행한 위치에 venv 라는 폴더가 생성이 됩니다.

$ virtualenv venv --distribute

New python executable in /Users/xxx/Work/apps/venv/bin/python

copying /Users/xxx/anaconda2/bin/python => /Users/xxx/Work/apps/venv/bin/python

copying /Users/xxx/anaconda2/bin/../lib/libpython2.7.dylib => /Users/xxx/Work/apps/venv/lib/libpython2.7.dylib

Installing setuptools, pip, wheel...done.


# 가상환경 실행

$ source venv/bin/activate

(venv) MacBook-Pro-2:apps xxx$


# 가상환경 빠져나오기

(venv) MacBook-Pro-2:apps xxx$ deactivate


# python 버전별 가상환경 실행

$ virtualenv venv --python=python2.7

or

$ python -m virtualenv venv


$ virtualenv venv --python=python3.6

or

$ python3 -m virtualenv venv


# 가상환경에서 필요한 package 구성 후 설치 모듈을 구성 합니다. (requirements.txt)

$ virtualenv venv2.7 --python=python2.7

Running virtualenv with interpreter /Users/xxx/anaconda2/bin/python2.7

New python executable in /Users/xxx/Work/apps/venv2.7/bin/python2.7

copying /Users/xxx/anaconda2/bin/python2.7 => /Users/xxx/Work/apps/venv2.7/bin/python2.7

copying /Users/xxx/anaconda2/bin/../lib/libpython2.7.dylib => /Users/xxx/Work/apps/venv2.7/lib/libpython2.7.dylib

Also creating executable in /Users/xxx/Work/apps/venv2.7/bin/python

Installing setuptools, pip, wheel...done.

MacBook-Pro-2:apps xxx$ source venv2.7/bin/activate

(venv2.7) MacBook-Pro-2:apps xxx$

(venv2.7) MacBook-Pro-2:apps xxx$ pip install boto3

Collecting boto3

  Downloading https://files.pythonhosted.org/packages/24/ec/7e3c056775ac725b31a7d5d38e6f7f93ac793f50c35a2cd36b4446be426c/boto3-1.7.12-py2.py3-none-any.whl (128kB)

    100% |████████████████████████████████| 133kB 518kB/s

Collecting s3transfer<0.2.0,>=0.1.10 (from boto3)

  Using cached https://files.pythonhosted.org/packages/d7/14/2a0004d487464d120c9fb85313a75cd3d71a7506955be458eebfe19a6b1d/s3transfer-0.1.13-py2.py3-none-any.whl

Collecting jmespath<1.0.0,>=0.7.1 (from boto3)

  Using cached https://files.pythonhosted.org/packages/b7/31/05c8d001f7f87f0f07289a5fc0fc3832e9a57f2dbd4d3b0fee70e0d51365/jmespath-0.9.3-py2.py3-none-any.whl

Collecting botocore<1.11.0,>=1.10.12 (from boto3)

  Downloading https://files.pythonhosted.org/packages/8e/d7/8b4b54f82a1a137f520d8b04f6c7b87b5f4762818100d1f38a0234fbf17c/botocore-1.10.12-py2.py3-none-any.whl (4.2MB)

    100% |████████████████████████████████| 4.2MB 2.6MB/s

Collecting futures<4.0.0,>=2.2.0; python_version == "2.6" or python_version == "2.7" (from s3transfer<0.2.0,>=0.1.10->boto3)

  Downloading https://files.pythonhosted.org/packages/2d/99/b2c4e9d5a30f6471e410a146232b4118e697fa3ffc06d6a65efde84debd0/futures-3.2.0-py2-none-any.whl

Collecting python-dateutil<3.0.0,>=2.1; python_version >= "2.7" (from botocore<1.11.0,>=1.10.12->boto3)

  Downloading https://files.pythonhosted.org/packages/0c/57/19f3a65bcf6d5be570ee8c35a5398496e10a0ddcbc95393b2d17f86aaaf8/python_dateutil-2.7.2-py2.py3-none-any.whl (212kB)

    100% |████████████████████████████████| 215kB 3.8MB/s

Collecting docutils>=0.10 (from botocore<1.11.0,>=1.10.12->boto3)

  Downloading https://files.pythonhosted.org/packages/50/09/c53398e0005b11f7ffb27b7aa720c617aba53be4fb4f4f3f06b9b5c60f28/docutils-0.14-py2-none-any.whl (543kB)

    100% |████████████████████████████████| 552kB 3.8MB/s

Collecting six>=1.5 (from python-dateutil<3.0.0,>=2.1; python_version >= "2.7"->botocore<1.11.0,>=1.10.12->boto3)

  Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl

Installing collected packages: six, python-dateutil, jmespath, docutils, botocore, futures, s3transfer, boto3

Successfully installed boto3-1.7.12 botocore-1.10.12 docutils-0.14 futures-3.2.0 jmespath-0.9.3 python-dateutil-2.7.2 s3transfer-0.1.13 six-1.11.0

(venv2.7) MacBook-Pro-2:apps xxx$ pip freeze > requirements.txt

(venv2.7) MacBook-Pro-2:apps xxx$ deactivate

MacBook-Pro-2:apps xxx$ cat requirements.txt

boto3==1.7.12

botocore==1.10.12

docutils==0.14

futures==3.2.0

jmespath==0.9.3

python-dateutil==2.7.2

s3transfer==0.1.13

six==1.11.0


만들어진 requirements.txt 파일을 가지고 설치는 아래와 같이 하시면 됩니다.


$ pip install -r requirements.txt 

or

$ pip3 install -r requirements.txt 


: