Run a Python Script in Terminal Without the Python Command

Execute python program from command line without script file

i found a solution, maybe it will help, you can use EOF

$ python << EOF
> def test2():
> print("test2")
>
> def test_func():
> test2()
> print("test_func")
>
> test_func()
> EOF

# output
test2
test_func

You can also use python -c with """

$ python -c """
def test2():
print("test2")

def test_func():
test2()
print("test_func")

test_func()
"""

run a python script in terminal without the python command

You use a shebang line at the start of your script:

#!/usr/bin/env python

make the file executable:

chmod +x arbitraryname

and put it in a directory on your PATH (can be a symlink):

cd ~/bin/
ln -s ~/some/path/to/myscript/arbitraryname

Run Python script without activating virtual environment

Use chmod +x script.py to make your script executable. The #!shebang selects an interpreter.

You can call an executable from the shell like so:

/path/to/script.py

Or:

cd /path/to; ./script.py

Alternatively, you can put your script in one of the directories defined by $PATH, which will let you call it just like any other utility.



Related Topics



Leave a reply



Submit