Jaa


Python & 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:

  1. Download the developer VHD at the above Technical Preview 5 link.
  2. 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.
  3. 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:

Enter-PSSession $s

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 

Quick test:

python --version

You should see the Python version. Now, exit out of your Nano Server machine:

Exit-PSSession

Back on your development machine, use your favorite editor (I use VS Code) and create a Python file (hello.py) with the following content:

print("Hello, world!")

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:

Enter-PSSession $s

python c:\hello.py

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

Create views.py:

cd .\hello\hello

copy .\urls.py views.py

psEdit .\views.py

Replace the contents of views.py with the following:

from django.http import HttpResponse 

def index(request): 

     return HttpResponse("Hello, Django! Hello, Nano Server!")

Edit urls.py:

psEdit .\urls.py

Replace the contents of urls.py with the following:

from django.conf.urls import url

from django.contrib import admin

urlpatterns = [

    # Admin

url(r'^admin/', admin.site.urls),

    #

    url(r'', 'hello.views.index'), 

]

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:

cd ..

python .\manage.py runserver 0.0.0.0:8000

Use a browser and enter the IP address of the Nano Server machine: https://1.2.3.4:8000, followed by a colon and the port number 8000.

Congratulations on your first Django app!

What’s next?

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

 

ref@

Comments

  • Anonymous
    June 12, 2016
    Are there plans for supporting Python 2.7?(My company has several apps running on Python 2.7.)
    • Anonymous
      June 14, 2016
      Hi Maarten,We're looking into it, but cannot commit to it at this time.ref@