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 OverflowTitle, 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...)
executable - run program in Python shell - Stack Overflow
Shell Script: Execute a python program from within a shell script - Stack Overflow
How to execute a Python script in the Execute Shell Script action? (for beginners)
How to I execute a .py file from the interpreter?
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])
You can use this in python3:
exec(open(filename).read())
Just make sure the python executable is in your PATH environment variable then add in your script
Use with AIpython path/to/the/python_script.py
Details:
- In the file job.sh, put this
Use with AI#!/bin/sh
python python_script.py
- Execute this command to make the script runnable for you :
chmod u+x job.sh - Run it :
./job.sh
Method 1 - Create a shell script:
Suppose you have a python file hello.py
Create a file called job.sh that contains
Use with AI#!/bin/bash
python hello.py
mark it executable using
Use with AI$ chmod +x job.sh
then run it
Use with AI$ ./job.sh
Method 2 (BETTER) - Make the python itself run from shell:
Modify your script hello.py and add this as the first line
Use with AI#!/usr/bin/env python
mark it executable using
Use with AI$ chmod +x hello.py
then run it
Use with AI$ ./hello.py