PythonAnywhere

Table of contents

Child pages

Useful Bash commands

  • regarding "jobs" (running tasks / programs, analogous to "windows" on a GUI OS):
    • List jobs: 'jobs'
    • Alt-tab out of a job: 'Ctrl + z'
    • Switch to a job: 'fg job_number' (fg is for 'foreground')
  • Close / quit a bash console session: "exit". You'll still need to close the browser tab manually, but you won't end up with a longer-and-longer list of unused bash console sessions on your PA home page.

Virtual environments

Misc useful things to do


Tasks

  • Important: When you change your task, the path to the PythonAnywhere log file for that task also changes, so if you have the task's log open in a browser window, you cannot simply reload the page to see the updated task's log, or you'll continue to see the log file for the previous version of the task and be left wondering why the task doesn't seem to be running.

How to set up a scheduled task / periodic job / chron job

  • Background
    • These need to be set up just right or they won't work, and the error messages won't be very helpful. This can be a huge hassle, so follow the instructions below!
    • These instructions will assume you want to run a Python script.
  • Step-by-step instructions
    • If you're trying to run a very simple script that isn't part of some larger package and doesn't need to run from a virtualenv (to get access to certain modules):
      1. Follow the instructions here: https://help.pythonanywhere.com/pages/ScheduledTasks/
    • If you need to run from a virtualenv but aren't dependent on other modules from some parent package:
      1. Follow the instructions here: https://help.pythonanywhere.com/pages/VirtualEnvInScheduledTasks
    • If you're trying to run a script / module that depends on other modules in some larger package you're working on, and you need to run it from a virtualenv to get access to certain third-party modules it uses:
      1. Create a new bash script in the directory that contains the parent package that contains the script / module you're interested in. 
        1. I recommend naming the file after the module / function it'll be running.
        2. Usually that'll be your project directory.
        3. Example: /home/nathanwailes/project_name/, where there is no __init__.py in the directory, but there is a subfolder that does contain an __init__.py. So the package folder is /home/nathanwailes/project_name/package_name.
          1. 'project name' vs. 'package name' can be confusing, so just think of it like this: the 'package name' is the thing that other people would import in Python, while the 'project name' is the thing that would be cloned by git / source control.
      2. Copy the instructions below into the bash script to make it run the Python file. 
        1. Here's an example of what the bash script should look like:
          • #!/bin/bash
            source /home/nathanwailes/.virtualenvs/name_of_the_virtualenv_for_the_project/bin/activate
            cd /home/nathanwailes/name_of_the_project/
            python -m name_of_the_package.name_of_a_submodule.name_of_the_module_you_want_to_run
            deactivate
          • Note how the script does a 'cd' into the directory above the parent module. This is necessary for the next command ('python -m') to work properly.
          • Note how I don't run the submodule directly, but run 'parent_package.(...).module_name' instead. This is necessary if the module you're trying to run has imports of other modules from the package it is in.
            • Example: if the module you're trying to run is 'package_name.module_name', and it has a line that says 'from package_name.other_module import some_function', the only way to get that to work is to run it as 'package_name.module_name' so that Python will know about the other modules in the package.
      1. Make sure to use 'chmod' to allow your bash script to be executable!
        • If you don't, you may see a "Permission denied" error.
        • Example:
          • chmod 755 name_of_the_bash_file.bash

How to have a long-running task

Logging from a task