How to run scripts from the python shell?
Running python script from the command line or terminal is pretty easy, you just have to type run python script_name.py and it is done. Take a look at the example below to see how it is done:
$ python script_name.py # python "path/script_name.py" if you have terminal/cmd open in some other directory.
But, what if you want to run the script from IDLE python shell itself? Sometimes I run the script from the python itself to just showoff :D. Here’s how you can do it too.
Commands
For Python2: If you are using Python2 like me, then you can use execfile() function like this:
>>> execfile("script_name.py") # Or, execfile("path/script_name.py") if shell is not opened in the same directory where the script is present.
Check out the example for more clarity
For Python3 : But, if you use python version 3+, you can’t really use execfile() as the function’s been deprecated(to read more info about that, read here). Instead of that, you can exec(open(«path/script_name.py»).read()) . It is just the same.
>>> exec(open("script_name.py").read()) # Or, exec(open("path/script_name.py").read()) if shell is not opened in the same directory where the script is present.
The above steps will work just fine, but what if your script takes command-line arguments? Well, there’s a way to pass the command-line arguments too. But first, let’s make one easy script using the command-line arguments in it.
print("This is a python script") x = sys.argv[0] y = sys.argv[1] print("Additon of two numbers <> and <> is <>".format(x,y,x+y))
Now, to be able to execute the above script from the Python shell, we first have to set the command-line args. Below are the steps to execute the above script from the python shell:
Commands
>>> import sys # Module to set command-line arguments >>> sys.argv = [11, 33] # Set the command line arguments first >>> execfile("script_name.py") # Or, execfile("path/script_name.py") if shell is opened in some other location than the directory having the script
Find the attached screenshot to understand the steps better.
Note: The above steps are for python 2.7.13, for python 3, just replace execfile(«path_to_script») with exec(open(«path/script_name.py»).read()) and it should work just fine.
That’s all, folks .
Run Python Script – How to Execute Python Shell Commands in the Terminal
Suchandra Datta
When you’re starting out learning a new programming language, your very first program is likely to be one that prints «hello world!».
Let’s say you want to do this in Python. There are two ways of doing it: using the Python shell or writing it as a script and running it in the terminal.
What is a Shell?
An operating system is made up of a bunch of programs. They perform tasks like file handling, memory management, and resource management, and they help your applications run smoothly.
All the work we do on computers, like analyzing data in Excel or playing games, is facilitated by the operating system.
Operating system programs are of two types, called shell and kernel programs.
Kernel programs are the ones who perform the actual tasks, like creating a file or sending interrupts. Shell is another program, whose job is to take input and decide and execute the required kernel program to do the job and show the output.
The shell is also called the command processor.
What is a Terminal?
The terminal is the program that interacts with the shell and allows us to communicate with it via text-based commands. This is why it’s also called the command line.
To access the terminal on Windows, hit the Windows logo + R, type cmd, and press Enter.
To access the terminal on Ubuntu, hit Ctrl + Alt + T.
What is the Python Shell?
Python is an interpreted language. This means that the Python interpreter reads a line of code, executes that line, then repeats this process if there are no errors.
The Python Shell gives you a command line interface you can use to specify commands directly to the Python interpreter in an interactive manner.
You can get a lot of detailed information regarding the Python shell in the official docs.
How to Use the Python Shell
To start the Python shell, simply type python and hit Enter in the terminal:
C:\Users\Suchandra Datta>python Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>print("hello world!")
The interactive shell is also called REPL which stands for read, evaluate, print, loop. It’ll read each command, evaluate and execute it, print the output for that command if any, and continue this same process repeatedly until you quit the shell.
There are different ways to quit the shell:
- you can hit Ctrl+Z on Windows or Ctrl+D on Unix systems to quit
- use the exit() command
- use the quit() command
C:\Users\Suchandra Datta>python Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print("HELLO WORLD") HELLO WORLD >>> quit() C:\Users\Suchandra Datta>
C:\Users\Suchandra Datta>python Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> exit() C:\Users\Suchandra Datta>
C:\Users\Suchandra Datta>python Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> ^Z C:\Users\Suchandra Datta>
What Can You Do in the Python Shell?
You can do pretty much everything that the Python language allows, from using variables, loops, and conditions to defining functions and more.
The >>> is the shell prompt where you type in your commands. If you have commands that span across several lines – for example when you define loops – the shell prints the . characters which signifies that a line continues.
>>> >>> watch_list = ["stranger_things_s1", "stranger_things_s2", "stranger_things_s3","stranger_things_s4"] >>> >>>
Here we defined a list with some TV show names via the Python shell.
Next, let’s define a function that accepts a list of shows and randomly returns a show:
>>> def weekend_party(show_list): . r = random.randint(0, len(show_list)-1) . return show_list[r] .
Note the continuation lines ( . ) of the Python shell here.
Finally to invoke the function from the shell, you simply call the function the way you would do in a script:
>>> weekend_party(watch_list) 'stranger_things_s1' >>> >>> >>> weekend_party(watch_list) 'stranger_things_s3' >>> >>> >>> weekend_party(watch_list) 'stranger_things_s2' >>> >>> >>> weekend_party(watch_list) 'stranger_things_s2' >>> >>> >>> weekend_party(watch_list) 'stranger_things_s3' >>>
You can inspect Python modules from the shell, as shown below:
>>> >>> >>> import numpy >>> numpy.__version__ '1.20.1' >>>
You can see what methods and attributes a module offers by using the dir() method:
>>> >>> x = dir(numpy) >>> len(x) 606 >>> x[0:3] ['ALLOW_THREADS', 'AxisError', 'BUFSIZE']
Here you can see that Numpy has 606 methods and properties in total.
How to Run Python Scripts
The Python shell is useful for executing simple programs or for debugging parts of complex programs.
But really large Python programs with a lot of complexity are written in files with a .py extension, typically called Python scripts. Then you execute them from the terminal using the Python command.
All the commands we executed previously via the shell, we can also write it in a script and run in this way.
Conclusion
In this article, we learnt about the shell, terminal, how to use the Python shell. We also saw how to run Python scripts from the command line.
I hope this article helps you understand what the Python shell is and how you can use it in your day to day lives. Happy learning!