I am trying to configure my vim editor in Ubuntu 18.04 to automatically run a python file inside the editor.   The problem seems to be that, on my Dell Inspiron, the function keypresses are being captured by the GUI.  For example, pressing F9 brings up a display of all my open windows. 
I've also tried setting a-F9 as well, but with no luck.  The command itself works manually, but vim won't run it when I press the key that's mapped to the command. 
Here's an example of the lines in the .vimrc that I've tried...
autocmd filetype python nnoremap <buffer> <F9> :exec '!clear; python' shellescape(@%, 1)<cr>
and...
autocmd filetype python nnoremap <buffer> <a-F9> :exec '!clear; python' shellescape(@%, 1)<cr>
I've also tried this:
imap <F5> <Esc>:w<CR>:!clear;python %<CR>
None of them seem to work.  Pressing the key doesn't run the command. 
Any help appreciated. 
Thanks
You can insert the literal key inside a buffer (here: your .vimrc) by pressing <C-V> followed by the key (e.g. <F9>). If that function key correctly arrives in Vim, the literal <F9> string (4 characters) should be inserted. If that's not the case, you first need to remove the interference by the surrounding system. It may be some bloatware (less likely on Linux though) that captures the function keys (uninstall it then), but I've also seen notebooks being configured with special functions (also volume / brightness up / down) on the function keys, and the actual function key is only sent via a combination with a special Fn key; that usually can be toggled in the BIOS. You can test other applications (e.g. the browser should react to <F5> with reload and <F11> with full-screen) to see if this indeed is a global problem. As you're on Linux, you can also use the xev tools for this.
Your key mappings in itself are fine. I would recommend putting any settings, mappings, and filetype-specific autocmds into ~/.vim/ftplugin/{filetype}_whatever.vim (or {filetype}/whatever.vim (for Python mappings, I'd choose python_mappings.vim); cp. :help ftplugin-name) instead of defining lots of :autocmd FileType {filetype}; it's cleaner and scales better; requires that you have :filetype plugin on, though. Settings that override stuff in default filetype plugins should go into ~/.vim/after/ftplugin/{filetype}.vim instead.
Function keys itself should also work in terminals, in combinations with modifier keys like Shift and Alt often not. Therefore, another approach (for maximum portability, e.g. when you're working through SSH) would be to skip the function keys altogether and go for <Leader> mappings.
Trying to set up an autocmd in .vimrc to map a key to a command, but having problems