How to declutter your Python 3 virtual environments, from possible version conflicts!
By: Bonface Mudiri
Tags: Virtual Environment, Coding, Programming, Ubuntu, Python

In this short article, we shall focus on how to install virtualenv and the virtualenvwrapper and tweak bashrc file so that we can easily activate and deactivate the environment without being overly verbose on the command line. <Pay attention, if you love being ‘lazy’ on the command line />

Ideally, a virtual environment makes it possible for you to work on a project, focus on the solution, than worrying too much about the possible conflict on dependencies.

NB: You may refer to the below sites for reference and learn about the virtual environment but I have tried to pick the simplest approach to this problem:

*STEP PROCESS*

First, let us check the Python version. The beauty about Ubuntu is, Python comes pre-installed.

$ python3 —-version

Now is time to do the updates and upgrade the packages

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

We need a package management tool to install our software and we shall be using pip 3. Install using below command

$ sudo apt-get install -y python3-pip

Please note when installing any package you will be using: $ pip3 install the_package_name

We need to take care of the debian package compilations and have a way to compile python extension modules.

$ sudo apt-get install build-essential libssl-dev libffi-dev python3-dev

Phewks! We are now finally ready to push in our virtual environment. I presume you have the permission to do so, otherwise, use sudo with -H flag if you get stuck with access

$ pip3 install virtualenv

Now confirm you have virtualenv installed

$ virtualenv —-version

Power unto us; lets us now install the virtualenvwrapper

$ sudo pip3 install virtualenvwrapper

~This is where you can do some pushups because it is about to get awesome!

$ mkdir ~/.virtualenvs $ mkdir Projects

This directory is the /path/where/you/want/your/Projects and you can change if you like. I like naming Projects, for readability and ease of access

Now Open ~/.bashrc file. It is intentionally hidden in the Home directory and can not be easily found, but you can use the below magic command to check that it exists.

$ ls -la ~/ | more

Now the easy way to edit that .bashrc file is to either use gedit or vim. Some people may find it difficult to use vim, but pick what works for you. If you have weirdly setup your Ubuntu, you may get issues trying to access and editing this file.

CAUTION: Please create a backup of your .bashrc file or forever hold your peace!! cp ~/.bashrc ~/.bashrc.bak You can always revert back to original bashrc cp -i ~/.bashrc.bak ~/.bashrc

Back to our step process, let us now gedit the bashrc file

$ gedit ~./bashrc

Now paste the below 5 lines of code inside the bash file, preferable on top of the bashrc so that you don’t mess up! Remember am using $HOME/Projects and presume you have set up in that path.

VIRTUALENVWRAPPER_PYTHON=’/usr/bin/python3' export WORKON_HOME=~/.virtualenvs export PROJECT_HOME=$HOME/Projects export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh source /usr/local/bin/virtualenvwrapper_lazy.sh

Now save the bash file and please don’t mess up this file. Let your creativity in tweaking stuff be saved for development, not within this file!!

Now still on the command line, continue as below. We need to source the bashrc

$ source ~./bashrc

Congratulations you are now done!

The neat way to create a virtual environment can now be done as below

$ mkproject my_project $ workon my_project

You will see something like (my_project) proving you have activated the virtual environment. You must see those parentheses.

To deactivate and get out of env

$ deactivate

Always ensure you activate the environment when installing things like Django... otherwise, versions may mess you up! GOOD LUCK!

Feel free to create a pull request to make this post 100% accurate

I hope this was very helpful!

*If you felt inspired by this article, please applaud with a clap 👏 to make this article reach others.