Python: How to use Pyenv ?

Hello, Today I am going to write a small article on how to use pyenv to install your required version of python and set it as globally across sytem or just local to project directory.

Installation

If you are using Mac, you can simply use brew

brew install pyenv
    
    

For Other operating system you can follow instruction at given Github Repository. I have given them below for CentOS, but they are similar for Debian as well, just adjust your package manager.


  yum install curl git -y
  curl https://pyenv.run | bash
  
  
Paste below lines into your shell profile ~/.bashrc or ~/.zshrc

  	export PYENV_ROOT="$HOME/.pyenv"
	export PATH="$PYENV_ROOT/bin:$PATH"
	eval "$(pyenv init --path)"
    
And restart your shell, you can follow any method available

    exec $SHELL
    # or 
    source ~/.bashrc
    

So pyenv installed now in your system.
Now Lets see how to use it. To list available commands we can use, its huge list so pipe to less.


➜  ~ pyenv install --list | less                                                                                                                                                                    
Available versions:
  2.1.3
  2.2.3
  2.3.7
  2.4.0

To install a version of python you can


 pyenv install 3.5.9

For Example


 ➜  snowpy pyenv install 3.6.13                                                                                                                                                                     
pyenv: ~/.pyenv/versions/3.6.13 already exists
continue with installation? (y/N) y
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.6.13.tar.xz...
-> https://www.python.org/ftp/python/3.6.13/Python-3.6.13.tar.xz
Installing Python-3.6.13...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.6.13 to ~/.pyenv/versions/3.6.13

To set a version of python across your system, like global version of python


 pyenv global 3.8.0 # now your system default version of python is 3.8.0
 python -V
 

for example if your project runs with python 3.5 then you can change to your project directory set


 pyenv local 3.5
 python -V
 

one more interesting point is, pyenv create a hidden file inside your project directory


 cat .python-version # this will list your python version.
 

now use venv or pipenv to setup your project in specific version of python


 python -m venv my_venv
 source ./my_env/bin/activate
 

0 comments:

Post a Comment