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)
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 ``` <p> ==Dependencies: Debian Based== ```language-bash $ apt-get install build-essential libssl-dev libffi-dev zlib1g-dev -y ``` <p> **Installing Python 2.7:** ```language-bash $ 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 ``` <p> **Installing Python 3.4:** ```language-bash $ 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 ``` <p> A full script that can setup multiple python versions can be found [here](https://goo.gl/8yEFe5) 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. ```language-bash $ mkdir /opt/apps/flask-app-1 $ cd /opt/apps/flask-app-1 $ virtualenv -p /usr/local/bin/python2.7 .venv ``` <p> Now, we will enter our virtualenv: ```language-bash $ source .venv/bin/activate ```<p> 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. ```language-bash $ python --version Python 2.7.6 ``` <p> While we are in our virtualenv, lets install Flask and Requests: ```language-bash $ pip install flask $ pip install requests ``` <p> With pip we can list the installed packages we have with `pip freeze`: ```language-bash $ pip freeze click==6.7 Flask==0.12 itsdangerous==0.24 Jinja2==126.96.36.199 MarkupSafe==1.0 requests==2.7.0 six==1.10.0 virtualenv==15.0.1 Werkzeug==0.12.1 ``` <p> 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: ```language-bash $ pip freeze > requirements.txt ``` <p> 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: ```language-bash $ pip install -r requirements.txt ``` <p> To exit your virtualenv, you do the following: ```language-bash $ deactivate ``` <p> **Creating our Virtual Environment for Python 3.4:** ```language-bash $ mkdir /opt/apps/flask-app-2 $ cd /opt/apps/flask-app-2 $ virtualenv -p /usr/local/bin/python3.4 .venv ``` <p> Now, we will enter our virtualenv: ```language-bash $ source .venv/bin/activate ```<p> Now, when we query python for the version you will see that we are using the python version we specified when creating our virtualenv. ```language-bash $ python --version Python 3.4.4 ``` <p> I hope this was useful, next up will be [Routing in Flask](https://sysadmins.co.za/python-flask-series-routing-in-flask-p4/)
Subscribe to SysAdmins | Linux Tutorials
Get the latest posts delivered right to your inbox