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
 

Python :F-Strings

In Python, to format output statements you can do in several ways as below.
    • 1. Regular print() using parameter data types
      2. Using .format()
      3. Using F-string

  • I will write small examples using all 3 types.

    1. Regular print() using parameter data types

    In the below example I will define values and print same for 3 most used data types which are int, string, float

    
    >>> a = 10
    >>> b = "Hello"
    >>> c = 98.9
    >>> print("a value is %d b value is %s c value is %f" %(a, b, c))
    a value is 10 b value is Hello c value is 98.900000
    

    If you observe the float this is not what I have defined actually, but we are not without options either, we can restrict how many decimals we want. Check below code

    
    >>> print("a value is %d b value is %s c value is %.2f" %(a, b, c))
    a value is 10 b value is Hello c value is 98.90
    >>> print("a value is %d b value is %s c value is %.3f" %(a, b, c))
    a value is 10 b value is Hello c value is 98.900
    >>>
    

    Next let's do same with .format()

    2. Using .format()

    Using format we can also evaluate expressions while printing, below code snippets cover both normal printing and expressions which are evaluated

    
    >>> a = 10
    >>> b = "Hello"
    >>> c = 98.9
    >>> print("a value is {0}, b value is {1}, c value is {2}".format(a,b,c))
    a value is 10, b value is Hello, c value is 98.9
    >>> print("a value is {a}, b value is {b}, c value is {c}".format(a=a,b=b,c=c))
    a value is 10, b value is Hello, c value is 98.9
    >>> print("a value is {a}, b value is {b}, c value is {c}".format(a=a,b=b,c=c if c > 100 else 0))
    a value is 10, b value is Hello, c value is 0
    
    

    I am not adding any explanation here because the code itself self explanatory.

    Now let's see F-Strings

    >>> f'{a} {b} {c}'
    '10 Hello 98.9'
    >>> f'{a} {b} {c if c > 100 else 0 } '
    '10 Hello 0 '
    >>> print(f'{a} {b} {c if c > 100 else 0 } ')
    10 Hello 0
    
    Hope it helps.

    Thank you.

    How to fix checkPermissions Missing write access to /usr/local/lib/node_modules error ?

    If you are working in a Node JS environment and without root access or sudo access, you might get an error as checkPermissions Missing write access to /usr/local/lib/node_modules like below
    
    npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
    npm ERR! code EACCES
    npm ERR! syscall access
    npm ERR! path /usr/local/lib/node_modules
    npm ERR! errno -13
    npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
    npm ERR!  [Error: EACCES: permission denied, access '/usr/local/lib/node_modules'] {
    npm ERR!   errno: -13,
    npm ERR!   code: 'EACCES',
    npm ERR!   syscall: 'access',
    npm ERR!   path: '/usr/local/lib/node_modules'
    npm ERR! }
    npm ERR!
    

    one solution to this problem is changing the permission of the folder /usr/local/lib/node_modules for the current user, but thats not recommended and what if its not in your hands to change the permission.
    One thing we can do to address this problem is modify the location where nodejs can store modules, for your user account.
    Follow below steps in given order to configure your node_modules folder for your user account with out harming others!!
    
    # Create a directory named npm-global as hidden in your home directory
    mkdir ~/.npm-global
    
    # Then set it as your node dir 
    npm config set prefix '~/.npm-global'
    
    # Update Path in your .bashrc or .bash_profile or .profile files as below
    export PATH="~/.npm-global/bin:$PATH"
    
    # once saved load the changes
    source ~/.bashrc
    

    And thats it, we are good. Now you can install any module and you wont be getting any permission related error as above.
    Hope it helps.

    How to install nodejs in CentOS ?

    If you choose default yum install of nodejs you will get 6.x release which is very old.
    To install latest version of nodejs in centos you can execute below command
    Command 1
      
      	curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -
       
    

    You will get output as below
    
    [vagrant@localhost ps-sinonjs]$ curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -
    
    ## Installing the NodeSource Node.js 14.x repo...
    
    
    ## Inspecting system...
    
    + rpm -q --whatprovides redhat-release || rpm -q --whatprovides centos-release || rpm -q --whatprovides cloudlinux-release || rpm -q --whatprovides sl-release
    + uname -m
    
    ## Confirming "el7-x86_64" is supported...
    
    + curl -sLf -o /dev/null 'https://rpm.nodesource.com/pub_14.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm'
    
    ## Downloading release setup RPM...
    
    + mktemp
    + curl -sL -o '/tmp/tmp.KNA1QsAK2D' 'https://rpm.nodesource.com/pub_14.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm'
    
    ## Installing release setup RPM...
    
    + rpm -i --nosignature --force '/tmp/tmp.KNA1QsAK2D'
    
    ## Cleaning up...
    
    + rm -f '/tmp/tmp.KNA1QsAK2D'
    
    ## Checking for existing installations...
    
    + rpm -qa 'node|npm' | grep -v nodesource
    
    ## Run `sudo yum install -y nodejs` to install Node.js 14.x and npm.
    ## You may also need development tools to build native addons:
         sudo yum install gcc-c++ make
    ## To install the Yarn package manager, run:
         curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
         sudo yum install yarn
    
    

    Your main instructions are here
    
    ## Run `sudo yum install -y nodejs` to install Node.js 14.x and npm.
    ## You may also need development tools to build native addons:
         sudo yum install gcc-c++ make
    ## To install the Yarn package manager, run:
         curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
         sudo yum install yarn
    

    I mean simply after executing command1 you can execute below commands and it will install latest Node.js version for you.
    
     sudo yum install -y nodejs 
     sudo yum install gcc-c++ make
     curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
     sudo yum install yarn
    
    Hope it helps.
    Thank you for reading the article.

    How to launch Sublime text editor from terminal in Macbook ?

    As you all know, Sublime is one of the famous and lite weight text editor and can be used for application development for almost every programming language available today.
    For VS Code to launch editor from terminal, you can use code from terminal.
    But for Sublime there is no direct method. To achieve this in sublime we can achieve it in 2 methods I believe. Lets see one by one

    Using Soft Link

    After creating soft link just type sublime in your terminal and sublime open.
      
    ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/sublime
    

    Using alias

    Add alias like below in your .bashrc or ~/.bash_profile files
    
    subl='/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl'
    
    
    then do load the changes with source
    
    source ~/.bashrc
    

    Hope this helps.
    Thanks
    Raja