我想用以下问题设置用户提示:
save_flag is not set to 1; data will not be saved. Press enter to continue.
Run Code Online (Sandbox Code Playgroud)
input()适用于python3但不适用于python2.raw_input()适用于python2但不适用于python3.有没有办法做到这一点,以便代码兼容python 2和python 3?
我有一些python代码使用numpy并已成功运行一年或更长时间.我上周突然遇到以下错误:
/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.py:2507: VisibleDeprecationWarning: `rank` is deprecated; use the `ndim` attribute or function instead. To find the rank of a matrix see `numpy.linalg.matrix_rank`.
VisibleDeprecationWarning)
Run Code Online (Sandbox Code Playgroud)
我在网上找不到多少,但我发现这是因为旧版scipy中的一个错误(虽然我的代码实际上并没有直接使用scipy).我用numpy 1.9.2和scipy 0.15.1升级到python 2.7.9,但是我仍然得到同样的错误.我不确定是什么导致这种情况,或者我如何解决这个问题.
我正在尝试使用 mpi4py 在大型 numpy 数组上并行化一些操作。我目前正在使用numpy.array_split将数组分成块,然后com.scatter将数组发送到不同的核心,然后comm.gather收集生成的数组。一个最小(非)工作示例如下:
import numpy as np
from mpi4py import MPI
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
if rank == 0:
test = np.random.rand(411,48,52,40)
test_chunks = np.array_split(test,size,axis=0)
else:
test_chunks = None
test_chunk = comm.scatter(test_chunks,root=0)
output_chunk = np.zeros([np.shape(test_chunk)[0],128,128,128])
for i in range(0,np.shape(test_chunk)[0],1):
print(i)
output_chunk[i,0:48,0:52,0:40] = test_chunk[i]
outputData = comm.gather(output_chunk,root=0)
if rank == 0:
outputData = np.concatenate(outputData,axis = 0)
Run Code Online (Sandbox Code Playgroud)
运行这给了我错误:
File "test_4d.py", line 23, in <module>
outputData = comm.gather(output_chunk,root=0)
File "Comm.pyx", …Run Code Online (Sandbox Code Playgroud) 我试图使用pip安装pyinstaller(在Ubuntu 16.0.4上):
pip3 install pyinstaller
Collecting pyinstaller
Using cached PyInstaller-3.2.tar.gz
Collecting setuptools (from pyinstaller)
Using cached setuptools-25.1.3-py2.py3-none-any.whl
Building wheels for collected packages: pyinstaller
Running setup.py bdist_wheel for pyinstaller ... done
Stored in directory: /home/.../.cache/pip/wheels/fc/b3/10/006225b1c1baa34750a7b587d3598d47d18114c06b696a8e0e
Successfully built pyinstaller
Installing collected packages: setuptools, pyinstaller
Successfully installed pyinstaller setuptools-20.7.0
You are using pip version 8.1.1, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Run Code Online (Sandbox Code Playgroud)
但是,如果我然后尝试打电话pyinstaller我得到错误pyinstaller: command not found
为什么在pip安装成功时我无法运行pyinstaller.
我有一个tkinter Entry框,用户可以在其中插入目录的路径.或者,用户可以单击按钮以选择目录.如何设置按钮的输出以填充"输入"框?我尝试了以下但dirname不是全局变量,因此无法识别UserFileInput.另外如何绑定输入字段旁边的按钮.
from Tkinter import *
import tkFileDialog
def askdirectory():
dirname = tkFileDialog.askdirectory()
return dirname
def UserFileInput(status,name):
optionFrame = Frame(root)
optionLabel = Label(optionFrame)
optionLabel["text"] = name
optionLabel.pack(side=LEFT)
text = str(dirname) if dirname else status
var = StringVar(root)
var.set(text)
w = Entry(optionFrame, textvariable= var)
w.pack(side = LEFT)
optionFrame.pack()
return w
if __name__ == '__main__':
root = Tk()
dirBut = Button(root, text='askdirectory', command = askdirectory)
dirBut.pack(side = RIGHT)
directory = UserFileInput("", "Directory")
root.mainloop()
Run Code Online (Sandbox Code Playgroud) 我正在使用下拉菜单创建几个小部件.我想在删除之前检查小部件是否存在(来自下拉菜单上的先前选择).我使用以下代码:
self.ndim_options, self.ndim_options_var = self.DropdownMenuCommand(("1","2","3"),'-',"Number of indirect dimensions","-")
def DropdownMenuCommand(self,options,status,name,row):
if row == "-":
row = self.row
optionLabel = tk.Label(self.frame, bg='turquoise')
optionLabel["text"] = name
optionLabel.grid(row=row, column=0, sticky='w')
var = tk.StringVar(self)
var.set(status)
w = tk.OptionMenu(self.frame, var, *options, command = self.setdimensionproperties)
w.config(bg = 'paleturquoise')
w["menu"].config(bg = 'paleturquoise')
w.grid(row=row, column=1)
self.row += 1
return w, var
def setdimensionproperties(self,val):
row = self.RowEnd
if val == "3": #Set parameters for a 4D (3 indirect dimensions)
#Remove any existing weighting functions
if self.weightingFuncNameDim2.winfo_exists():
self.weightingFuncNameDim2.grid_remove()
self.weightingFuncNameDim2, self.weightingFuncNameDim2_var …Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码来创建数组的索引列表.但是,我希望索引以Fortran顺序运行,即内部循环是更快变化的循环.有没有办法在python中实现这一点.目前,我得到的输出是C顺序.
np.transpose(np.nonzero(np.ones([32,30])))
Run Code Online (Sandbox Code Playgroud)
输出:
array([[ 0, 0],
[ 0, 1],
[ 0, 2],
...,
[31, 27],
[31, 28],
[31, 29]])
Run Code Online (Sandbox Code Playgroud)
但是,我需要以下形式的ouptut:
array([[ 0, 0],
[ 1, 0],
[ 2, 0],
...,
[29, 29],
[30, 29],
[31, 29]])
Run Code Online (Sandbox Code Playgroud) 我需要从文本文件中读取复杂数字到numpy数组.我的问题类似于这个使用numpy.savetxt和numpy.loadtxt编写和读取复数,但是,这里的解决方案是改变数据保存的格式.我没有这个奢侈,因为文本文件是由生成的其他我无法改变的软件.文本文件的示例如下:
25 + 0i
8.43818 + -4.94194i
4.46817 + -5.08305i
4.55764 + -3.02201i
2.69138 + -5.43104i
-0.151334 + -4.83717i
1.98336 + -1.3339i
3.59002 + -0.932973i
1.42727 + -0.617317i
1.02005 + -1.14214i
-0.15564 + 2.74564i
Run Code Online (Sandbox Code Playgroud)
我尝试过以下方法:
np.loadtxt('file.txt',delimiter='\n',dtype=np.complex128)
Run Code Online (Sandbox Code Playgroud)
但是我得到错误:
ValueError: complex() arg is a malformed string
Run Code Online (Sandbox Code Playgroud)
我读过的帖子暗示这是+ -某些行中的符号问题,但是,即使+删除了额外的符号,我也会得到相同的错误.
我正在使用 Tkinter 条目小部件来允许用户将文本输入到 GUI。条目小部件具有默认文本,我想通过按一下按钮清除它们。我有以下代码:
from Tkinter import *
def delete_entries(fields):
for field in fields:
field.delete(0,END)
def UserInput(status,name):
optionFrame = Frame(root)
optionLabel = Label(optionFrame)
optionLabel["text"] = name
optionLabel.pack(side=LEFT)
var = StringVar(root)
var.set(status)
w = Entry(optionFrame, textvariable= var)
w.pack(side = LEFT)
optionFrame.pack()
if __name__ == '__main__':
root = Tk()
fields = 'ExperimentNumber', 'OutputFileName', 'npts1', 'block1'
ExperimentNumber = UserInput("1", "Experiment number")
OutputFileName = UserInput("output.txt", "Output file name")
npts1 = UserInput("1000", "Number of points")
block1 = UserInput("8", "Block size")
Delete_button = Button(root, text = …Run Code Online (Sandbox Code Playgroud) 我有一个使用 Qt5 运行的应用程序(通过 PyQt)。我正在使用 miniconda 并且 python 代码已被 cythonized。该代码是在我的本地计算机上设置的,从原始.py文件生成的 cython 代码以及安装在 miniconda 中的相关库和模块,以便代码成功运行。然后我将所有内容发送到另一台机器,编译 cython 代码以提供.so文件并尝试运行它。此时我收到错误:
This application failed to start because it could not find or load the Qt platform plugin "xcb"
in "".
Reinstalling the application may fix this problem.
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
我已经寻找了很长时间寻找可能的解决方案,并且我有以下额外信息:
我已经检查了 ldd 的各种.so文件。似乎都找到了所需的库。
我已经尝试过locate libqxcb.so并且返回:
~/miniconda3/pkgs/qt-5.6.2-3/plugins/platforms/libqxcb.so
&
~/miniconda3/plugins/platforms/libqxcb.so
根据TranslucentCloud 的建议,我已将其复制到目录~/miniconda3/bin/platforms(新创建的)中。
我还包含export QT_DEBUG_PLUGINS=1在调用我的代码的脚本中。这给出了以下输出:
QFactoryLoader::QFactoryLoader() checking directory path "~/miniconda3/bin/platforms" ...
QFactoryLoader::QFactoryLoader() looking at "~/miniconda3/bin/platforms/libqxcb.so" …Run Code Online (Sandbox Code Playgroud) 我使用以下 MWEcomm.Scatterv并将comm.Gatherv其分布在给定数量的内核 ( size)
import numpy as np
from mpi4py import MPI
import matplotlib.pyplot as plt
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
if rank == 0:
test = np.random.rand(411,48,52,40) #Create array of random numbers
outputData = np.zeros(np.shape(test))
split = np.array_split(test,size,axis = 0) #Split input array by the number of available cores
split_sizes = []
for i in range(0,len(split),1):
split_sizes = np.append(split_sizes, len(split[i]))
displacements = np.insert(np.cumsum(split_sizes),0,0)[0:-1]
plt.imshow(test[0,0,:,:])
plt.show()
else:
#Create variables on other …Run Code Online (Sandbox Code Playgroud) 我正在尝试将Julia用于某些线性代数.该文档列出了许多适合处理矩阵的函数.其中一些直接用于运行Julia,例如
julia> ones(2,2)
2×2 Array{Float64,2}:
1.0 1.0
1.0 1.0
Run Code Online (Sandbox Code Playgroud)
而其他人给出一个UndefVarError例子
julia> eye(2,2)
ERROR: UndefVarError: eye not defined
Stacktrace:
[1] top-level scope at none:0
Run Code Online (Sandbox Code Playgroud)
为什么我只能访问线性代数部分列出的一些函数?https://michaelhatherly.github.io/julia-docs/en/latest/stdlib/linalg.html#Base.LinAlg.expm
我也试过导入LinearAlgebra包但这没有什么区别:
julia> using LinearAlgebra
julia> eye(2,2)
ERROR: UndefVarError: eye not defined
Stacktrace:
[1] top-level scope at none:0
Run Code Online (Sandbox Code Playgroud)
事实上,现在有些功能可用,例如dot,根据文档的其他功能也是线性代数库的一部分继续发出错误:
julia> dot
ERROR: UndefVarError: dot not defined
julia> using LinearAlgebra
julia> dot
dot (generic function with 12 methods)
julia> vecdot
ERROR: UndefVarError: vecdot not defined
Run Code Online (Sandbox Code Playgroud)
上述两个功能都Base.LinAlg.dot在文档中列出.
我目前安装的软件包是:
(v1.0) pkg> …Run Code Online (Sandbox Code Playgroud) 我有三个功能;用户选择dirBut一个目录,其输出进入dirname并更新一个Entry框。在第三个函数 中,dataInput用户选择一个文件。我希望打开文件对话框在用户先前选择并由 定义的目录中打开dirname,但是,我不确定如何传递dirname给句柄,以便我可以使用它,因为askopenfilename是askdirectory从按钮调用的。
def UserFileInput(self,status,name):
row = self.row
optionLabel = tk.Label(self)
optionLabel.grid(row=row, column=0, sticky='w')
optionLabel["text"] = name
text = status
var = tk.StringVar(root)
var.set(text)
w = tk.Entry(self, textvariable= var)
w.grid(row=row, column=1, sticky='ew')
self.row += 1
return w, var
def askdirectory(self):
dirname = tkFileDialog.askdirectory()
if dirname:
self.directoryEntry.delete(0, tk.END)
self.directoryEntry.insert(0, dirname)
def askfilename(self):
filename = tkFileDialog.askopenfilename(initialdir=dirname)
if filename:
self.dataInput.delete(0, tk.END)
self.dataInput.insert(0, filename)
currentDirectory = os.getcwd()
directory,var …Run Code Online (Sandbox Code Playgroud) python ×12
numpy ×5
tkinter ×4
arrays ×3
mpi4py ×2
anaconda ×1
input ×1
julia ×1
miniconda ×1
pip ×1
pyinstaller ×1
pyqt5 ×1
python-2.7 ×1
python-2.x ×1
python-3.x ×1
qt ×1
raw-input ×1
scipy ×1