scripting

Python: Import And Initialize Argparse After if __name__ == '__main__'?

Python: Import And Initialize Argparse After if __name__ == '__main__'? Question: If I am using argparse and an if __name__ == ‘__main__’ test in a script that I would also like to use as a module, should I import argparse under that test and then initialize it? None of the style guides I have found …

Total answers: 2

shebang env preferred python version

shebang env preferred python version Question: I have some python-2.x scripts which I copy between different systems, Debian and Arch linux. Debian install python as ‘/usr/bin/python’ while Arch installs it as ‘/usr/bin/python2’. A problem is that on Arch linux ‘/usr/bin/python’ also exists which refers to python-3.x. So every time I copy a file I have …

Total answers: 5

Read Bash variables into a Python script

Read Bash variables into a Python script Question: I am running a bash script (test.sh) and it loads in environment variables (from env.sh). That works fine, but I am trying to see python can just load in the variables already in the bash script. Yes I know it would probably be easier to just pass …

Total answers: 5

How to return multiple variables from python to bash

How to return multiple variables from python to bash Question: I have a bash script that calls a python script. At first I was just returning one variable and that is fine, but now I was told to return two variables and I was wondering if there is a clean and simple way to return …

Total answers: 2

Purpose of #!/usr/bin/python3 shebang

Purpose of #!/usr/bin/python3 shebang Question: I have noticed this in a couple of scripting languages, but in this example, I am using python. In many tutorials, they would start with #!/usr/bin/python3 on the first line. I don’t understand why we have this. Shouldn’t the operating system know it’s a python script (obviously it’s installed since …

Total answers: 7

List of open browser tabs programmatically

List of open browser tabs programmatically Question: Is there a way to programmatically obtain a list of open tabs in a browser by index? For example, suppose Google Chrome is open with two tabs. In the program, a line something like: tabs_list = list_tabs(hwnd) …where hwnd is the handle to the window for the overall …

Total answers: 3

Batch equivalent of "source" on Windows: how to run a Python script from a virtualenv

Batch equivalent of "source" on Windows: how to run a Python script from a virtualenv Question: I’ve done a fair bit of bash scripting, but very little batch scripting on Windows. I’m trying to activate a Python virtualenv, run a Python script, then deactivate the virtualenv when the script exits. I’ve got a folder called …

Total answers: 3

Get name of current script in Python

Get name of current script in Python Question: I’m trying to get the name of the Python script that is currently running. I have a script called foo.py and I’d like to do something like this in order to get the script name: print(Scriptname) Asked By: SubniC || Source Answers: You can use __file__ to …

Total answers: 14

Loading external script with jinja2 template directive

Loading external script with jinja2 template directive Question: I’m very new to jinja2 and the use of templates in general so I was wondering if there’s an easy way to load an external javascript. I was thinking of using: {% block javascript %} <script src=”myscript.js”></script> {% endblock %} But I can’t help to ask: Is …

Total answers: 2

Python script header

Python script header Question: The typical header should be #!/usr/bin/env python But I found below also works when executing the script like $python ./my_script.py #!/usr/bin/python #!python What’s difference between these 2 headers? What could be the problem for 2nd one? Please also discussing the case for python interpreter is in PATH or not. Thanks. Asked …

Total answers: 6