Sunday, July 21, 2019

AWS EC2 - Install and Configure virtualenvwrapper with Python3

virtualenvwrapper is a very nice wrapper of Python virtualenv tool. It includes wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies. I really like that how easily it allows you to switch between different projects and virtual environments, saves you a lot of time. This blog shows you how to install and configure it on a AWS EC2 instance, using Python3

Install Python3

$ sudo yum install python3
$ pip3 -V
$ pip3 -V
pip 9.0.3 from /usr/lib/python3.7/site-packages (python 3.7)

Install virtualenvwrapper globally

$ sudo pip3 install virtualenvwrapper
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting virtualenvwrapper
  ...
Installing collected packages: virtualenv, virtualenv-clone, pbr, six, stevedore, virtualenvwrapper
  Running setup.py install for virtualenvwrapper ... done
Successfully installed pbr-5.4.1 six-1.12.0 stevedore-1.30.1 virtualenv-16.6.2 virtualenv-clone-0.5.3 virtualenvwrapper-4.8.4

Create project folder and virtual environment folder

$ ll /home/ec2-user/
total 8
drwxrwxr-x 2 ec2-user ec2-user   6 Jul 21 20:27 code
drwxrwxr-x 2 ec2-user ec2-user   6 Jul 21 20:27 venvs

Locates "virtualenvwrapper.sh"

$ which virtualenvwrapper.sh
/usr/local/bin/virtualenvwrapper.sh

Update bash profile

$ vim ~/.bash_profile
Append the following:
# Configure virtualenvwrapper
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
export WORKON_HOME="/home/ec2-user/venvs"
export PROJECT_HOME="/home/ec2-user/code"

Resource bash profile

$ source ~/.bash_profile
virtualenvwrapper.user_scripts creating /home/ec2-user/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/ec2-user/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /home/ec2-user/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /home/ec2-user/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/ec2-user/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/ec2-user/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/ec2-user/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/ec2-user/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /home/ec2-user/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /home/ec2-user/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/ec2-user/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/ec2-user/.virtualenvs/get_env_details

Create a new project

$ mkproject new_proj
Using base prefix '/usr'
  No LICENSE.txt / LICENSE found in source
New python executable in /home/ec2-user/venvs/new_proj/bin/python3
Also creating executable in /home/ec2-user/venvs/new_proj/bin/python
Installing setuptools, pip, wheel...
done.

virtualenvwrapper.user_scripts creating /home/ec2-user/venvs/new_proj/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/ec2-user/venvs/new_proj/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/ec2-user/venvs/new_proj/bin/preactivate
virtualenvwrapper.user_scripts creating /home/ec2-user/venvs/new_proj/bin/postactivate
virtualenvwrapper.user_scripts creating /home/ec2-user/venvs/new_proj/bin/get_env_details
Creating /home/ec2-user/code/new_proj
Setting project for new_proj to /home/ec2-user/code/new_proj
(new_proj) [ec2-user@ip-172-31-36-216 new_proj]$ ls ~/venvs/
new_proj
(new_proj) [ec2-user@ip-172-31-36-216 new_proj]$ ls ~/code/
new_proj

You can see that a "new_proj" folder inside "~/code" and a new virtual environment "new_proj" inside "~/venvs" are created automatically and the virtual environment of "new_proj" automatically activated. Let's create a second project and show how easily we can switch between projects and their virtual environments.
$ mkproject new_proj2
...
Creating /home/ec2-user/code/new_proj2
Setting project for new_proj2 to /home/ec2-user/code/new_proj2

Switch to "new_proj" and its virtual env:

(new_proj2) [ec2-user@hostname new_proj2]$ workon new_proj
(new_proj) [ec2-user@hostname new_proj]$
You can see how easily you are able to switch between projects without deactivate, reactivate, switch folder, ..etc.!

You can list all the exisitng virtual environments:

# List environments
$ workon
new_proj2
new_proj

More information, checkout https://virtualenvwrapper.readthedocs.io/en/latest/

No comments: