python-3.3

Multilpe python versions and interpreters

江枫思渺然 提交于 2019-11-28 09:13:15
问题 I am trying to setup python 2.7.6 on my windows 7 machine (python 3.3.3 was installed first). When selecting the project interpreter after adding it in settings-project interpreter, I get this error File "C:\Python33\Lib\site.py", line 173 file=sys.stderr) ^ SyntaxError: invalid syntax When initially adding the python 2.7 interpreter in the settings page (as compared to adding the virtual environment above), it gives this error message Cannot setup python SDK at C: .... . The SDK seems

subprocess stdin buffer not flushing on newline with bufsize=1

余生长醉 提交于 2019-11-28 08:31:59
问题 I have two small python files, the first reads a line using input and then prints another line a = input() print('complete') The second attempts to run this as a subprocess import subprocess proc = subprocess.Popen('./simp.py', stdout=subprocess.PIPE, stdin=subprocess.PIPE, bufsize=1) print('writing') proc.stdin.write(b'hey\n') print('reading') proc.stdout.readline() The above script will print "writing" then "reading" but then hang. At first I thought this was a stdout buffering issue, so I

How to convert some character into five digit unicode one in Python 3.3?

喜夏-厌秋 提交于 2019-11-28 08:27:21
问题 I'd like to convert some character into five digit unicode on in Python 3.3. For example, import re print(re.sub('a', u'\u1D15D', 'abc' )) but the result is different from what I expected. Do I have to put the character itself, not codepoint? Is there a better way to handle five digit unicode characters? 回答1: Python unicode escapes either are 4 hex digits (\uabcd) or 8 (\Uabcdabcd); for a codepoint beyond U+FFFF you need to use the latter (a capital U), make sure to left-fill with enough

Using Pillow with Python 3

心已入冬 提交于 2019-11-28 07:33:31
I'm not having much luck using Pillow with Python 3.3.2 and I'd be grateful for some help. My problem is that after installing Pillow, I can't import Image. My setup: I've got Linux Mint 16 installed (on an HP Pavilion dv7 laptop). I've got Python 3.3.2+ installed, and it's working fine. I've got Python 2.7.5+ installed, and it's working fine. What I did: I followed the instructions at http://pillow.readthedocs.org/en/latest/index.html to install Pillow v2.4.0 (PIL fork): I started with: ~$ pip install Pillow I installed python-setuptools with: ~$ sudo apt-get install python-dev python

Is python package virtualenv necessary when I use python 3.3?

允我心安 提交于 2019-11-28 07:12:12
I was looking in Cristoph Gohlke's python packages and I noticed that there is a package Virtualenv for Python 3.3. Since there is a package venv in the standard python library v3.3, I was wondering if there is an advantage to install this package separately. Edit: From the documentation of both packages, virtualenv 1.8.2 and venv I can say that the venv standard library package lacks the functionality of: --no-site-packages option choice between setuptools or distribute inability to install pip, since it is not available in the default python installation no customization of prompt prefix

Python blockless subproccess input with constant output on Windows

被刻印的时光 ゝ 提交于 2019-11-28 06:40:36
问题 I am trying to run a command with subproccess and the _thread modules. The subproccess has a stream of output. To combat this I used two threads, one constantly prints new lines and the other is checking for input. When I pass the subproccess input through proc.stdin.write('Some string') it returns 1 and then I get no output. Communicate doesn't work as per most other questions I have read because it blocks waiting for the EOF although it does print the first line of the whatever was going to

How to use 2to3 properly for python?

こ雲淡風輕ζ 提交于 2019-11-28 06:17:04
I have some code in python 2.7 and I want to convert it all into python 3.3 code. I know 2to3 can be used but I am not sure exactly how to use it. Thanks for any help As it is written on 2to3 docs , to translate an entire project from one directory tree to another, use: $ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode If you don't have 2to3 on your path, you can directly invoke lib2to3 : python -m lib2to3 directory\file.py And as the docs (and other answers) mention, you can use some flags for more customization: the -w flag to enable writeback, which applies the changes

Given a method, how do I return the class it belongs to in Python 3.3 onward?

心已入冬 提交于 2019-11-28 03:37:38
问题 Given x = C.f after: class C: def f(self): pass What do I call on x that will return C ? The best I could do is exec ing a parsed portion of x.__qualname__ , which is ugly: exec('d = ' + ".".join(x.__qualname__.split('.')[:-1])) For a use case, imagine that I want a decorator that adds a super call to any method it's applied to. How can that decorator, which is only given the function object, get the class to super (the ??? below)? def ensure_finished(iterator): try: next(iterator) except

'str' object has no attribute 'decode' in Python3

我与影子孤独终老i 提交于 2019-11-27 23:10:07
I've some problem with "decode" method in python 3.3.4. This is my code: for lines in open('file','r'): decodedLine = lines.decode('ISO-8859-1') line = decodedLine.split('\t') But I can't decode the line for this problem: AttributeError: 'str' object has no attribute 'decode' Do you have any ideas? Thanks One encodes strings, and one decodes bytes. You should read bytes from the file and decode them: for lines in open('file','rb'): decodedLine = lines.decode('ISO-8859-1') line = decodedLine.split('\t') Luckily open has an encoding argument which makes this easy: for decodedLine in open('file',

How to choose a random line from a text file

别来无恙 提交于 2019-11-27 18:08:32
问题 I am trying to make a lottery program for my school (we have an economic system). My program generates numbers and saves it off into a text file. When I want to "pull" numbers out of my generator I want it to ensure that there is a winner. Q: How do I have Python select a random line out of my text file and give my output as that number? 回答1: How do I have python select a random line out of my text file and give my output as that number? Assuming the file is relatively small, the following is