Calling Python from bash: Difference between revisions
Jump to navigation
Jump to search
Line 20: | Line 20: | ||
└─ <wrapper-bash-script> | └─ <wrapper-bash-script> | ||
</font> | </font> | ||
To maintain this subtree as part of a larger Python project whose <code>.gitignore</code> ignores virtual environment directories, add this to the subtree's <code>.gitignore</code>: | |||
<syntaxhighlight lang='text'> | |||
!venv | |||
</syntaxhighlight> | |||
<font color=darkkhaki>TODO: reconcile with [[Python Project Layout]]</font>. | <font color=darkkhaki>TODO: reconcile with [[Python Project Layout]]</font>. | ||
Revision as of 18:04, 2 May 2023
Internal
Overview
This article summarizes a series of aspects related to invoking Python program from bash.
Running a Python Program with a Bash Wrapper
Check out these links for the meaning of standalone Python program and Python script.
Python Source Code and Tests Maintained within the Project Structure
. ├─ src ├─ tests ├─ venv ├─ requirements.txt └─ <wrapper-bash-script>
To maintain this subtree as part of a larger Python project whose .gitignore
ignores virtual environment directories, add this to the subtree's .gitignore
:
!venv
TODO: reconcile with Python Project Layout.
Only Externally Published Packages
. ├─ venv ├─ requirements.txt └─ <wrapper-bash-script>
Using the Interpreter from the a Virtual Environment
TODO
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 ...
Bash Wrapper that Bootstraps a Virtual Environment
function init_venv() {
[[ -d $(dirname $)/venv ]] && return 0
info "initializing venv ..."
python3 --version 1>/dev/null 2>&1 || fail "python3 not in PATH"
python3 -m venv "$(dirname $)/venv"
}
Also see:
Inline Python Code
Use bash here-doc:
python3 <<EOF
print('blah')
EOF
Also see: