1. Home
  2. Docs
  3. User Guide
  4. Python
  5. Install Django on CWP

Install Django on CWP

Django is a free and open source high-level Python Web framework.Being a Python Web framework, Django requires Python

What Python version should I use with Django?

Python 3 is recommended. Django 1.11 is the last version to support Python 2.7. Support for Python 2.7 and Django 1.11 ends in 2020. Since newer versions of Python are often faster, have more features, and are better supported, the latest version of Python 3 is recommended.

You don’t lose anything in Django by using an older release, but you don’t take advantage of the improvements and optimizations in newer Python releases.

Django versions Python versions
1.11 2.7, 3.4, 3.5, 3.6, 3.7
2.0 3.4, 3.5, 3.6, 3.7
2.1, 2.2 3.5, 3.6, 3.7

 
Installation steps

1. Install the latest version of python

a) Prepare Your System

# yum -y groupinstall development
# yum -y install zlib-devel libffi-devel

b) Download python Source Code

# cd /usr/local/src
# wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz

c) Build Python

# cd /usr/local/src
# tar xvf Python-3.7.3.tgz
# cd Python-3.7.3
# ./configure --with-ensurepip=install --enable-shared
# make
# make install

After installation please create a symlink for the shared object library by using the following command:

ln -s /usr/local/lib/libpython3.7m.so.1.0 /usr/lib64/libpython3.7m.so.1.0

Upgrade pip

pip3.7 install --upgrade pip

Note: Please note that after installation, the new python binary path will be located at /usr/local/bin/python

2. Installing Django
The recommended way to install Django is to use pip
 
Install django with the following command:

# pip3.7 install django

This will install the latest version of django.To install a particular version of django,for example version 2.0

# pip3.7 install django==2.0

To tell Django is installed and which version, run the following command in a shell prompt

# python3.7 -m django --version

You can also confirm it’s installed correctly type:

3. Installing Django in Python Virtual Enviroment
 
The steps above are for installing django globally.However, Django can be installed into a virtualenv. Virtualenv is a tool used to create an isolated Python environment. This environment has its own installation directories that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either)
 
Installing Virtualenv using pip3

#pip3.7 install virtualenv 

3.1. You’ll need the full path to the Python 3 version of virtualenv, so run the following to view it:

#which virtualenv

3.2. Navigate to your site’s directory, where you’ll create the new virtual environment:

#cd /home/username

3.3. Create the virtual environment while you specify the version of Python you wish to use.

The following command creates a virtualenv named ‘project1’ and uses the -p flag to specify the full path to the Python3 version you just installed

#virtualenv -p /usr/local/bin/python3.7 project1

This command creates a local copy of your environment specific to this website. While working on this website, you should activate the local environment in order to make sure you’re working with the right versions of your tools and packages.

3.4. To activate the new virtual environment, run the following:

# source project1/bin/activate

The name of the current virtual environment appears to the left of the prompt. For example:

(project1) [root@server]#

Within the environment, install the Django package using pip. Installing Django allows us to create and run Django applications

(project1) [root@server]# pip3.7 install django

You can also install all other packages in the virtual environment that you may need in your development.

For Example, you can install mysqlclient by using the following command:

(project1) [root@server]# pip3.7 install mysqlclient

Going forward, whenever you create new Django projects you simply need to create a new virtual environment, install a fresh version of Django and other packages that may be needed.

3.5. Deactivating your virtualenv

When finished working in the virtual environment, you can deactivate it by running the following:

(project1) [root@server]# deactivate

This puts you back into your Shell user’s default settings.If you want to go back into your virtualenv, please refer to 3.4 above.

Was this article helpful to you? Yes 3 No 1