sys

What is the difference between a stack and a frame?

What is the difference between a stack and a frame? Question: Under what situations would I want to use one over the other? What is the difference between: >>> import inspect >>> print(inspect.getouterframes(inspect.currentframe())) [(<frame object at 0x8fc262c>, ‘<stdin>’, 1, ‘<module>’, None, None)] And: >>> import traceback >>> traceback.extract_stack() [(‘<stdin>’, 1, ‘<module>’, None)] Update: Another: >>> …

Total answers: 2

Set LD_LIBRARY_PATH before importing in python

Set LD_LIBRARY_PATH before importing in python Question: Python uses the PYTHONPATH environment-variable to determine in which folders it should look for modules. You can play around with it by modifying sys.path, which works nicely for pure Python-Modules. But when a module uses shared object files or static libraries, it looks for those in LD_LIBRARY_PATH (on …

Total answers: 5

How to extract a file within a folder within a zip?

How to extract a file within a folder within a zip? Question: I need to extract a file called Preview.pdf from a folder called QuickLooks inside of a zip file. Right now my code looks a little like this: with ZipFile(newName, ‘r’) as newName: newName.extract(QuickLooksPreview.pdf) newName.close() (In this case, newName has been set equal to …

Total answers: 1

python: sys is not defined

python: sys is not defined Question: I have a piece of code which is working in Linux, and I am now trying to run it in windows, I import sys but when I use sys.exit(). I get an error, sys is not defined. Here is the begining part of my code try: import numpy as …

Total answers: 4

taking multiline input with sys.stdin

taking multiline input with sys.stdin Question: I have the following function: def getInput(): # define buffer (list of lines) buffer = [] run = True while run: # loop through each line of user input, adding it to buffer for line in sys.stdin.readlines(): if line == ‘quitn’: run = False else: buffer.append(line.replace(‘n’,”)) # return list …

Total answers: 4

Python memory usage of numpy arrays

Python memory usage of numpy arrays Question: I’m using python to analyse some large files and I’m running into memory issues, so I’ve been using sys.getsizeof() to try and keep track of the usage, but it’s behaviour with numpy arrays is bizarre. Here’s an example involving a map of albedos that I’m having to open: …

Total answers: 4

Usage of sys.stdout.flush() method

Usage of sys.stdout.flush() method Question: What does sys.stdout.flush() do? Asked By: I159 || Source Answers: Consider the following simple Python script: import time import sys for i in range(5): print(i), #sys.stdout.flush() time.sleep(1) This is designed to print one number every second for five seconds, but if you run it as it is now (depending on …

Total answers: 7

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

How to finish sys.stdin.readlines() input? Question: This might be a silly question, but as I can’t find an answer, I have to ask it. In interactive python I want to process a message which i get with: >>> message = sys.stdin.readlines() Everything works fine, but… how to stop it from getting an input and make …

Total answers: 6

Capture stdout from a script?

Capture stdout from a script? Question: suppose there is a script doing something like this: # module writer.py import sys def write(): sys.stdout.write(“foobar”) Now suppose I want to capture the output of the write function and store it in a variable for further processing. The naive solution was: # module mymodule.py from writer import write …

Total answers: 11

Import from sibling directory

Import from sibling directory Question: I have a Python class called “ClassA” and another Python class which is supposed to import ClassA which is “ClassB”. The directory structure is as follows: MainDir ../Dir …./DirA/ClassA …./DirB/ClassB How would I use sys.path so that ClassB can use ClassA? Asked By: skylerl || Source Answers: You can use …

Total answers: 3