Questions tagged «python»

Python是一种多范式,动态类型的多用途编程语言。它旨在快速学习,理解和使用并强制使用干净统一的语法。请注意,Python 2自2020年1月1日起已不再受支持。不过,对于特定于版本的Python问题,请添加[python-2.7]或[python-3.x]标签。使用Python变体或库(例如Jython,PyPy,Pandas,Numpy)时,请将其包含在标签中。


3
subprocess.check_output()似乎不存在(Python 2.6.5)
我一直在阅读有关子流程模块的Python文档(请参阅此处),它讨论的subprocess.check_output()命令似乎正是我所需要的。 但是,当我尝试使用它时,出现错误,提示它不存在,并且在运行dir(subprocess)时未列出。 我正在运行Python 2.6.5,下面使用的代码是: import subprocess subprocess.check_output(["ls", "-l", "/dev/null"]) 有谁知道为什么会这样吗?
79 python 

4
如何使用matplotlib显示两个数字?
同时绘制两个图形时出现了一些麻烦,没有在一个图中显示。但是根据文档,我编写了代码,只有图1所示。我想也许我失去了一些重要的东西。有人可以帮我弄清楚吗?谢谢。(代码中使用的* tlist_first *是数据列表。) plt.figure(1) plt.hist(tlist_first, bins=2000000, normed = True, histtype ="step", cumulative = True, color = 'g',label = 'first answer') plt.ylabel('Percentage of answered questions') plt.xlabel('Minutes elapsed after questions are posted') plt.axvline(x = 30, ymin = 0, ymax = 1, color = 'r', linestyle = '--', label = '30 min') plt.axvline(x …

3
未在Flask会话中使用Flask-Session扩展名设置秘密密钥
现在,我正在使用烧瓶第3方库Flask-Session,但没有运气可以正常工作。 当我连接到我的站点时,出现以下错误: RuntimeError:会话不可用,因为未设置任何秘密密钥。将应用程序上的secret_key设置为唯一且秘密的内容。 下面是我的服务器代码。 from flask import Flask, session from flask.ext.session import Session SESSION_TYPE = 'memcache' app = Flask(__name__) sess = Session() nextId = 0 def verifySessionId(): global nextId if not 'userId' in session: session['userId'] = nextId nextId += 1 sessionId = session['userId'] print ("set userid[" + str(session['userId']) + "]") …
79 python  session  flask 

4
如何在Python中使用inspect从被呼叫者获取呼叫者的信息?
我需要从被叫方获取呼叫者信息(什么文件/什么行)。我了解到可以为此目的使用inpect模块,但不能完全使用它。 如何使用inspect获取那些信息?或者还有其他获取信息的方法吗? import inspect print __file__ c=inspect.currentframe() print c.f_lineno def hello(): print inspect.stack ?? what file called me in what line? hello()
79 python  inspect 

1
在Flask应用程序中提交表单时,错误请求错误的原因是什么?
在阅读了许多类似的听起来问题和相关的Flask文档之后,我似乎无法弄清楚提交表单时是什么导致了以下错误: 400错误的要求 浏览器(或代理)发送了该服务器无法理解的请求。 尽管表单始终可以正确显示,但是当我提交与以下两个功能之一关联的HTML表单时,就会发生错误的请求: @app.route('/app/business', methods=['GET', 'POST']) def apply_business(): if request.method == 'POST': new_account = Business(name=request.form['name_field'], email=request.form['email_field'], account_type="business", q1=request.form['q1_field'], q2=request.form['q2_field'], q3=request.form['q3_field'], q4=request.form['q4_field'], q5=request.form['q5_field'], q6=request.form['q6_field'], q7=request.form['q7_field'], account_status="pending", time=datetime.datetime.utcnow()) db.session.add(new_account) db.session.commit() session['name'] = request.form['name_field'] return redirect(url_for('success')) return render_template('application.html', accounttype="business") @app.route('/app/student', methods=['GET', 'POST']) def apply_student(): if request.method == 'POST': new_account = Student(name=request.form['name_field'], email=request.form['email_field'], …

6
Python脚本头
典型的标头应为 #!/usr/bin/env python 但是我发现以下在执行脚本时也可以使用 $python ./my_script.py #!/usr/bin/python #!python 这2个标头之间有什么区别?第二个可能是什么问题?还请讨论python解释器是否在PATH中的情况。谢谢。
79 python  scripting 


16
Python Virtualenv-没有名为virtualenvwrapper.hook_loader的模块
我正在运行Mac OS 10.6.8。并希望除了python 2.6之外还安装python 2.7,并在新的virtualenv中使用python 2.7。我执行了以下步骤: 我下载并安装了python 2.7: http://www.python.org/ftp/python/2.7.3/python-2.7.3-macosx10.6.dmg 然后,我运行命令使用python2.7设置新的virtualenv: mkvirtualenv --python=python2.7 mynewenv 我的.bash_profile如下所示: # needed for virtualenvwrapper export WORKON_HOME=$HOME/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv source /usr/local/bin/virtualenvwrapper.sh # Setting PATH for Python 2.7 # The orginal version is saved in .bash_profile.pysave PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}" export PATH 现在,当我打开控制台时,出现以下错误消息。 ImportError: No module named virtualenvwrapper.hook_loader virtualenvwrapper.sh: …


20
Python AttributeError:“模块”对象没有属性“ SSL_ST_INIT”
我的Python脚本失败了: Traceback (most recent call last): File "./inspect_sheet.py", line 21, in <module> main() File "./inspect_sheet.py", line 12, in main workbook_name=workbook_name, File "./google_sheets.py", line 56, in __init__ self.login() File "./google_sheets.py", line 46, in login self.client = gspread.authorize(credentials) File "/usr/local/lib/python2.7/site-packages/gspread/client.py", line 335, in authorize client.login() File "/usr/local/lib/python2.7/site-packages/gspread/client.py", line 98, in login self.auth.refresh(http) …

5
为什么在Python中遍历range()比使用while循环更快?
前几天,我在做一些Python基准测试,发现了一些有趣的东西。以下是两个执行或多或少相同功能的循环。循环1所需的时间大约是循环2的两倍。 循环1: int i = 0 while i < 100000000: i += 1 循环2: for n in range(0,100000000): pass 为什么第一个循环这么慢?我知道这是一个微不足道的例子,但这激起了我的兴趣。range()函数有什么特别之处,使其比以相同方式递增变量更有效?

3
为什么SQLAlchemy用sqlite插入比直接使用sqlite3慢25倍?
为什么这个简单的测试用例用SQLAlchemy插入100,000行比直接使用sqlite3驱动程序慢25倍?我在现实世界的应用程序中看到过类似的减速情况。难道我做错了什么? #!/usr/bin/env python # Why is SQLAlchemy with SQLite so slow? # Output from this program: # SqlAlchemy: Total time for 100000 records 10.74 secs # sqlite3: Total time for 100000 records 0.40 secs import time import sqlite3 from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String, create_engine from …

8
如何为元组增加价值?
我正在编写一个脚本,其中有一个类似的元组列表('1','2','3','4')。例如: list = [('1','2','3','4'), ('2','3','4','5'), ('3','4','5','6'), ('4','5','6','7')] 现在我需要添加'1234','2345','3456'并'4567'分别为每个元组的结尾。例如: list = [('1','2','3','4','1234'), ('2','3','4','5','2345'), ('3','4','5','6','3456'), ('4','5','6','7','4567')] 有可能吗?
79 python  tuples 

12
为什么pip安装了旧版本的软件包?
我刚刚将软件包的新版本上传到PyPi(1.2.1.0-r4):我可以下载egg文件并使用easy_install进行安装,并且该版本可以正确签出。但是,当我尝试使用pip进行安装时,它将安装版本1.1.0.0。即使我明确指定要使用的版本,也会pip install -Iv tome==1.2.1.0-r4收到以下消息:Requested tome==1.2.1.0-r4, but installing version 1.1.0.0,但我不明白为什么。 我仔细检查parse_version并确认1.2.1上的版本字符串大于1.1.0上的版本字符串,如下所示: >>> from pkg_resources import parse_version as pv >>> pv('1.1.0.0') < pv('1.2.1.0-r4') True >>> 那么,为什么要选择安装1.1.0呢?
79 python  pip  setuptools 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.