redirect sys.stdout to specific Jupyter Notebook cell

The documentation for ipywidgets.Output has a section about interacting with output widgets from background threads. Using the Output.append_stdout method there is no need for locking. The final cell in this answer can then be replaced with: def t1_main(): for i in range(10): output1.append_stdout(f’thread1 {i}\n’) time.sleep(0.5) def t2_main(): for i in range(10): output2.append_stdout(f’thread2 {i}\n’) time.sleep(0.5) output1.clear_output() … Read more

What does sys.stdin read?

So you have used Python’s “pre built in functions”, presumably like this: file_object = open(‘filename’) for something in file_object: some stuff here This reads the file by invoking an iterator on the file object which happens to return the next line from the file. You could instead use: file_object = open(‘filename’) lines = file_object.readlines() which … Read more

AttributeError: module ‘sys’ has no attribute ‘maxint’

In python3, sys.maxint changed to sys.maxsize. Here are the values: Python2 >>> sys.maxint 9223372036854775807 Python3 >>> sys.maxsize 9223372036854775807 On the same platform, the values match. The value is typically 2**31 – 1 on a 32-bit platform and 2**63 – 1 on a 64-bit platform. Replacing your call to maxint with maxsize will stop this particular … Read more

How to finish sys.stdin.readlines() input?

For UNIX based systems (Linux, Mac): Hello, you can type : Ctrld Ctrld closes the standard input (stdin) by sending EOF. Example : >>> import sys >>> message = sys.stdin.readlines() Hello World My Name Is James Bond # <ctrl-d> EOF sent >>> print message [‘Hello\n’, ‘World\n’, ‘My\n’, ‘Name\n’, ‘Is\n’, ‘James\n’, ‘Bond\n’] For Windows : To … Read more

Import from sibling directory

as a literal answer to the question ‘Python Import from parent directory‘: to import ‘mymodule’ that is in the parent directory of your current module: import os parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) os.sys.path.insert(0,parentdir) import mymodule edit Unfortunately, the __file__ attribute is not always set. A more secure way to get the parentdir is through the inspect module: … Read more

Python3 UnicodeDecodeError with readlines() method

I think the best answer (in Python 3) is to use the errors= parameter: with open(‘evil_unicode.txt’, ‘r’, errors=”replace”) as f: lines = f.readlines() Proof: >>> s = b’\xe5abc\nline2\nline3′ >>> with open(‘evil_unicode.txt’,’wb’) as f: … f.write(s) … 16 >>> with open(‘evil_unicode.txt’, ‘r’) as f: … lines = f.readlines() … Traceback (most recent call last): File “<stdin>”, … Read more

Set LD_LIBRARY_PATH before importing in python

UPDATE: see the EDIT below. I would use: import os os.environ[‘LD_LIBRARY_PATH’] = os.getcwd() # or whatever path you want This sets the LD_LIBRARY_PATH environment variable for the duration/lifetime of the execution of the current process only. EDIT: it looks like this needs to be set before starting Python: Changing LD_LIBRARY_PATH at runtime for ctypes So … Read more

tech