python3错误与开放的CSV文件,gunicorn
问题描述:
运行应用程序时,当与gunicorn新贵运行我的应用程序,我得到:python3错误与开放的CSV文件,gunicorn
TypeError: 'newline' is an invalid keyword argument for this function
当我在命令行中运行它,但是,我没有问题。
我见过解决方案,表明newline
应该在文件打开,而不是csv.writer
。正如你所看到的,我确实在文件打开时已经有了它。
要重新创建:
- 保存
my_app.py
到/ home/- 你的家 -/ chmod u+x /home/--your home--/my_app.py
- 保存
my_upstart.conf
到/ etc /初始化/ - 编辑
my_upstart.conf
更换与你的家庭目录 sudo service my_upstart start
-
curl localhost:5001/vis
-H “内容类型:文本/ CSV” sudo cat /var/log/upstart/my_upstart.log
在my_upstart.log
,你会看到上面
my_app.py
#!/usr/bin/python3
from flask import Flask, request
app = Flask(__name__)
@app.route('/vis/', strict_slashes=False)
def vis():
with (open('~/test.csv', mode='w', newline='')) as f:
writer = csv.writer(f)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001)
提到TypeError
my_upstart.conf
description "Gunicorn config file for serving the Wellness app"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid ubuntu
setgid ubuntu
script
cd /home/<your home>/
exec gunicorn --bind 0.0.0.0:5001 my_app:app
end script
答
gunicorn使用Python 2和它的对应的分发包,wheras我使用蟒3.按照这些步骤来解决:
sudo pip3 install gunicorn
在
- ,
- 编辑的第一线阅读
#!/usr/bin/python3
(而不是python
)和 - 改变了gunicorn版本的任何地方它似乎匹配什么
gunicorn --version
说。
- 编辑的第一线阅读
/usr/bin/gunicorn
答
比较Python版本2和3的open
的文档,您会注意到可以传递什么参数的差异很大。特别是参数newline
在Python 2中不可用。
所以我的猜测是,当gunicorn运行时它会选择一个版本2的Python可执行文件。
有关更多详细信息,请参见Cannot get gunicorn to use Python 3。
我无法重现您的问题。请[edit]包含[mcve]。什么是完整的追溯? – davidism
@davidism我已经更新了操作步骤,重新创建问题。谢谢! – dbconfession