{"id":340,"date":"2022-06-23T00:00:00","date_gmt":"2022-06-23T00:00:00","guid":{"rendered":"https:\/\/tac.debuzzify.com\/?p=340"},"modified":"2023-06-27T01:31:13","modified_gmt":"2023-06-27T01:31:13","slug":"python-run-command-in-terminal","status":"publish","type":"post","link":"https:\/\/www.the-analytics.club\/python-run-command-in-terminal\/","title":{"rendered":"4 Ways to Run Python Commands from Terminal"},"content":{"rendered":"\n\n\n

You can run Python commands from your terminal or command prompt. Since Python is an interpreted language, you don’t have to compile it before doing so.<\/p>\n\n\n\n

But you could do it in several different ways. Each has its merits, so they work well in one instance and not in the other.<\/p>\n\n\n\n

\n
\n
\n

Grab your aromatic coffee <\/a>(or tea<\/a>) and get ready…!<\/p>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n

1. Python REPL: the beginner-friendly way to run Python commands<\/h2>\n\n\n\n

REPL stands for Read-Evaluate-Pring-Loop. As it suggests, the REPL environment will evaluate each line you enter and print its results immediately on the terminal.<\/p>\n\n\n\n

 <\/p>\n\n\n

\n
\"Screenshot<\/figure><\/div>\n\n\n

 <\/p>\n\n\n\n

When you’re learning Python<\/a>, you’d almost always use a REPL. It’s the easiest way to execute Python commands.<\/p>\n\n\n\n

Just type “python” on the terminal. It’ll take you to an interpreter environment. This is where you’d probably write your print (\"Hello World!\")<\/code><\/p>\n\n\n\n

REPLs are very powerful. Even the most experienced still use REPLs to test out various aspects of their application.<\/p>\n\n\n\n

For instance, Django developers use the shell<\/a> to test out database queries for their apps. Most data scientists use Jupyter Notebooks, an evolved version of REPLs.<\/a><\/p>\n\n\n\n

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
python<\/span> <\/span>manage.py<\/span> <\/span>shell<\/span> <\/span># To get a Django shell<\/span><\/span><\/code><\/pre>Bash<\/span><\/div>\n\n\n\n

The notable advantage of REPLs is that we don’t have to wait until the entire program evaluates. You could run the command and see the output then and there.<\/p>\n\n\n\n

Yet, there are instances where we don’t even want to go into a different environment. The following method comes in handy in many ways.<\/p>\n\n\n\n

2. Run the Python command from the terminal and get output instantly.<\/h2>\n\n\n\n

There are instances where you need to execute some Python commands instantly. Going into a REPL won’t take much time. But there is a better way to do this.<\/p>\n\n\n\n

You can use the “-c” option to follow up with a command to execute. I use this very often to generate secret tokens. The following Python command generates a random token of 64 characters.<\/p>\n\n\n\n

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
python<\/span> <\/span>-c<\/span> <\/span>"<\/span>import secrets; print(secrets.token_urlsafe(64))<\/span>"<\/span><\/span><\/code><\/pre>Bash<\/span><\/div>\n\n\n\n

 <\/p>\n\n\n

\n
\"Screenshot<\/figure><\/div>\n\n\n

 <\/p>\n\n\n\n

I use this one very often to generate secure passwords quickly.<\/p>\n\n\n\n

Since Python is a very concise language, these sorts of one-liners are prevalent. As a programmer, you’ll have many instances where you need to execute Python<\/a> scripts on the go.<\/p>\n\n\n\n

3. Run a Python script in the terminal<\/h2>\n\n\n\n

You can run Python scripts on the terminal in two ways<\/a>. First, you can type Python (or Python3) followed by the path to your script file. The second option is you can make the Python script file an executable.<\/p>\n\n\n\n

The easiest is to use the first one. You could also change the Python interpreter without editing the script file.<\/p>\n\n\n\n

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
python<\/span> <\/span>hello.py<\/span><\/span>\n>><\/span> Hello, world<\/span>!<\/span><\/span><\/code><\/pre>Bash<\/span><\/div>\n\n\n\n

Yet, if you use the script often and the interpreter remains the same, the best option is an executable. You can specify the interpreter path at the top of the page, as shown below.<\/p>\n\n\n\n