在使用py.test时,我有一些测试可以正常运行SQLite但在我切换到Postgresql时静默挂起.我该如何调试这样的东西?是否有"详细"模式我可以运行我的测试,或设置断点?更一般地说,当pytest无声地停止时,标准的攻击计划是什么?我已经尝试使用pytest-timeout,并使用$ py.test --timeout = 300运行测试,但测试仍然挂起,屏幕上没有任何活动
我正在使用vim编辑器作为python IDE.下面是一个简单的python程序来计算数字的平方根:
import cmath
def sqrt():
try:
num = int(input("Enter the number : "))
if num >= 0:
main(num)
else:
complex(num)
except:
print("OOPS..!!Something went wrong, try again")
sqrt()
return
def main(num):
squareRoot = num**(1/2)
print("The square Root of ", num, " is ", squareRoot)
return
def complex(num):
ans = cmath.sqrt(num)
print("The Square root if ", num, " is ", ans)
return
sqrt()
Run Code Online (Sandbox Code Playgroud)
警告是:
1-square-root.py|2 col 1 C| E302 expected 2 blank lines, found 0 [pep8]
1-square-root.py|15 col 1 C| E302 …
Run Code Online (Sandbox Code Playgroud) joblib文档包含以下警告:
在Windows下,保护代码的主循环非常重要,以避免在使用joblib.Parallel时递归生成子进程.换句话说,您应该编写如下代码:
Run Code Online (Sandbox Code Playgroud)import .... def function1(...): ... def function2(...): ... ... if __name__ == '__main__': # do stuff with imports and functions defined about ...
没有代码应该在"if __name__ =='__ main__'"块之外运行,只有导入和定义.
最初,我认为这只是为了防止偶然的奇怪情况,其中函数传递给joblib.Parallel
递归调用模块,这意味着它通常是良好的做法,但通常是不必要的.但是,对我而言,为什么这只会对Windows造成风险是没有意义的.此外,这个答案似乎表明,无法保护主循环导致代码运行速度比非常简单的非递归问题慢几倍.
出于好奇,我从joblib文档中运行了一个非常简单的尴尬并行循环示例,而没有保护Windows框中的主循环.我的终端被垃圾邮件发送以下错误,直到我关闭它:
ImportError: [joblib] Attempting to do parallel computing without protecting your import on a system that does not suppo
rt forking. To use parallel-computing in a script, you must protect you main loop using "if __name__ == '__main__'". Ple
ase see the joblib documentation on Parallel …
Run Code Online (Sandbox Code Playgroud) 我有简单的python脚本'first.py':
#first.py
def firstFunctionEver() :
print "hello"
firstFunctionEver()
Run Code Online (Sandbox Code Playgroud)
我想使用以下方法调用此脚本:python first.py
并让它调用firstFunctionEver()
.但是,脚本是丑陋的 - 我可以将调用放入哪个函数firstFunctionEver()
并在加载脚本时运行它?
所以我启动了pyscripter,我得到了一个文件:
def main():
pass
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
那是什么?为什么我的程序在没有它的情况下工作?无论如何,这是什么目的?我的代码会在哪里?让我们说一个打印你好世界的功能.那会怎么样?我在哪里打电话给它?
我试图定义一个函数来制作一个矩形的周长.这是代码:
width = input()
height = input()
def rectanglePerimeter(width, height):
return ((width + height)*2)
print(rectanglePerimeter(width, height))
Run Code Online (Sandbox Code Playgroud)
我想我没有留下任何论据或类似的东西.
我偶尔会注意到Python脚本中的以下内容:
if __name__ == "__main__":
# do stuff like call main()
Run Code Online (Sandbox Code Playgroud)
这有什么意义?
我在一个名为'functions.ipynb'的文件中定义了一个hello world函数.现在,我想使用"导入函数"在另一个文件中导入函数.我确信它们在同一个文件夹中.但是,它仍然显示"ImportError:没有模块命名函数".顺便说一下,我正在使用jupyter笔记本.非常感谢!
printbob.py:
import sys
for arg in sys.argv:
print arg
Run Code Online (Sandbox Code Playgroud)
getbob.py
import subprocess
#printbob.py will always be in root of getbob.py
#a sample of sending commands to printbob.py is:
#printboby.py arg1 arg2 arg3 (commands are seperated by spaces)
print subprocess.Popen(['printbob.py', 'arg1 arg2 arg3 arg4']).wait()
x = raw_input('done')
Run Code Online (Sandbox Code Playgroud)
我明白了:
File "C:\Python27\lib\subprocess.py", line 672, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 882, in _execute_child
startupinfo)
WindowsError: [Error 193] %1 is not a valid Win32 application
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?我只想在另一个python脚本中获取另一个python脚本的输出.我是否需要调用cmd.exe或者我可以运行printbob.py并向其发送命令吗?
以下方式允许我启动 Flask 服务器。
选项1:
set FLASK_APP = app.py
flask run
Run Code Online (Sandbox Code Playgroud)
选项 2:
set FLASK_APP = app.py
python -m flask run
Run Code Online (Sandbox Code Playgroud)
选项 3:
python app.py
Run Code Online (Sandbox Code Playgroud)
使用这两者有什么区别?