Calling Python from bash: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 18: Line 18:


==External Module==
==External Module==
Given an external module <code>my_module.py</code> with the following content:
<syntaxhighlight lang='python'>
def my_function(arg1, arg2, arg3):
    print('this is my_function(' + arg1 + ", " + arg2 + ", " + arg3 + ")")
</syntaxhighlight>


=Using the Interpreter from the a Virtual Environment=
=Using the Interpreter from the a Virtual Environment=

Revision as of 23:56, 15 February 2022

Internal

Overview

Inline Python Code

Use bash here-doc:

  python3 <<EOF
print('blah')
EOF

Also see:

Python Script

Code in External Script

External Script

External Module

Given an external module my_module.py with the following content:

def my_function(arg1, arg2, arg3):
    print('this is my_function(' + arg1 + ", " + arg2 + ", " + arg3 + ")")

Using the Interpreter from the a Virtual Environment

If you want to use the interpreter from a specific virtual environment instead of the interpreter found in PATH, explicitly use the path to the binary from the virtual environment directory:

$(dirname $0)/venv/bin/python ...