In this tutorial we’re going to be extending our current install to include python. We will be going over a basic python install, setting up our virtual environment and touch on how to test our multiple virtual environments. This also includes how this setup will benefits us vs. just installing python packages globally so we can have multiple version of our projects with differing package requirements.
When we finish this third part of the tutorial we will have a ready to go python install on top of our web stack, all that we will have left to do is configure nginx to serve the pages from our project and we will be live and running. That will come in the last part of this tutorial series when we get sanic and a basic project skeleton setup and running live on our server.
Contents:
(Part 1) Initial Ubuntu 16.04 Server Setup
- Getting Started
- SSH-Key Authentication Setup
- Finishing Up
(Part 2) Web Stack Install and Configuration
- Nginx
- MySQL
- PHP
- Lets Encrypt
- Finishing Up
(Part 3 (You Are Here)) Python Environment Setup
- Installing Software
- Setting Up the VirtEnv
- Finishing Up
(Part 4) Sanic Project Setup
- Getting Files Setup
- Setting Up Project Defaults
- Running Our Project
- Where to go from here?
Why Python?
As a new python developer, i find it a easy language to learn and develop in. I also find the huge community behind both versions of python to be a great benefit to any developer and this includes the huge number of packages available to choose from to enhance your own projects.
As developers we really love having the choices of different supported version of software to ensure we always have a working version for our application. We hate to be pressed to “Fix” or “Update” a old codebase to something new quickly as it just introduces more problems then solutions. We love the fact we can have our live code running on a older more stable version while testing on a newer version to fix and find those pesky bugs and ensure all our functions, methods and such work as intended. For this i love python, it maintains two current version of the language currently 2.7 | 3.6 both of which are still actively used for projects.
Now i know a lot of people have differing opinions on which version to use and as to why their preferred version is better then the other and vice verse but this is not that type of article. In this tutorial we will use Python version 3.5+ as that’s a requirement for sanic and that is our end goal here after all. Also so we keep everything current for ease of install we will do the same with python and utilize version 3.6.
The next thing we need to go over before we get our install running is the “virtual environment” for python. The virtual environment will create a isolated environment for our projects packages to reside in separately from our other instances of python virtual environments. This will allow us to keep different dependencies available to our projects without installing them globally for all projects. By doing this we can ensure our projects are running only the dependencies we want and not others we need for other projects.
And finally a virtual environment has no baring on our codes structure, we will do that ourselves, rather this only keeps our python packages in order for use to have when we need them. If this is hard to grasp, worry not i’ll have a example towards the end of this part of the tutorial for you to test our yourself.
Installing Python
To get started, from the terminal we are going to need to run a few commands, including adding the repository since we initially setup Ubuntu 16.04 (if you are reading this on 16.10+ you can install from the apt command). Let’s get started by running the following commands:
sudo add-apt-repository ppa:jonathonf/python-3.6
(Enter at the prompt)
sudo apt-get update
sudo apt-get install python3.6 python3.6-dev python3.6-venv python3-pip
This completes the python setup, not much more we need to do to get any of our projects up and running.
Getting Our Virtual Environment Setup
Now that we have python installed we can finally get our virtual environment setup and our first project ready, this is going to be short and sweet to ensure we have our project setup and ready to run isolated from the rest. We simply need to issue a few short commands, this includes setting up a directory to house our environments and then actually making the virtual environment.
mkdir ~/.venvs
python3.6 -m venv ~/.venvs/helloworld
And from that we have setup our virtual environment and are now ready to use it to actually make some code.
source ~/.venvs/helloworld/bin/activate
(simply deactivate to drop back to terminal not in our venv)
mkdir ~/hello
nano hello.py
print("Hello, World")
Save and Close and run, by using just the regular python command while we have our venv activated and we should see the output on the terminal.
python hello.py
Now this wasn’t a real test to ensure that we have a different environment for our python script, more so it just give us a easier to use python and pip command. Below we will do a real test to ensure out environments are isolated from each other.
Testing Environment
Now to test a bit to ensure we have everything correctly setup:
python3.6 -m venv ~/.venvs/test
pip install bcrypt
nano test.py
import bcrypt
password = b"p@ssw0rd!"
hashed = bcrypt.hashpw(password, bcrypt.gensalt())
if bcrypt.hashpw(password, hashed) == hashed:
print("It Matches!")
else:
print("It Does not Match :(")
Now if we try this simple script without a environment activated then we will get the following python error:
python3.6 test.py
bryson@whitedog-test:~$ python3.6 test.py Traceback (most recent call last): File "test.py", line 1, in <module> import bcrypt ModuleNotFoundError: No module named 'bcrypt' Error in sys.excepthook: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook from apport.fileutils import likely_packaged, get_recent_crashes File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module> from apport.report import Report File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module> import apport.fileutils File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module> from apport.packaging_impl import impl as packaging File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module> import apt File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module> import apt_pkg ModuleNotFoundError: No module named 'apt_pkg'Original exception was: Traceback (most recent call last): File "test.py", line 1, in <module> import bcrypt ModuleNotFoundError: No module named 'bcrypt'
Now if we run the the script again after we enable our test environment, we’re greeted with with the expected output:
source ~/.venvs/test/bin/activate
python test.py
(test) bryson@whitedog-test:~$ python test.py It Worked
Finishing Up
By finishing up this tutorial, we now have our server fully setup and ready to run our projects in the future. We have made a easy to use clear separation of our projects dependencies and have a way to run multiple versions of multiple projects without messing with other projects.
In the next tutorial we will go over setting up Sanic and getting it ready and being served by nginx. Until then, i hope you have enjoyed the tutorial and as always if any comments or feedback feel free to leave a comment.