Should I put #! (shebang) in Python scripts, and what form should it take?

Should I put the shebang in my Python scripts? In what form? #!/usr/bin/env python or #!/usr/local/bin/python Are these equally portable? Which form is used most? Note: the tornado project uses the shebang. On the other hand the Django project doesn’t. 15 s 15 The shebang line in any script determines the script’s ability to be … Read more

Relative imports in Python 3

I want to import a function from another file in the same directory. Sometimes it works for me with from .mymodule import myfunction but sometimes I get a: SystemError: Parent module ” not loaded, cannot perform relative import Sometimes it works with from mymodule import myfunction, but sometimes I also get a: SystemError: Parent module … Read more

How can I flush the output of the print function (unbuffer python output)?

How do I force Python’s print function to output to the screen? 1 13 In Python 3, print can take an optional flush argument: print(“Hello, World!”, flush=True) In Python 2 you’ll have to do import sys sys.stdout.flush() after calling print. By default, print prints to sys.stdout (see the documentation for more about file objects).

What is the Python 3 equivalent of “python -m SimpleHTTPServer”

What is the Python 3 equivalent of python -m SimpleHTTPServer? 7 From the docs: The SimpleHTTPServer module has been merged into http.server in Python 3.0. The 2to3 tool will automatically adapt imports when converting your sources to 3.0. So, your command is python -m http.server, or depending on your installation, it can be: python3 -m … Read more