Python Virtual Environments with “virtualenv”

Python virtual environment allows python programmers to safely work with different python packages & different versions to test the application. Plus it would not impact the other python environments on the same system.

The following command will install the “virtualenv” in /usr/local/bin folder:

ssh -H python3 -m pip install virtualenv

The virtualenv is now available to all the applications/users on the system.

The following command installs the virtualenv in user’s local folder (~/.local/bin/):

python3 -m pip install virtualenv

Check the version of virtualenv:

python3 -m virtualenv --version

Create a virtual environment with the name “ve100” using the following command:

virtualenv ve100

The above command will create a folder with the name “ve100” at the current location.

Activate the following the virtual python environment using the following command:

source ve100/bin/activate

To deactivate, run the deactivate command:

deactivate

To create a virtual environment with a desired version of python:

virtualenv -p /usr/bin/python2.7 ve200

Python Package Management with pip3

“pip” is a default package manager for python packages/modules.

You can install pip using the following command:

sudo apt install python3-pip

The pip3 binary in installed in /usr/bin folder.

Check the pip version:

pip3 --version

or

python3 -m pip --version

You can upgrade “pip” using the following command:

sudo -H python3 -m pip install --upgrade pip

The table below lists some commonly used of pip command:

CommandDescription
python3 -m pip –helpDisplay pip help
python3 -m pip listLists the installed packages
python3 -m pip list -vLists the installed packages along with the install path
python3 -m pip search oauthSearches for packages with “oauth” string
python3 -m pip install pandasInstalls the latest version of the package “pandas”
python3 -m pip show pandasShows the details of the installed package pandas
python3 -m pip uninstall pandasUninstalls the package pandas
pip freezeDisplays the lists of packages installed along with it’s version in the format:
[package_name_1]==[version]
[package_name_2]==[version]

Tutorial: Python Virtual Environment Wrapper

Installation:

Step 1: Make sure the “virtualenv” is already installed

virtualenv --version

Step 2: Install “virtualenvwrapper

sudo -H python3 -m pip install virtualenvwrapper

Step 3: Add some environment variables

Add the following at your .bash_profile file (or to your shell’s profile file)

export WORKON_HOME="$HOME/my_python_venvs"
export VIRTUALENVWRAPPER_VIRTUALENV="/usr/local/bin/virtualenv"
export VIRTUALENVWRAPPER_PYTHON="/usr/bin/python3"

export PROJECT_HOME="$HOME/my_python_projects"

Step 4: Source the wrapper script

 source /usr/local/bin/virtualenvwrapper.sh

Step 5: Create directories

mkdir $HOME/my_python_venvs
mkdir $HOME/my_python_projects

You are ready to use the virtualenvwrapper!!

Using virtualenvwrapper

Switch between multiple versions of python3

You can have multiple versions of python 3 installed on your Ubuntu machine, and switch between the versions. This presents nice flexibility to the users. You can potentially have the latest version installed, and switch to the prior version easily!

The following steps will enable you to switch between 3.6, 3.7 and 3.8 version of python on a ubuntu machine.

Step 1: Check existing version

$ python3 --version
Python 3.6.9

Step 2: Run apt update

$ sudo apt update -y

Step 3: Install python 3.7

$ sudo apt install python3.7

Step 4: Install python 3.8

$ sudo apt install python3.8

Step 5: Use “update-alternatives” enable the switch flexibility

$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 3

Step 6: Now, you can switch between the python version by executing the following command, and picking the one you desire.

$ sudo update-alternatives --config python3
There are 3 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.6   2         auto mode
  1            /usr/bin/python3.6   2         manual mode
  2            /usr/bin/python3.7   2         manual mode
  3            /usr/bin/python3.8   2         manual mode

Press <enter> to keep the current choice[*], or type selection number:

Step 7: Test the version by running the following command

$ python3 --version
Python 3.7.5    (Note: This depends on the version installed, and picked)

vi Editor Cheat sheet

Page Contents


Opening File
Saving & Quitting
Switching to Shell & Run Commands
Modes – Command, Insert/Append & Exit
Deleting Text
Moving Cursor in file
Moving on Screen
Moving on Screen using Ctrl key
Go to desired line by line number
Changing Text
Copying, Pasting and Moving lines
Show/Hide Line Numbers
Searching – Ignore/Set case
Search and Replace

Opening File


CommandPurpose
vi Open new file to be named later
vi file_name Open an existing file or create a new file
vi -r file_name Recover crashed file
view filenameOpen file in read-only mode

Savings & Quitting


CommandPurpose
:qQuit
:q!Quit without saving the changes
:wSave
:wq or :wq!Save and Quit
:w filen_nameSave to file “file_name”
:e! Discard unsaved changes, and reopen the file.
:shSwitch to shell temporarily, and return back using Ctrl+d

Switching to Shell & Run Commands


CommandPurpose
:shSwitch to shell temporarily, and return back using Ctrl+d
:!cmd1 Execute the command cmd1

Modes – Command, Insert/Append & Exit


CommandPurpose
iInsert characters left of cursor
IInsert characters beginning of line
aInsert characters right of cursor
AInsert characters end of line
oInsert line below cursor
OInsert line above cursor
EscInsert Mode to Command Mode
Exit Mode to Command Mode

Deleting Text


CommandPurpose
xDelete character at the cursor
XDelete character to the left of the cursor
dwDelete word or part of word, to right of cursor
ddDelete current line
DDelete part of line to right of cursor
dG Delete to end of file
d1G Delete – beginning of file to cursor
:1,5d Delete lines 1 to 5

CommandPurpose
hMove one character left
jMove one line down
kMove one line up
lMove one character to right
wMove one word right
WMove one word (past punctuation) right
bMove one word left
BMove one word (past punctuation) left
e Move to the end of the current word

Navigation – Moving on Screen


CommandPurpose
H Move to top of screen (1st line)
M Move to middle of screen
L Move to bottom of screen (last line on screen)

Navigation – Moving on Screen using Ctrl key


CommandPurpose
Ctrl+f Page forward one screen forward
Ctrl+d Scroll forward one half screen
Ctrl+b Page backward one screen
Ctrl+u Scroll backward one-half screen

Navigation – Go to desired line by line number


CommandPurpose
GGo to the last line
1GGo to the 1st line
24GGo to the 24th line

Changing Text


CommandPurpose
ccChange line
cwChange characters in a word or entire word right of cursor
CChange from cursor to end of line
sSubstitute string for character(s) from cursor forward
rReplace character with other character at cursor
RReplace characters from the cursor position
r ReturnBreak line at cursor
JJoin to current line line below
xpTranspose character cursor and character to right
~Change case of letter to uppercase or lowercase at cursor
uUndo previous command
UUndo all changes to current line

Copying, Pasting and Moving lines


CommandPurpose
yyYank or copy current line
5yyCopy 5 lines
YYank or copy line
pPut yanked or deleted line below current line
PPut yanked or deleted line above current line
:1,5 co 9 Copy lines 1-5 and put after line 9
:2,5 m 9 Move lines 4-5 and put after line 9

Show/Hide Line Numbers


CommandPurpose
:set nuShow line numbers for all lines
:set nonuHide numbers for all line

Searching – Ignore/Set case


CommandPurpose
:set icIgnore case while searching
:set noicCase sensitive searching

Search and Replace


CommandPurpose
/stringSearch for string anywhere in file
?stringstring Search backward for string
n Find next occurrence of string in search direction
N Find previous occurrence of string in search direction
:s/old/new/ Replace the first occurrence of the word “old” with “new” on the current line
:s/old/new/gReplace all occurrences of the word “old” with “new” on the current line.
:.,+50s/old/new/ Replace all occurrence of the word “old” with “new” starting at the current line for the next 50 lines.
:1,50s/old/new/ Replace all occurrence of the word “old” with “new” on lines 1 thru 50.
:%s/old/new/ Replace all occurrences of the word “old” with “new” in the entire file.:e!

Zen of Python

The Zen of Python (’20 Points’) enlightens you with knowledge to write simple and elegant code:

Enter “import this” @ our python REPL to get the list:

  1. Beautiful is better than ugly.
  2. Explicit is better than implicit.
  3. Simple is better than complex.
  4. Complex is better than complicated.
  5. Flat is better than nested.
  6. Sparse is better than dense.
  7. Readability counts.
  8. Special cases aren’t special enough to break the rules.
  9. Although practicality beats purity.
  10. Errors should never pass silently.
  11. Unless explicitly silenced.
  12. In the face of ambiguity, refuse the temptation to guess.
  13. There should be one– and preferably only one –obvious way to do it.
  14. Although that way may not be obvious at first unless you’re Dutch.
  15. Now is better than never.
  16. Although never is often better than *right* now.
  17. If the implementation is hard to explain, it’s a bad idea.
  18. If the implementation is easy to explain, it may be a good idea.
  19. Namespaces are one honking great idea — let’s do more of those!

How to install Oracle VirtualBox on Ubuntu 16.04?

Follow these simple steps to install Oracle VirtualBox on Ubuntu 16.04 & 18.04:

Step 1: Update Packages

$ sudo apt-get update
$ sudo apt-get upgrade

Step 2: Configure apt Repo

$ wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add -
$ wget -q https://www.virtualbox.org/download/oracle_vbox.asc -O- | sudo apt-key add -

$ sudo add-apt-repository "deb http://download.virtualbox.org/virtualbox/debian `lsb_release -cs` contrib"

Step 3: Installation

$ sudo apt-get update
$ sudo apt-get install virtualbox-5.2

Step 4: Launch Virtual Box

$ virtualbox &

Great Job! You now have Oracle VirtualBox installed and ready to explore.


How to change the hostname on Ubuntu 16.04?

Follow these simple steps to change the hostname on a ubuntu machine:

Step 1: Update /etc/hostname file

This file contains the current hostname of the machine. Update the name to your

desired name.

$ sudo vi /etc/hostname

Step 2: Update /etc/hosts file

Replace the current hostname in the file with the desired name.

$ sudo vi /etc/hosts

Step 3: Set the hostname using the hostnamectl command

$ sudo hostnamectl set-hostname new_hostname