Questions tagged [python-3.4]
The version of the Python programming language released on March 16, 2014. For issues that are specific to Python 3.4. Use the more generic [python] and [python-3.x] tags where possible.
python-3.4
2,585
questions
358
votes
7
answers
120k
views
Python type hinting without cyclic imports
I'm trying to split my huge class into two; well, basically into the "main" class and a mixin with additional functions, like so:
main.py file:
import mymixin.py
class Main(object, MyMixin):...
318
votes
19
answers
338k
views
Python3 project remove __pycache__ folders and .pyc files
What is the BEST way to clear out all the __pycache__ folders and .pyc/.pyo files from a python3 project. I have seen multiple users suggest the pyclean script bundled with Debian, but this does not ...
307
votes
23
answers
469k
views
Python3: ImportError: No module named '_ctypes' when using Value from module multiprocessing
I am using Ubuntu and have installed Python 2.7.5 and 3.4.0. In Python 2.7.5 I am able to successfully assign a variable x = Value('i', 2), but not in 3.4.0. I am getting:
Traceback (most recent call ...
263
votes
5
answers
336k
views
Getting value of enum on string conversion
I have the following enum defined:
from enum import Enum
class D(Enum):
x = 1
y = 2
print(D.x)
now the printed value is
D.x
instead, I wanted the enum's value to be print
1
What can be ...
211
votes
8
answers
212k
views
How could I use requests in asyncio?
I want to do parallel http request tasks in asyncio, but I find that python-requests would block the event loop of asyncio. I've found aiohttp but it couldn't provide the service of http request using ...
112
votes
10
answers
194k
views
How to install pip in CentOS 7?
CentOS 7 EPEL now includes Python 3.4: yum install python34
However, when I try that, even though Python 3.4 installs successfully, it doesn't appear to install pip. Which is weird, because pip ...
98
votes
11
answers
485k
views
No module named 'openpyxl' - Python 3.4 - Ubuntu
I installed openpyxl with
$ pip install openpyxl
when I try the command
from openpyxl import Workbook
I get
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
...
97
votes
3
answers
112k
views
How to properly create and run concurrent tasks using python's asyncio module?
I am trying to properly understand and implement two concurrently running Task objects using Python 3's relatively new asyncio module.
In a nutshell, asyncio seems designed to handle asynchronous ...
85
votes
4
answers
121k
views
Getting values from functions that run as asyncio tasks
I was trying the following code:
import asyncio
@asyncio.coroutine
def func_normal():
print("A")
yield from asyncio.sleep(5)
print("B")
return 'saad'
@asyncio....
82
votes
14
answers
255k
views
OSError: [WinError 193] %1 is not a valid Win32 application
I am trying to call a Python file "hello.py" from within the python interpreter with subprocess. But I am unable to resolve this error. [Python 3.4.1].
import subprocess
subprocess.call([...
74
votes
1
answer
30k
views
Python Enum, when and where to use? [closed]
Python 3.4.0 introduced enum, I've read the doc but still don't know the usage of it. From my perspective, enum.Enum is an extended namedtuple type, which may not be true. So these are what I want to ...
69
votes
5
answers
41k
views
Getting Home Directory with pathlib
Looking through the new pathlib module in Python 3.4, I notice that there isn't any simple way to get the user's home directory. The only way I can come up with for getting a user's home directory is ...
69
votes
4
answers
88k
views
Please explain "Task was destroyed but it is pending!" after cancelling tasks
I am learning asyncio with Python 3.4.2 and I use it to continuously listen on an IPC bus, while gbulb listens on the DBus.
I created a function listen_to_ipc_channel_layer that continuously listens ...
66
votes
5
answers
42k
views
What's the correct way to clean up after an interrupted event loop?
I have an event loop that runs some co-routines as part of a command line tool. The user may interrupt the tool with the usual Ctrl + C, at which point I want to clean up properly after the ...
62
votes
3
answers
29k
views
How can I use functools.singledispatch with instance methods?
Python 3.4 added the ability to define function overloading with static methods. This is essentially the example from the documentation:
from functools import singledispatch
class TestClass(object):
...
56
votes
4
answers
73k
views
I have python3.4 but no pip or ensurepip.. is something wrong with my python3.4 version?
I've read in multiple places that python3.4 ships with pip. My OS is Lubuntu 14.04 and the default python version is Python 2.7.6 but in
/usr/bin
it says I have python3.4 installed (when I run ...
55
votes
11
answers
149k
views
Python 3.4.0 with MySQL database
I have installed Python version 3.4.0 and I would like to do a project with MySQL database. I downloaded and tried installing MySQLdb, but it wasn't successful for this version of Python.
Any ...
53
votes
11
answers
65k
views
How to check if a directory contains files using Python 3
I've searched everywhere for this answer but can't find it.
I'm trying to come up with a script that will search for a particular subfolder then check if it contains any files and, if so, write out ...
49
votes
1
answer
42k
views
What unit of time does timeit return?
I don't know how to interpret the output from Python's timeit.timeit() function. My code is as follows:
import timeit
setup = """
import pydash
list_of_objs = [
{},
{'a': 1, 'b': 2, 0: 0},
...
47
votes
16
answers
94k
views
print binary tree level by level in python
I want to print my binary tree in the following manner:
10
6 12
5 7 11 13
I have written code for insertion of nodes but can't able to ...
47
votes
2
answers
101k
views
How do I import from a file in the current directory in Python 3?
In python 2 I can create a module like this:
parent
->module
->__init__.py (init calls 'from file import ClassName')
file.py
->class ClassName(obj)
And this works. In python 3 I ...
46
votes
1
answer
7k
views
Keras not using multiple cores
Based on the famous check_blas.py script, I wrote this one to check that theano can in fact use multiple cores:
import os
os.environ['MKL_NUM_THREADS'] = '8'
os.environ['GOTO_NUM_THREADS'] = '8'
os....
45
votes
2
answers
53k
views
Python 3.4 and 2.7: Cannot install numpy package for python 3.4
I am using Ubuntu 12.04 and want to use python 3.4 side by side with python 2.7.
The installation of python 3.4 worked properly. However, I cannot install the numpy package for python 3 (and as a ...
44
votes
9
answers
10k
views
Subclass `pathlib.Path` fails
I would like to enhance the class pathlib.Path but the simple example above dose not work.
from pathlib import Path
class PPath(Path):
def __init__(self, *args, **kwargs):
super()....
44
votes
3
answers
18k
views
Should 3.4 enums use UPPER_CASE_WITH_UNDERSCORES?
As the documentation says, an enumeration is a set of symbolic names (members) bound to unique, constant values. The PEP8 says that constants are usually named as UPPER_CASE, should I use this ...
38
votes
6
answers
5k
views
Set debugger breakpoint at end of a function without return
I am debugging method f() that has no return in it.
class A(object):
def __init__(self):
self.X = []
def f(self):
for i in range(10):
self.X.append(...
37
votes
4
answers
13k
views
normalize non-existing path using pathlib only
python has recently added the pathlib module (which i like a lot!).
there is just one thing i'm struggling with: is it possible to normalize a path to a file or directory that does not exist? i can ...
35
votes
13
answers
35k
views
Solving install issues with Python 3.4 on Windows
I have recently tried to install Python 3.4 as an alternative installation on my system in an attempt to familiarise myself before migrating code. My main Python installation is 2.7.6.
I tried the 64 ...
35
votes
3
answers
69k
views
lxml.etree.XML ValueError for Unicode string
I'm transforming an xml document with xslt. While doing it with python3 I had this following error. But I don't have any errors with python2
-> % python3 cstm/artefact.py
Traceback (most recent ...
34
votes
4
answers
42k
views
Hide some maybe-no-member Pylint errors
The following Python fragment code gets analyzed by Pylint:
if type(result) is array.array:
read = result.tobytes()
... with the following error for the last line:
E:401,22: Instance of 'int' ...
33
votes
3
answers
11k
views
Why does the asyncio's event loop suppress the KeyboardInterrupt on Windows?
I have this really small test program which does nothing apart from a executing an asyncio event loop:
import asyncio
asyncio.get_event_loop().run_forever()
When I run this program on Linux and ...
33
votes
1
answer
35k
views
Test an object is a subclass of the type of another instance
I have this code:
class Item:
def __init__(self,a):
self.a=a
class Sub(Item):
def __init__(self,a,b):
self.b=b
Item.__init__(self,a)
class SubSub(Sub):
def ...
33
votes
2
answers
4k
views
What is a DynamicClassAttribute and how do I use it?
As of Python 3.4, there is a descriptor called DynamicClassAttribute. The documentation states:
types.DynamicClassAttribute(fget=None, fset=None, fdel=None, doc=None)
Route attribute access on a ...
32
votes
3
answers
10k
views
How to list all exceptions a function could raise in Python 3?
Is there a programmatic way to get a list of all exceptions a function could raise?
I know for example that os.makedirs(path[, mode]) can raise PermissionError (and maybe others), but the ...
32
votes
3
answers
1k
views
cx-freeze, runpy and multiprocessing - multiple paths to failure
This is a bit of a complex one, and may take some of your time.
The basic problem is, that on linux (Ubuntu in my test case) a cx-freeze'd version of my program (Omnitool) is not able to create ...
30
votes
3
answers
23k
views
Compiling Python 3.4 is not copying pip
I have compiled Python 3.4 from the sources on Linux Mint, but for some reason it is not copying pip to its final compiled folder (after the make install).
Any ideas?
30
votes
5
answers
51k
views
System Python conflict between Anaconda and existing Python installation
I've been going with a basic Python3.4 install that I've been installing many modules into for over the past month but have reached a point where pip is coming up short and I'm going to just install ...
30
votes
4
answers
117k
views
How to install pandas for Python 3?
I try to install pandas for Python 3 by executing the following command:
sudo pip3 install pandas
As a result I get this:
Downloading/unpacking pandas
Cannot fetch index base URL https://pypi....
28
votes
5
answers
67k
views
Finding entries containing a substring in a numpy array?
I tried to find entries in an Array containing a substring with np.where and an in condition:
import numpy as np
foo = "aa"
bar = np.array(["aaa", "aab", "aca"])
np.where(foo in bar)
this only ...
28
votes
4
answers
5k
views
setting up environment in virtaulenv using python3 stuck on setuptools, pip, wheel
Running the following:
virtualenv -p python3 venv
gives:
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /specific/a/home/cc/students/...
27
votes
1
answer
27k
views
python import multiple times [duplicate]
I suppose this is a general question so sorry if not posted in the right place.
Say for instance, I have a function a which imports os. If I was to call this function from another file multiple times ...
27
votes
9
answers
62k
views
pyttsx: No module named 'engine'
I'm trying to install TTS package by using this. Everything was okay until I tried to execute the following command:
import pyttsx
I got back this error:
File "/usr/local/lib/python3.4/dist-...
27
votes
1
answer
35k
views
Reload a Module in Python 3.4 [duplicate]
I know this might sound like a really stupid question but whatever. I've made a small script in Python and I've made some changes while in a shell. Normally, on an OS X computer (It's running Python 2....
27
votes
29
answers
112k
views
Making a collatz program automate the boring stuff
I'm trying to write a Collatz program using the guidelines from a project found at the end of chapter 3 of Automate the Boring Stuff with Python. I'm using python 3.4.0. Following is the project ...
27
votes
4
answers
106k
views
How to get text from span tag in BeautifulSoup
I have links looks like this
<div class="systemRequirementsMainBox">
<div class="systemRequirementsRamContent">
<span title="000 Plus Minimum RAM Requirement">1 GB</span> </...
27
votes
2
answers
3k
views
Eval scope in Python 2 vs. 3
I came across bizarre eval behavior in Python 3 - local variables aren't picked up when eval is called in a list comprehension.
def apply_op():
x, y, z = [0.5, 0.25, 0.75]
op = "x,y,z"
...
26
votes
4
answers
63k
views
HTTPResponse object -- JSON object must be str, not 'bytes'
I've been trying to update a small Python library called libpynexmo to work with Python 3.
I've been stuck on this function:
def send_request_json(self, request):
url = request
req = urllib....
26
votes
1
answer
51k
views
Discovering keys using h5py in python3
In python2.7, I can analyze an hdf5 files keys use
$ python
>>> import h5py
>>> f = h5py.File('example.h5', 'r')
>>> f.keys()
[u'some_key']
However, in python3.4, I get ...
26
votes
3
answers
29k
views
How can I construct an enum.Enum from a dictionary of values?
I'd like to generate some types at runtime from a config file. For simplity, let's assume I already have the data loaded as a python dictionary:
color_values = dict(RED = 1, YELLOW = 2, GREEN = 3)
...
26
votes
5
answers
6k
views
Auto register Django auth models using custom admin site
I implemented authentication management using Django auth with the default admin site but then I wanted to use my own AdminSite to rewrite some behaviors:
class OptiAdmin(admin.AdminSite):
...