Python Setup - SamsungResearchUK-IoT-Meetup/multimode_sensor_platform GitHub Wiki
Python Setup Guide
Welcome to the setup guide for python on the Multimode Sensor Platform. This guide makes sure you have everything you need in your python environment to get your project up and running. It concentrates on making sure you have a python virtual environment to ensure you use the correct python version, packages and libraries. Furthermore, it ensures that nothing in this project will contaminate or break anything on your system.
First lets make sure you have the tools and libraries for creating your virtual environment.
Prerequisites
We assume you are running a typical linux distribution. We are using:
- Linux Ubuntu Xenial 16.04 or later.
- Default Python 2.7.12 (on Ubuntu 16.04)
- Python 3 installed on your machine. You can check this with:
$\> /usr/bin/python3.6 --version
Python 3.6.9
Installing Your Virtual Environment
Now that Python 2.7 is end of life we are going to use pip3 to install the tool 'virtualenv' and 'virtualenvwrapper'. This is slightly more convoluted but still fairly straight forward. Make sure you have the 'pip' tool for python 3 installed. Pip is used to install python packages:
$/> sudo apt install python3-pip
To verify you have pip3 installed for python3 do:
$/> pip3 --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
It should say that it's installed for python 3.6. Next install virtualenv via pip3:
$/> pip3 install virtualenv
To see where it's installed virtualenv in your machine do:
$/> which virtualenv
/home/nherriot/.local/bin/virtualenv
Notice it's installed into your home directory under ~/.local/bin/ Next install virtualenvwrapper with:
$/> pip3 install virtualenvwrapper
Setup Your Local Directory and Environment Variables
Your virtual environment is really a separate directory with a local python distribution in the directory you create for your project. Each project will have it's local copy of python and the libraries and packages it uses. Hence never mixing up with other packages and libraries in other projects. For this we have created a virtual environment directory where our projects and virtual environments go. First create a directory to hold your virtual environments:
$/> mkdir virtualenv
Next we need to update our environment variables so that when you use a terminal window it knows about your virtual environment settings. We are going to add lines to:
- Tell your terminal to now use python3 to create virtual environments.
- Tell your terminal session where your virtual environment is.
- Tell your terminal session where you want projects stored.
- Tell virtualenvwrapper where your virtualenv binary file is.
- Execute the virtualenvwrapper script.
In your .bashrc file add the following lines. Make sure you replace with the home dir on your machine.
# Added to get virtualenvwrapper working properly.
# See: https://virtualenvwrapper.readthedocs.io/en/latest/
# Added to force virtualenv to use python3
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
# My machine has virtual environments here.
export WORKON_HOME=/home/<your home directory>/virtualenv
# Where projects will reside. I have my projects also in my virtual environment
export PROJECT_HOME=/home/<your home directory>/virtualenv
# Where the virtualenv binary file lives after installation
export VIRTUALENVWRAPPER_VIRTUALENV=/home/<your home directory>/.local/bin/virtualenv
# Following instructions to install virtualenv and virtualenv wrapper with python3 and pip3
# the virtualenvwrapper script now lives here:
# source $HOME/.local/bin/virtualenvwrapper.sh
source ~/.local/bin/virtualenvwrapper.sh
Note: Make sure you replace with the name of your home directory! :-) Now make sure all this works. Run your .bashrc file again with:
$/> source .bashrc
Creating Your Virtual Environment
Now you have everything installed and setup to create your python virtual environment. Lets create a project to hold all your work and set a specific python version for the project. We will be using python 3. Python 3 may already be setup on your machine but not active and used. You can check this in our prerequisites section above.
In the example below we have used multimode_sensor_platform as the name of the project. Please use the name you wish to call your project instead. Create your project using the command:
$/> mkvirtualenv multimode_sensor_platform
Running virtualenv with interpreter /usr/bin/python3.6
Using base prefix '/usr'
New python executable in /home/nherriot/virtalenv/multimode_sensor_platform/bin/python3.6
Also creating executable in /home/nherriot/virtalenv/multimode_sensor_platform/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
virtualenvwrapper.user_scripts creating /home/nherriot/virtalenv/multimode_sensor_platform/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/nherriot/virtalenv/multimode_sensor_platform/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/nherriot/virtalenv/multimode_sensor_platform/bin/preactivate
virtualenvwrapper.user_scripts creating /home/nherriot/virtalenv/multimode_sensor_platform/bin/postactivate
virtualenvwrapper.user_scripts creating /home/nherriot/virtalenv/multimode_sensor_platform/bin/get_env_details
(multimode_sensor_platform) nherriot@Zenbook-UX32A ~/virtalenv/multimode_sensor_platform $
You should notice that you have an 'active' virtual environment. In the shell output it will show that your virtual environment is called (multimode_sensor_platform). You can check python is now at version 3.6 and being run from inside your new project directory by typing the python --version command and using the 'which' command like:
(multimode_sensor_platform) nherriot@Zenbook-UX32A ~/virtalenv/multimode_sensor_platform $ python --version
Python 3.6.9
(multimode_sensor_platform) nherriot@Zenbook-UX32A ~/virtalenv/multimode_sensor_platform $ which python
/home/nherriot/virtalenv/multimode_sensor_platform/bin/python
Now you are set to go! :-)