1. Home
  2. Languages
  3. Python Applications Management

Python Applications Management

Introduction

Python is essentially code executed on our servers to host, create and control web applications using this type of language.

In this article, we will focus on information for beginners. We will start by explaining how to access Python in N0C and provide a detailed example of how to launch and modify an application using this interface.

Then, for more advanced users, we will explain how to do the same operations in command line.

Note : The Glossary of N0C contains explanations on multiple topics and can be consulted to clarify certain terms.

Documentation

Documentation on Python can be found on the Web, in particular at: https://docs.python.org/3/.

You can refer to this site to get more information about the commands of the Python Selectorhttps://docs.cloudlinux.com/command-line_tools/#new-python-selector.

Debugging an Application

When an error occurs, a page like the one below is displayed:

This page is not very explanatory. Activating the development mode allows you to investigate. For more details, see the article Debugging a Node.js, Ruby or Python application with N0C (only available in French for the present moment).

Prerequisite

Go to https://mg.n0c.com/en/.

Access to Python with N0C

  1. Click on Languages > Python:
  1. Applications already set up are displayed in the interface.

Implementation of a New Python Application with N0C

To explain the approach more easily, we will proceed with an example.  

Let’s say you want to create the run.py application that displays the words “Hello World!“. The following steps are necessary:

  1. First of all, you have to create an application in the directory intended to contain it (we will call this directory “codepython” as an example, but the user can give it the name they desire). When it is executed, the application displays the words “It works!”.
  2. You then need to program it (in our example, to display “Hello World!”).
  3. Then, call the application environment.
  4. Finally, you have to activate the Python environment.

Step #1 – Create the Python Application

Make sure the codepython directory is created (see the article N0C FileManager if needed).

  1. Click on the Create button:
  1. Choose the desired VERSION of Python:
  1. Indicate in which APP DIRECTORY you want the application to be created (in our example, we would write codepython).
  2. Indicate from which APPLICATION DOMAIN/URL the application should be accessible (in our example, we would write codepython).
  3. Enter the name of the BOOT FILE (optional).
  4. Click on the SAVE button.
  5. Confirm the creation of run.py and passenger_wsgi.py1 by opening the codepython directory in the File Manager:
  1. Test the Python application by typing https://DOMAIN/codepython in your browser, DOMAIN having to be replaced by your real domain name.
  2. The result is (as expected): “It works!”.

Step #2 – Program the Application

Now that the Python application is installed and functional in the directory to be used, you can program it as we wish. The application must be started again to see the modifications in the browser when it is exectued. 

Here are the steps to program:

  1. Click on Files > File Manager.
  1. Open the directory (in our example, codepython):

3. To edit the code, right-click on run.py and click on Edit:

  1. Program run.py. In our example, copy the following code, so that the words “Hello World!” are displayed when you run codepython on your domain:
import os
import sys

sys.path.insert(0, os.path.dirname(__file__))

def app(environ, start_response):
     start_response('200 OK', [('Content-Type', ‘text/plain')])
     message = 'Hello World!n’
     version = 'Python %sn' % sys.version.split()[0]
     response = 'n'.join([message, version])
     return [response.encode()] 
  1. Click on the Save button to save the code:
  1. Click on the Close button to exit.
  2. Close N0C and open it again to apply the changes.
  3. Test the Python application by typing https://DOMAIN/codepython in your browser, DOMAIN having to be replaced by your real domain name.
  4. The result shall now be “Hello World!”.

Step #3 – Call the Environment of the Application

Before performing any other command, you must call the application environment by running the following command at the root of your application, which will set the environment variables and allow you to configure your application:

The command must be copied into the shell of the account in question. It is possible to connect via SSH.

Therefore, in our example, the command to be copied is :

source /home/fbkfdayw/virtualenv/codepython/3.8/bin/activate

Where :

  • « fbkfdayw » must be replaced by your username;
  • « codepython » must be replaced by the name of your application; and
  • « 3.8 » must be replaced by the Python version number.

Step #4 – Activate the Python Environment  

It is important to make sure that your application has the right versions of plugins.

  1. Click on the Restart icon next to the application to activate (in our example, codepython):        
  1. A message shall be displayed to confirm the successful execution of the command.

You can consult the documentation available at https://docs.python.org/3/ to help you debug your code.

Actions on Application with N0C

  1. Identify the application you want to modify and click on the Edit icon:
  1. Change the desired settings:
  1. Click on the SAVE button to save your changes.
  2. Execute the command to save the application environment.

Stopping and Starting an Application

It is possible to stop and start an application by clicking on the following icons:

Restart a Python Application

Sometimes it is necessary to restart an application as, for example, when you modify the code.

  1. Click on the Restart icon next to the application:
  1. A message shall be displayed to confirm the successful execution of the command.

Delete a Python Application

  1. Identify the application and click on the Delete icon:
  1. At the command prompt, confirm the deletion.

Implementation of a New Python Application in Command Line

For more advanced users, the command line approach can be used. 

The following steps must be followed in order:

  1. Connect in SSH.
  2. Find the availble Python versions.
  3. Create the functional base application at the CloudLinux level.

Step #1 – Connect in SSH

If required, see the article How to Create an SSH Key and Connect to an Account.

For example, if you are using the Apple MacOSX environment, you can enter:

ssh < USER >@ < DOMAIN > -p PORT

Where :

  • USER: Your World username.
  • DOMAIN: The domain name (make sure it points to the hosting account).
  • PORT : Port to use for World hosting (use 5022; use 2908 for VPS/HybridCloud hosting).

At the command prompt, enter your password.

Step #2 – Find the Available Python Versions

This command will return the Python versions available on the server:

cloudlinux-selector get --json --interpreter python | python -m json.tool

Step #3 – Creation of the Basic Functional Node.js Application at CloudLinux Level

The following command creates a functional base:

cloudlinux-selector create --json --interpreter python --app-root < DIRECTORY > --domain < DOMAIN > --app-uri < APP_URI > --version < VERSION > --startup-file < STARTUP_FILE > --entry-point app

Where :

  • DIRECTORY: The directory in which you want to install your application. Example: codepython.
  • DOMAIN: The domain on which you want to run your application. Example: domain.com.
  • APP_URI : The path of your application after your domain. For example, /codepython which would become the following URL: domain.com/codepython.
  • VERSION: An available version (according to the return of the command we saw before). Example: 3.3.
  • STARTUP_FILE: The name of the “input” file of your application. Default: run.py

In our example: 

cloudlinux-selector create --json --interpreter python --app-root codepython --domain domain.com --app-uri domain.com/codepython --version 3.3 --startup-file run.py --entry-point app

If the return is {“result”: “success”, “timestamp”: 1614167732.675758}, you now have a working basic Python application. 

Otherwise, an error message would be displayed and you would have to correct the command accordingly.

Actions on Application in Command Line

It is possible to start, stop or restart an application, to name just a few possible actions. Simply enter the appropriate command:

cloudlinux-selector (start | stop | restart | destroy) --json --interpreter python --app-root <APP_ROOT>

Where:

  • start: Allows you to start.
  • stop: Allows you to stop.
  • restart: Allows you to restart.
  • destroy: Allows you to delete.

In our example, if you want to start, you shall write:

cloudlinux-selector start --json --interpreter python --app-root codepython

Command Line Log Out

To log out, enter the following command:

exit

1Note: passenger_wsgi.py is the entry point of the application. It is a file and not an application. In other words, passenger_wsgi.py “redirects” to the file you have chosen (the file run.py in our example).

Updated on April 6, 2022

Related Articles