pyenv-virtualenv is a pyenv plugin that provides features to manage virtualenvs and conda environments for Python on UNIX-like systems.
If you don't have pyenv in your system, follow below post;
Installing pyenv-virtualenv
$ brew update
$ brew install pyenv-virtualenv
Setting PATH
Run one of the following commands in terms of your shell's .rc
file.
$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.zshrc
Creating virtual environment
$ mkdir python_demo
$ cd python_demo
$ pyenv install 3.11.4
$ pyenv virtualenv 3.11.4 python_demo-3.11.4
$ pyenv virtualenvs
$ pyenv prefix python_demo-3.11.4
/Users/kenanhancer/.pyenv/versions/python_demo-3.11.4
$ ls -Llat $(pyenv prefix python_demo-3.11.4)
drwxr-xr-x 14 kenanhancer staff 448 19 Jun 17:36 bin
-rw-r--r-- 1 kenanhancer staff 107 19 Jun 17:36 pyvenv.cfg
drwxr-xr-x 3 kenanhancer staff 96 19 Jun 17:36 lib
drwxr-xr-x 2 kenanhancer staff 64 19 Jun 17:36 include
$ tree -a -L 4 $(pyenv prefix python_demo-3.11.4)
├── bin
│ ├── Activate.ps1
│ ├── activate
│ ├── activate.csh
│ ├── activate.fish
│ ├── pip
│ ├── pip3
│ ├── pip3.11
│ ├── pydoc
│ ├── python -> python3.11
│ ├── python3 -> python3.11
│ └── python3.11 -> /Users/kenanhancer/.pyenv/versions/3.11.4/bin/python3.11
├── include
│ └── python3.11
├── lib
│ └── python3.11
│ └── site-packages
│ ├── _distutils_hack
│ ├── distutils-precedence.pth
│ ├── pip
│ ├── pip-23.1.2.dist-info
│ ├── pkg_resources
│ ├── setuptools
│ └── setuptools-65.5.0.dist-info
└── pyvenv.cfg
Creating virtual environment from current version
$ mkdir python_demo
$ cd python_demo
$ pyenv local 3.9.17
$ pyenv virtualenv python_demo-3.9.17
$ pyenv prefix python_demo-3.9.17
/Users/kenanhancer/.pyenv/versions/python_demo-3.9.17
├── bin
│ ├── Activate.ps1
│ ├── activate
│ ├── activate.csh
│ ├── activate.fish
│ ├── pip
│ ├── pip3
│ ├── pip3.9
│ ├── pydoc
│ ├── python -> python3.9
│ ├── python3 -> python3.9
│ └── python3.9 -> /Users/kenanhancer/.pyenv/versions/3.9.17/bin/python3.9
├── include
├── lib
│ └── python3.9
│ └── site-packages
│ ├── _distutils_hack
│ ├── distutils-precedence.pth
│ ├── pip
│ ├── pip-23.0.1.dist-info
│ ├── pkg_resources
│ ├── setuptools
│ └── setuptools-58.1.0.dist-info
└── pyvenv.cfg
Activate and deactivate virtual environment
$ pyenv activate python_demo-3.9.17
$ pyenv which python
/Users/kenanhancer/.pyenv/versions/test31-3.9.17/bin/python
$ pyenv prefix
/Users/kenanhancer/.pyenv/versions/test31-3.9.17
$ pyenv which pip
/Users/kenanhancer/.pyenv/versions/test31-3.9.17/bin/pip
$ pyenv deactivate
Automatically Activate and deactivate virtual environment
setting local environment to created virtual environment, makes our job easier. When we go in to python_demo directory, pyenv will activate virtual environment automatically, and going out of this directory will deactivate virtual environment.
$ cd python_demo
$ pyenv local python_demo-3.9.17
(test31-3.9.17) kenanhancer@kenans-MacBook-Pro test31 %
Deleting virtual environment
$ pyenv uninstall python_demo-3.9.17