python-3.6

How to resolve “ValueError: attempted relative import beyond top-level package”

左心房为你撑大大i 提交于 2019-11-29 12:52:45
I have the following problem with my project, help me please! Here is the structure of my package: /pkg /pkg/__init__.py /pkg/sub1/__init__.py /pkg/sub2/__init__.py /pkg/sub1/foo1.py /pkg/sub2/foo2.py Here is implementation of foo1.py: from ..sub2 import foo2 def f(): print("Hello!") When I run foo1 I get error: ValueError: attempted relative import beyond top-level package . I can solve it doing the following adjustment: import sys import os sys.path.append(os.path.abspath(os.path.pardir)) from sub2 import foo2 def f(): print("Hello!") But I wonder if there is a way to do it without importing

stdout redirect from Jupyter notebook is landing in the terminal

独自空忆成欢 提交于 2019-11-29 11:55:01
So I was trying to redirect stdout for a some of the cells in my Jupyter Notebook to a file with this and then cancel it with this for the rest of the cells. The output from the first set of cells landed in the file like it was meant to. The second set of cells after the cancel command sys.stdout = sys.__stdout__ was giving no output, appearing to do nothing, but I later realised it was landing in the terminal where the notebook was launched. It works perfectly in the Python interpreter with exactly the same Python: (miniconda3-latest) cardamom@pegasus:~/Documents/project1 $ python Python 3.6

repeating the rows of a data frame

蓝咒 提交于 2019-11-29 10:55:19
I'm trying repeat the rows of a dataframe. Here's my original data: pd.DataFrame([ {'col1': 1, 'col2': 11, 'col3': [1, 2] }, {'col1': 2, 'col2': 22, 'col3': [1, 2, 3] }, {'col1': 3, 'col2': 33, 'col3': [1] }, {'col1': 4, 'col2': 44, 'col3': [1, 2, 3, 4] }, ]) which gives me col1 col2 col3 0 1 11 [1, 2] 1 2 22 [1, 2, 3] 2 3 33 [1] 3 4 44 [1, 2, 3, 4] I'd like to repeat the rows depending on the length of the array in col3 i.e. I'd like to get a dataframe like this one. col1 col2 0 1 11 1 1 11 2 2 22 3 2 22 4 2 22 5 3 33 6 4 44 7 4 44 8 4 44 9 4 44 What's a good way accomplishing this? You can

ModuleNotFoundError in tracebacks with Python3.6 on linux

我是研究僧i 提交于 2019-11-29 06:03:13
I installed Python 3.6 on Ubuntu 16.04 by using Jonathon Fernyhough's PPA : sudo add-apt-repository ppa:jonathonf/python-3.6 sudo apt-get update sudo apt-get install python3.6 I made a string, using the new literal string interpolation, but I supplied an invalid format specifier. I not only got the expected ValueError: Invalid format specifier , but also the unexpected ModuleNotFoundError: No module named 'apt_pkg' . $ python3.6 Python 3.6.0 (default, Dec 29 2016, 21:40:36) [GCC 5.4.1 20161202] on linux Type "help", "copyright", "credits" or "license" for more information. >>> value = 4 * 20 >

How to set a window icon with PyQt5?

送分小仙女□ 提交于 2019-11-29 03:45:33
问题 from PyQt5 import QtWidgets, QtGui from PyQt5.QtWidgets import * from PyQt5.QtCore import * class Application(QMainWindow): def __init__(self): super(Application, self).__init__() self.setWindowIcon(QtGui.QIcon('icon.png')) I am trying to set a window icon (top left of the window) but the normal icon disappeared instead. I tried with many icon resolutions (8x8, 16x16, 32x32, 64x64) and extensions (.png and .ico). What am I doing wrong? 回答1: The answer has been given by the asker (invisible

Update pip3 for Python 3.6?

大兔子大兔子 提交于 2019-11-29 03:26:29
问题 I just upgraded from Python 3.4 to Python 3.6 using Homebrew. The output states: Pip, setuptools, and wheel have been installed. To update them pip3 install --upgrade pip setuptools wheel You can install Python packages with pip3 install <package> They will install into the site-package directory /usr/local/lib/python3.6/site-packages I tried to run pip3 install --upgrade pip setuptools wheel But it does not upgrade pip for Python 3.6. Instead it finds pip3 in in Python 3.4 and says

Python 3.5 vs. 3.6 what made “map” slower compared to comprehensions

匆匆过客 提交于 2019-11-29 02:56:13
问题 I sometimes used map if there was a function/method that was written in C to get a bit extra performance. However I recently revisited some of my benchmarks and noticed that the relative performance (compared to a similar list comprehension) drastically changed between Python 3.5 and 3.6. That's not the actual code but just a minimal sample that illustrates the difference: import random lst = [random.randint(0, 10) for _ in range(100000)] assert list(map((5).__lt__, lst)) == [5 < i for i in

How to escape f-strings in python 3.6? [duplicate]

人走茶凉 提交于 2019-11-29 02:51:27
This question already has an answer here: How can I print literal curly-brace characters in python string and also use .format on it? 10 answers I have a string in which I would like curly-brackets, but also take advantage of the f-strings feature. Is there some syntax that works for this? Here are two ways it does not work. I would like to include the literal text " {bar} " as part of the string. foo = "test" fstring = f"{foo} {bar}" NameError: name 'bar' is not defined fstring = f"{foo} \{bar\}" SyntaxError: f-string expression part cannot include a backslash Desired result: 'test {bar}'

How to use newline '\n' in f-string to format output in Python 3.6?

痞子三分冷 提交于 2019-11-28 23:32:22
问题 I would like to know how to format this case in a Pythonic way with f-strings: names = ['Adam', 'Bob', 'Cyril'] text = f"Winners are:\n{'\n'.join(names)}" print(text) The problem is that '\' cannot be used inside the {...} expression portions of an f-string. Expected output: Winners are: Adam Bob Cyril 回答1: You can't. Backslashes cannot appear inside the curly braces {} ; doing so results in a SyntaxError : >>> f'{\}' SyntaxError: f-string expression part cannot include a backslash This is

what does --enable-optimizations do while compiling python?

做~自己de王妃 提交于 2019-11-28 20:50:05
问题 I'm trying to compile Python 3.6 on an arm based Linux machine, ./configure outputs this: If you want a release build with all optimizations active (LTO, PGO, etc), please run ./configure --enable-optimizations . what does --enable-optimizations do? 回答1: This flag enables Profile guided optimization (PGO) and Link Time Optimization (LTO). Both are expensive optimizations that slow down the build process but yield a significant speed boost (around 10-20% from what I remember reading). The