Skip to content

Prepare Virtual Environment

  • by

What is it and Why?

A virtual environment works in sense of an independent workstation, in terms of virtual machine which run completely different OS, virtual environment is a place where we can install our desired packages, based on the project specific, apart from the main directory, isolating itself from combining or confusing with the whole OS system directory.

The benefit of creating so is to provide an independent and standalone environment to the user. It does work best when ones has several projects, respectively requiring different packages and dependencies. With different virtual environments, packages wouldn’t be mixed with others’, regardless types of packages, and the most of the time, the specified version.


Below showcases were demonstrated under following systems: Ubuntu Linux 18.06 LTS, Python 3.6.9.

Figure 1: Checking Python and its version.

First and foremost, we double check if there is any Python packages been installed in the system. We could notice that by default Python version 3 was readied.

Figure 2: Checking pip

pip, frankly speaking is the powerful installer packages under Python, mainly in-charged in installing majority of the packages most of the time. We were going to use it a lot in our journeys.

Figure 3: Typical output upon installing, in this case, pip

After having pip in our system, we use so to install virtualenvwrapper, a tool/ library to help us create virtual environment. As per Figure 3 shown, the way to download pip is very simple with a typical “sudo apt install” commands, followed by appropriate version of Python’s format.

Figure 4: Outputs upon installing VirtualWrapper.

With the aid of pip, we then install virtualenvwrapper in order to have the tool in creating virtual environments. Worth to mention, once again, the steps are so simple and straight forwarded as shown above!

Figure 5: Outputs upon installing VIM.

When the virtualenvwrapper is installed, we need to update the PATH.

In short, PATH is an environment variable in Unix-based OS, particularly specifying pathways and directories for where those executable files and programs located at. There are tonnes of methods to update the PATH, and here we are demonstrating the updates using VIM. High efficient and ease of use is the reason we have it.

Then we execute the .bashrc file by following code:

vim .bashrc

The first landing page is shown as per Figure 6.

Figure 6: Landing page about VIM

Then we head to the most bottom of the vim bashrc to edit and update to correct PATH for our virtualenvwrapper installation.

Figure 7: The most bottom part of the environment editor

The code required is as shown below, paste them according to own scenarios and directory names.

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_VIRTUALENV=/home/goran/.local/bin/virtualenv
source ~/.local/bin/virtualenvwrapper.sh

After done, press esc key and type :wq then press enter.

Figure 8: Edit to correct PATH.

We realize there’s an error when we save the bashrc, that stating the PATH is not set properly and correctly, specifically for line VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.

Then we check for the version of python by typing which python.

Figure 9: Wrongly updated, as the correct way is “python” instead of “python3”.

Now we know, the 3 shouldn’t be there as shown as the naming of the system directory is “python”. We update it to the right one.

Figure 10: Create a new environment named “virtualenv”

After successfully update the PATH, we then create a new virtual environment by following codes, as shown as Figure 10, in this case, the naming is virtualenv.

mkvirtualenv (name)

Figure 11: Quit the virtual environment.

deactivate

The code to exit the virtual environment.

Figure 12: Delete any existing virtual environment.

rmvirtualenv (name)

The code to delete the virtual environment.


Above are the simple yet straight forwarded methods to prepare virtual environment, for ease of further projects or developments. Remember, do not afraid of error, instead solve them, and we are good to carry on! Please feel free to leave your thoughts below!

Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *