This will be part 3 of our Introduction to Python Flask Series.
In this section we will be covering our Environment Setup, where I will be showing you how to setup a typical Python Flask Environment using virtualenv.
What is VirtualEnv?
Virtualenv allows you to have isolated Python Environments, where each project or environment can have their own versions. Some applications may need a specific version of a certain package, so lets say you are running multiple applications on one server, then having to manage each ones dependencies can be a pain. As you may run into scenarios where they are dependent on specific versions, where you have to upgrade/downgrade packages like no-ones business.
Luckily with the help of virtualenv, each environment is isolated from each other, so system wide you might be running Python 2.7 with minimal packages installed, then you can create a virtual environment with Python 3 with packages for the application you are developing.
What will we be installing today:
- Python Setuptools
- Python 2.7
- Python 3.4
- Pip (Python Package Manager)
- Virtualenv
Installing Dependencies for Python
Dependencies: RHEL Based
$ yum groupinstall 'development tools' -y
$ yum install zlib zlib-devel xz-devel lib-devel libevent-devel openssl-devel sqlite-devel bzip2-devel -y
Dependencies: Debian Based
$ apt-get install build-essential libssl-dev libffi-dev zlib1g-dev -y
Installing Python 2.7:
$ cd /usr/srv
$ wget http://python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz
$ tar xf Python-2.7.6.tar.xz
$ cd Python-2.7.6
$ ./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
$ make && make altinstall
$ curl https://bootstrap.pypa.io/ez_setup.py | python2.7
$ easy_install-2.7 pip
$ pip2.7 install virtualenv
Installing Python 3.4:
$ cd /usr/src
$ wget https://www.python.org/ftp/python/3.4.4/Python-3.4.4.tgz
$ tar xzf Python-3.4.4.tgz
$ cd Python-3.4.4
$ ./configure --with-zlib-dir=/usr/local/lib --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
$ make && make altinstall
$ curl https://bootstrap.pypa.io/ez_setup.py | python3.4
$ easy_install-3.4 pip
$ pip3.4 install virtualenv
A full script that can setup multiple python versions can be found here
Now we should have 2 versions of Python running on our host, next we will setup 2 virtual environments, one running python 2.7 with flask, and python 3.4 with flask.
Creating our Virtual Environment for Python 2.7:
First, for our Python 2.7 app, we will be creating our application directory and initialize our virtualenv.
$ mkdir /opt/apps/flask-app-1
$ cd /opt/apps/flask-app-1
$ virtualenv -p /usr/local/bin/python2.7 .venv
Now, we will enter our virtualenv:
$ source .venv/bin/activate
Note, that when we query python for the version you will see that we are using the python version we specified when creating our virtualenv.
$ python --version
Python 2.7.6
While we are in our virtualenv, lets install Flask and Requests:
$ pip install flask
$ pip install requests
With pip we can list the installed packages we have with pip freeze
:
$ pip freeze
click==6.7
Flask==0.12
itsdangerous==0.24
Jinja2==2.9.5.1
MarkupSafe==1.0
requests==2.7.0
six==1.10.0
virtualenv==15.0.1
Werkzeug==0.12.1
We can dump this to a file, which we can later use to install packages from a list so that we don't have to specify them manually. We can dump them by doing this:
$ pip freeze > requirements.txt
Now lets say you are on a different host and you would like to install the packages from the requirements.txt
file, we do this by using the following command:
$ pip install -r requirements.txt
To exit your virtualenv, you do the following:
$ deactivate
Creating our Virtual Environment for Python 3.4:
$ mkdir /opt/apps/flask-app-2
$ cd /opt/apps/flask-app-2
$ virtualenv -p /usr/local/bin/python3.4 .venv
Now, we will enter our virtualenv:
$ source .venv/bin/activate
Now, when we query python for the version you will see that we are using the python version we specified when creating our virtualenv.
$ python --version
Python 3.4.4
I hope this was useful, next up will be Routing in Flask
Comments