Print output with color style in GDB python script? - gdb-python

I'd like to call print('somestr') in a python script called in GDB ((gdb) source my_script.py), and style the output in console colors. I tried the python colorama package which works in terminal, but not when invoking from GDB. Is there a way to print in colors in GDB python script? Haven't got any clue from the Python API below.
https://sourceware.org/gdb/onlinedocs/gdb/Python-API.html

Related

Why can't I run python scripts from the command line interactive session using "./ name.py"?

I'm following along with Google's Python class, and the person in the videos always runs his scripts from the interactive session in command line using "./". Whenever I try it, I just get a syntax error. How can I use ./ to run scripts? I'm using Windows 10
To run a script from the command line you need to use the syntax
python3 script.py
Now on Unix systems, it's possible to add a shebang to the first line of the script as followings
#!/usr/bin/env python3
This then allows the shell syntax './name.py' to work. But windows doesn't have this mechanism. Instead, you need to create an 'association' between the .py extension and the python executable ('right click', 'open with'). Or just use the full syntax. Both require the python executable to be in your path, and generally on windows both python 2 and 3 will have the same executable name

Running a python script in windows

I am new to Python.
I am executing a script in windows, but it does nothing and gives an error "invalid syntax".
Can you help me with the error. I have set the PATH variable to include my python.exe location.
Please see attached image:
Your image is from the python command line, only python commands are accepted there and there is no need to specify python in the beginning (in the shell python is just a call to the python interpeter).
In order to run windows commands in python use the os or subprocess modules.

commands for executing a program in python shell

I need the exact commands to execute a program in the python shell in terminal.
My file is located in Home/Pictures/pythonpractice folder and its called fibonacci.py
which commands should I give to execute it in terminal.? please write the details.
python Home/Pictures/pythonpractice/fibonacci.py

Beginner Python spam.py

I am new to python, just got the learning python book and got stuck with the spam.py in the command line. The book says to make a file named spam.py and then ask python to run this by typing
%python spam.py
I have added the python to my PATH as it was C:\Python27 so I can call Python in the Windows CMD, but this just will not run. The error I receive is
>>>python spam.py
File "<stdin>", line 1
python spam.py
^
SyntaxError: invalid syntax
I appreciate any help that you can give.
Your problem is that you're trying to run your code from within the Python interpreter itself (the >>> prompt is the giveaway here since that's the Python prompt).
Exit from the interpreter (with CTRL-Z and ENTER for Windows) and run it from cmd.exe (the c:\> is the prompt in the example below):
c:\> python spam.py
From within the interpreter, you can also run an external file with:
execfile('spam.py')
Could you post the code from spam.py...
You seem to be trying to run the spam.py from the Python interpreter. Go to where the file is in Windows Explorer and launch it from there, using the C:\Python2.7\python.exe CLI.
By the way, since you didn't understand the syntax error warning, please see:
http://dictionary.reference.com/browse/SYNTAX
http://dictionary.reference.com/browse/SEMANTICS
You are trying to execute Python script file within the interpreter. Come out from the Python interpreter by pressing CTRL+Z and then ENTER key.
Then execute with the command :
Say, C:/> python spam.py
In order to run a python program you have to run program in Command Line not in Python Interpreter (press Windows Sign + R and type cmd.exe)
Moreover you have to remember to be exactly in the directory where your file is saved, e.g.:
If the file file is C:\Python27\spam.py you have to be in C:\Python27.
To change the directory:
use dir to display the folders and files in current place
use cd to change your directory (e.g. C:\Python27\>cd Spam moves you to C:\Python27\Spam
use Tab key to autocomplete names of the commands, folders and files
As you said you have added Python to PATH and followed my instructions, the statement below should work perfectly
python spam.py
Hope I could help.
If for example, your file is in G: drive, type this in cmd:
python G:/myfile.py
Basically, type in the path. Just doing "cd" won't work in Python

learning python hello world script not executing

I am new to Python and learning to program in it.
I wrote a basic script
my.py
#! /bin/python
print "hello world!"
but this does not execute.
I did
chmod +x my.py
and
./my.py
I have tried removing the blank space in my program
#!/bin/python
but even this gives me error
bash: ./my.py: /bin/python: bad interpreter: No such file or directory
I am using Ubuntu 12.04 and Python is present on system.
I have checked the program by typing on terminal
So what could be the problem here?
The error means there is no file at the path /bin/python
Try which python to see the location of the interpreter
But why not python my.py?