Python3:

exec(open('helloworld.py').read())

If your file not in the same dir:

exec(open('./app/filename.py').read())

See https://stackoverflow.com/a/437857/739577 for passing global/local variables.

Note: If you are running in windows you should use double slash "//" otherwise it gives error


In deprecated Python versions

Python2 Built-in function: execfile

execfile('helloworld.py')

It normally cannot be called with arguments. But here's a workaround:

import sys
sys.argv = ['helloworld.py', 'arg']  # argv[0] should still be the script name
execfile('helloworld.py')

Deprecated since 2.6: popen

import os
os.popen('python helloworld.py') # Just run the program
os.popen('python helloworld.py').read() # Also gets you the stdout

With arguments:

os.popen('python helloworld.py arg').read()

Advance usage: subprocess

import subprocess
subprocess.call(['python', 'helloworld.py']) # Just run the program
subprocess.check_output(['python', 'helloworld.py']) # Also gets you the stdout

With arguments:

subprocess.call(['python', 'helloworld.py', 'arg'])

Read the docs for details :-)


Tested with this basic helloworld.py:

import sys
if len(sys.argv) > 1:
    print(sys.argv[1])
Answer from Hugues Fontenelle on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › how to run a script from python shell? i can import it, but can run it :)
r/learnpython on Reddit: How to run a script from python shell? I can import it, but can run it :)
January 20, 2024 -

Title, i can type in python shell

import scripts.myhelloworldscript

and script runs, but i don't think it's right way to do it? (cause if i type same again script is not running again, i have to close and reopen the shell, and type again to make it run 2nd time...)

🌐
pyOpenSci
pyopensci.org › lessons › package-share-code › run-code › execute-script.html
How to Execute a Python Script — pyOpenSci Lessons
The first way to execute a Python script is to use the python command. The python command takes the name of a file and executes it from your shell, like this:
Discussions

executable - run program in Python shell - Stack Overflow
I have a demo file: test.py. In the Windows Console I can run the file with: C:\>test.py How can I execute the file in the Python Shell instead? More on stackoverflow.com
🌐 stackoverflow.com
Shell Script: Execute a python program from within a shell script - Stack Overflow
Here I have demonstrated an example to run python script within a shell script. For different purposes you may need to read the output from a shell command, execute both python script and shell command within the same file. More on stackoverflow.com
🌐 stackoverflow.com
How to execute a Python script in the Execute Shell Script action? (for beginners)
I've read the documentation and several forum posts, but couldn't find a simple answer. This is what I run in Terminal and it works: python3 /Users/myname/Documents/Folder_for_python_scripts/sort_text_by_character_count.py How do I execute this in the Execute Shell Script action? More on forum.keyboardmaestro.com
🌐 forum.keyboardmaestro.com
20
0
September 22, 2023
How to I execute a .py file from the interpreter?
I'm assuming that the course is talking about running python3 -i report.py, which will run report.py normally and then drop you into a python interpreter after the program is done running. This python interpreter will have all of your python file's functions, variables, etc so that you can check the value of things. It's an interesting feature for debugging, although I don't use it personally More on reddit.com
🌐 r/learnpython
9
2
June 6, 2021
🌐
freeCodeCamp
freecodecamp.org › news › run-python-script-how-to-execute-python-shell-commands-in-terminal
Run Python Script – How to Execute Python Shell Commands in the Terminal
July 14, 2022 - 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 ...
🌐
Real Python
realpython.com › run-python-scripts
How to Run Your Python Scripts and Code – Real Python
2 weeks ago - Running Python scripts is essential for executing your code. You can run Python scripts from the command line using python script.py, directly by making files executable with shebangs on Unix systems, or through IDEs and code editors.
🌐
Hackr
hackr.io › home › articles › programming
How to Run a Python Script via a File or the Shell
January 30, 2025 - Step #04 – Now, go to the Run tab and then click on the Run Module option. Alternatively, you can press F5 to execute the Python script instantly · Step #05 – Before displaying the output, you will be prompted with a dialog “Source Must ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-execute-a-python-file-in-python-shell
How to execute a Python file in Python shell?
August 3, 2023 - import subprocess result = subprocess.run(["python", "hello.py"], capture_output=True, text=True) print(result.stdout) Hello from the script!
Find elsewhere
🌐
Ethz
iacweb.ethz.ch › staff › mathause › ip_python › intro › 05_running-python.html
Running Python — Integrated Practical: Python in Geosciences documentation
The output from the script is shown on the command-line terminal: ... Note that only the command with print is shown. IPython is a feature-rich interactive shell built on top of the basic Python Shell that we discussed in the previous section. IPython will run your Python code just like the ...
🌐
Thomas Stringer
trstringer.com › python-in-shell-script
Run Python Code in a Shell Script | Thomas Stringer
January 31, 2021 - There are a couple of ways to do multiline strings in a shell script, and this one of using here-documents (<<) for string redirection is the cleanest looking one and easiest to remember. Since I use spaces and not tabs, I’m not able to take advantage of <<- removing leading tabs, so I need to be careful with my indentation. Let’s say we had our Python code indented in the shell script.
🌐
The Knowledge Academy
theknowledgeacademy.com › blog › how-to-run-python-scripts
How to Run Python Scripts: A Step-by-Step Guide
February 6, 2024 - Here's a detailed guide on how to run Python scripts in interactive mode: ... Open Command Prompt or PowerShell on Windows, or Terminal on macOS/Linux. ... Type python or python3, depending on your system, and press Enter. ... Upon entering ...
Address: Reflex, Bracknell
🌐
Coding Temple
codingtemple.com › blog › how-to-run-a-python-script
How to Run a Python Script & Execute Python Code
September 23, 2026 - On a Mac or Linux you can also make a script run on its own: add #!/usr/bin/env python3 as the first line, run chmod +x script.py once, then start it with ./script.py. “‘python’ is not recognized as an internal or external command” (Windows): Python is not on your PATH. Use py instead, or reinstall from python.org and tick “Add python.exe to PATH”.
🌐
LabEx
labex.io › tutorials › python-how-to-execute-python-script-in-interactive-shell-425436
How to execute Python script in interactive shell | LabEx
## Add shebang line #!/usr/bin/env python3 ## Make script executable chmod +x script.py ./script.py · ## Conditional script execution if __name__ == '__main__': ## Code to run only when script is primary main()
🌐
Stack Overflow
stackoverflow.com › questions › 7420937 › run-program-in-python-shell
executable - run program in Python shell - Stack Overflow
I have a demo file: test.py. In the Windows Console I can run the file with: C:\>test.py How can I execute the file in the Python Shell instead?
🌐
YouTube
youtube.com › automate with rakesh
#3 Python Tutorial for Beginners | Know Python Shell and Use Python Shell to Run Python Script File - YouTube
🔥 Subscribe for Tutorial Videos: https://bit.ly/34OgvpTMany learners want to know how to use Python Shell to run a Pyhon File or Python Script File (.py fi...
Published: April 1, 2022
Views: 10K
🌐
Educative
educative.io › answers › how-to-run-a-python-script-in-linux
How to run a Python script in Linux
Simply, enter a line of code in python and press enter, the shell then executes the written line of code. Now, we can write and run python codes directly from the python shell.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-run-a-python-script
How to Run a Python Script - GeeksforGeeks
Open Command Prompt or Terminal, type python, and press Enter. ... The print() statement is executed immediately. The result is displayed in the terminal. Run the following lines one by one in the interactive mode.
Published: September 22, 2026
🌐
GitHub
github.com › extrabacon › python-shell
GitHub - extrabacon/python-shell: Run Python scripts from Node.js with simple (but efficient) inter-process communication through stdio · GitHub
import { PythonShell } from 'python-shell'; let options = { mode: 'text', pythonPath: 'path/to/python', scriptPath: 'path/to/my/scripts', args: ['value1', 'value2', 'value3'], }; PythonShell.run('my_script.py', options).then((messages) => { ...
🌐
Python documentation
docs.python.org › 3 › tutorial › interpreter.html
2. Using the Python Interpreter — Python 3.15.2 documentation
Since Python statements often contain spaces or other characters that are special to the shell, it is usually advised to quote command in its entirety. Some Python modules are also useful as scripts. These can be invoked using python -m module [arg] ..., which executes the source file for module as if you had spelled out its full name on the command line. When a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards.
🌐
Keyboard Maestro
forum.keyboardmaestro.com › questions & suggestions
How to execute a Python script in the Execute Shell Script action? (for beginners) - Questions & Suggestions - Keyboard Maestro Discourse
September 22, 2023 - I've read the documentation and several forum posts, but couldn't find a simple answer. This is what I run in Terminal and it works: python3 /Users/myname/Documents/Folder_for_python_scripts/sort_text_by_character_coun…