Questions tagged [sys]

For questions about the Python module named sys. This tag is also used for questions relating to the Sys namespace - the root namespace for the Microsoft AJAX Library, which contains all fundamental classes and base classes.

1100 questions
226
votes
7 answers

Usage of sys.stdout.flush() method

What does sys.stdout.flush() do?
I159
  • 29,741
  • 31
  • 97
  • 132
225
votes
4 answers

Python memory usage of numpy arrays

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…
EddyTheB
  • 3,100
  • 4
  • 23
  • 32
187
votes
4 answers

Why should we NOT use sys.setdefaultencoding("utf-8") in a py script?

I have seen few py scripts which use this at the top of the script. In what cases one should use it? import sys reload(sys) sys.setdefaultencoding("utf-8")
mlzboy
  • 14,343
  • 23
  • 76
  • 97
134
votes
2 answers

Where is Python's sys.path initialized from?

Where is Python's sys.path initialized from? UPD: Python is adding some paths before refering to PYTHONPATH: >>> import sys >>> from pprint import pprint as p >>> p(sys.path) ['', …
Alex
  • 43,191
  • 44
  • 96
  • 127
121
votes
12 answers

Capture stdout from a script?

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…
Paolo
  • 20,112
  • 21
  • 72
  • 113
95
votes
10 answers

Get parent of current directory from Python script

I want to get the parent of current directory from Python script. For example I launch the script from /home/kristina/desire-directory/scripts the desire path in this case is /home/kristina/desire-directory I know sys.path[0] from sys. But I don't…
Fedorenko Kristina
  • 2,607
  • 2
  • 19
  • 18
70
votes
4 answers

What does sys.intern() do and when should it be used?

I came across this question about memory management of dictionaries, which mentions the intern function. What exactly does it do, and when would it be used? To give an example: if I have a set called seen, that contains tuples in the form…
pufferfish
  • 16,651
  • 15
  • 56
  • 65
61
votes
5 answers

Set LD_LIBRARY_PATH before importing in python

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…
iFreilicht
  • 13,271
  • 9
  • 43
  • 74
59
votes
4 answers

python: sys is not defined

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 np import…
astrochris
  • 1,756
  • 5
  • 20
  • 42
52
votes
2 answers

What is the difference between a stack and a frame?

Under what situations would I want to use one over the other? What is the difference between: >>> import inspect >>> print(inspect.getouterframes(inspect.currentframe())) [(, '', 1, '', None,…
jmunsch
  • 22,771
  • 11
  • 93
  • 114
42
votes
3 answers

Import from sibling directory

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…
skylerl
  • 4,030
  • 12
  • 41
  • 60
42
votes
3 answers

Python3 UnicodeDecodeError with readlines() method

Trying to create a twitter bot that reads lines and posts them. Using Python3 and tweepy, via a virtualenv on my shared server space. This is the part of the code that seems to have trouble: #!/foo/env/bin/python3 import re import tweepy, time,…
r_e_cur
  • 457
  • 1
  • 4
  • 8
39
votes
6 answers

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

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…
Gandi
  • 3,522
  • 2
  • 21
  • 31
36
votes
2 answers

AttributeError: module 'sys' has no attribute 'maxint'

I'm trying to run the following code with Python 3.7: import sys print(sys.maxint) but I get an error: D:\Python3.7\python.exe "D:/PyCharm 2017.2.3/Workplace/maximizer.py" Traceback (most recent call last): File "D:/PyCharm…
Trux
  • 403
  • 1
  • 5
  • 12
35
votes
6 answers

What does sys.stdin read?

I get how to open files, and then use Python's pre built in functions with them. But how does sys.stdin work? for something in sys.stdin: some stuff here lines = sys.stdin.readlines() What's the difference between the above two different uses…
Vimzy
  • 1,871
  • 8
  • 30
  • 56
1
2 3
73 74