Python and Django on Nano Server
One of Nano Server’s core scenarios is to serve as a lightweight OS for born-in-the-cloud applications running in a VM or a container. Nano Server already supports ASP.NET Core (aka ASP.NET 5) with IIS, node.js, and now, we’re happy to announce support for Python & Django.
The instructions in this blog explain how to get Python & Django running on Nano Server. If you want to try this on a Nano Server VM, with Windows Server 2016 Technical Preview 5 there are three ways you can quickly get a VM:
- Download the developer VHD at the above Technical Preview 5 link.
- Create a VM in Azure using the Nano Server image in the Azure Gallery. If you don’t have an Azure account, you can get a free 30-day trial.
- Download the Technical Preview 5 ISO at the above link and then consult the Nano Server documentation to build a VM.
Installing Python & Django on Nano Server
Download Python 3.5.1 to your development machine from python.org and make sure to select the 64-bit Windows version! Follow the installation instructions.
Once installed, use the following PowerShell script from the ISE or a classic console, to copy the binaries to your Nano Server machine:
$ip = “0.0.0.0” # the IP address of your Nano Server machine
$s = New-PSSession -ComputerName $ip -Credential ~\Administrator
$PythonInstallPath = “$env:LOCALAPPDATA\Programs\Python”
# this is the default installation folder – change it if you specified a different folder during installation
Copy-Item -ToSession $s -Path $PythonInstallPath -Destination “C:\” -Force -Recurse
Setting up your environment and running “Hello, world!”
Remote into your Nano Server machine:
Now add the Python folders to your path environment variable:
$env:Path += “;C:\Python\Python35;C:\Python\Python35\Scripts”
To make this change persist even after your Nano Server machine reboots, enter the following:
setx PATH $env:Path /M
You should see the Python version. Now, exit out of your Nano Server machine:
Back on your development machine, use your favorite editor (I use VS Code) and create a Python file (hello.py) with the following content:
Save the file and copy it to your Nano Server machine:
Copy-Item -ToSession $s -Path “.\hello.py” -Destination “C:\” -Force
Remote into your Nano Server machine again, and run:
Congratulations on running your first Python app on Nano Server J
Installing Django and creating your first app
Install Django and upgrade pip on Nano Server:
python -m pip install –upgrade pip
pip install django
Create a new Django project. This creates a new hello folder:
django-admin.exe startproject hello
copy .\urls.py views.py
Replace the contents of views.py with the following:
from django.http import HttpResponse
return HttpResponse(“Hello, Django! Hello, Nano Server!”)
Replace the contents of urls.py with the following:
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
Note: The project name “hello” is case-sensitive!
Open port 8000:
netsh advfirewall firewall add rule name=”TCP-Port” dir=in action=allow protocol=TCP localport=8000
Start the app:
python .\manage.py runserver 0.0.0.0:8000
Use a browser and enter the IP address of the Nano Server machine: http://18.104.22.168:8000, followed by a colon and the port number 8000.
Congratulations on your first Django app!
First Node.js and now Python & Django. These are the first in a series of app frameworks we will document how to get running on Nano Server. Let us know what your favorite app framework is, and be generous with your feedback on your Python & Django experience on Nano Server.
Happy coding J