pyenv virtualenv python 설정

Python : 2020. 12. 10. 18:02
반응형

python 사용자라면 맥이던 윈도우던 리눅스던 pyenv-virtualenv 를 꼭 써야한다.

필수다 필수!!

만약에 리눅스를 다시깔거나 시스템 옮길때는 ~/.pyenv 만 복사해가면 된다.

github.com/pyenv/pyenv-installer

 

pyenv/pyenv-installer

This tool is used to install `pyenv` and friends. Contribute to pyenv/pyenv-installer development by creating an account on GitHub.

github.com

여기를 참고한다.

$ curl https://pyenv.run | bash

이렇게 해주면 알아서 설치 됨.

설치된거 확인하기

$ exec $SHELL

한다음 

$ pyenv update

 

일단 이거 깔아준다.

$ sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev

 

그다음 pyenv-virtualenv 를 깐다.

github.com/pyenv/pyenv-virtualenv

 

pyenv/pyenv-virtualenv

a pyenv plugin to manage virtualenv (a.k.a. python-virtualenv) - pyenv/pyenv-virtualenv

github.com

 

 

사용법은

 

Usage

Using pyenv virtualenv with pyenv

To create a virtualenv for the Python version used with pyenv, run pyenv virtualenv, specifying the Python version you want and the name of the virtualenv directory. For example,

$ pyenv virtualenv 2.7.10 my-virtual-env-2.7.10

will create a virtualenv based on Python 2.7.10 under $(pyenv root)/versions in a folder called my-virtual-env-2.7.10.

Create virtualenv from current version

If there is only one argument given to pyenv virtualenv, the virtualenv will be created with the given name based on the current pyenv Python version.

$ pyenv version 3.4.3 (set by /home/yyuu/.pyenv/version) $ pyenv virtualenv venv34

List existing virtualenvs

pyenv virtualenvs shows you the list of existing virtualenvs and conda environments.

$ pyenv shell venv34 $ pyenv virtualenvs miniconda3-3.9.1 (created from /home/yyuu/.pyenv/versions/miniconda3-3.9.1) miniconda3-3.9.1/envs/myenv (created from /home/yyuu/.pyenv/versions/miniconda3-3.9.1) 2.7.10/envs/my-virtual-env-2.7.10 (created from /home/yyuu/.pyenv/versions/2.7.10) 3.4.3/envs/venv34 (created from /home/yyuu/.pyenv/versions/3.4.3) my-virtual-env-2.7.10 (created from /home/yyuu/.pyenv/versions/2.7.10) * venv34 (created from /home/yyuu/.pyenv/versions/3.4.3)

There are two entries for each virtualenv, and the shorter one is just a symlink.

Activate virtualenv

Some external tools (e.g. jedi) might require you to activate the virtualenv and conda environments.

If eval "$(pyenv virtualenv-init -)" is configured in your shell, pyenv-virtualenv will automatically activate/deactivate virtualenvs on entering/leaving directories which contain a .python-version file that contains the name of a valid virtual environment as shown in the output of pyenv virtualenvs (e.g., venv34 or 3.4.3/envs/venv34 in example above) . .python-version files are used by pyenv to denote local Python versions and can be created and deleted with the pyenv local command.

You can also activate and deactivate a pyenv virtualenv manually:

pyenv activate <name> pyenv deactivate

Delete existing virtualenv

Removing the directories in $(pyenv root)/versions and $(pyenv root)/versions/{version}/envs will delete the virtualenv, or you can run:

pyenv uninstall my-virtual-env

You can also delete existing virtualenvs by using virtualenv-delete command, e.g. you can run:

pyenv virtualenv-delete my-virtual-env

This will delete virtualenv called my-virtual-env.

virtualenv and venv

There is a venv module available for CPython 3.3 and newer. It provides an executable module venv which is the successor of virtualenv and distributed by default.

pyenv-virtualenv uses python -m venv if it is available and the virtualenv command is not available.

Anaconda and Miniconda

You can manage conda environments by conda create as same manner as standard Anaconda/Miniconda installations. To use those environments, you can use pyenv activate and pyenv deactivate.

$ pyenv version miniconda3-3.9.1 (set by /home/yyuu/.pyenv/version) $ conda env list # conda environments: # myenv /home/yyuu/.pyenv/versions/miniconda3-3.9.1/envs/myenv root * /home/yyuu/.pyenv/versions/miniconda3-3.9.1 $ pyenv activate miniconda3-3.9.1/envs/myenv discarding /home/yyuu/.pyenv/versions/miniconda3-3.9.1/bin from PATH prepending /home/yyuu/.pyenv/versions/miniconda3-3.9.1/envs/myenv/bin to PATH $ python --version Python 3.4.3 :: Continuum Analytics, Inc. $ pyenv deactivate discarding /home/yyuu/.pyenv/versions/miniconda3-3.9.1/envs/myenv/bin from PATH

If conda is available, pyenv virtualenv will use it to create environment by conda create.

$ pyenv version miniconda3-3.9.1 (set by /home/yyuu/.pyenv/version) $ pyenv virtualenv myenv2 $ conda env list # conda environments: # myenv /home/yyuu/.pyenv/versions/miniconda3-3.9.1/envs/myenv myenv /home/yyuu/.pyenv/versions/miniconda3-3.9.1/envs/myenv2 root * /home/yyuu/.pyenv/versions/miniconda3-3.9.1

You can use version like miniconda3-3.9.1/envs/myenv to specify conda environment as a version in pyenv.

$ pyenv version miniconda3-3.9.1 (set by /home/yyuu/.pyenv/version) $ pyenv shell miniconda3-3.9.1/envs/myenv $ which python /home/yyuu/.pyenv/versions/miniconda3-3.9.1/envs/myenv/bin/python

Special environment variables

You can set certain environment variables to control pyenv-virtualenv.

  • PYENV_VIRTUALENV_CACHE_PATH, if set, specifies a directory to use for caching downloaded package files.
  • VIRTUALENV_VERSION, if set, forces pyenv-virtualenv to install the desired version of virtualenv. If virtualenv has not been installed, pyenv-virtualenv will try to install the given version of virtualenv.
  • GET_PIP, if set and venv is preferred over virtualenv, use get_pip.py from the specified location.
  • GET_PIP_URL, if set and venv is preferred over virtualenv, download get_pip.py from the specified URL.
  • PIP_VERSION, if set and venv is preferred over virtualenv, install the specified version of pip.
  • PYENV_VIRTUALENV_VERBOSE_ACTIVATE, if set, shows some verbose outputs on activation and deactivation
반응형
Posted by Real_G

댓글을 달아 주세요