2017-11-04 57 views
1

运行应用程序时,当与gunicorn新贵运行我的应用程序,我得到:python3错误与开放的CSV文件,gunicorn

TypeError: 'newline' is an invalid keyword argument for this function

当我在命令行中运行它,但是,我没有问题。

我见过解决方案,表明newline应该在文件打开,而不是csv.writer。正如你所看到的,我确实在文件打开时已经有了它。

要重新创建:

  1. 保存my_app.py到/ home/- 你的家 -/
  2. chmod u+x /home/--your home--/my_app.py
  3. 保存my_upstart.conf到/ etc /初始化/
  4. 编辑my_upstart.conf更换与你的家庭目录
  5. sudo service my_upstart start
  6. curl localhost:5001/vis -H “内容类型:文本/ CSV”
  7. 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) 

提到TypeErrormy_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 
+0

我无法重现您的问题。请[edit]包含[mcve]。什么是完整的追溯? – davidism

+0

@davidism我已经更新了操作步骤,重新创建问题。谢谢! – dbconfession

回答

0

gunicorn使用Python 2和它的对应的分发包,wheras我使用蟒3.按照这些步骤来解决:

  1. sudo pip3 install gunicorn
  2. /usr/bin/gunicorn
    • 编辑的第一线阅读#!/usr/bin/python3(而不是python)和
    • 改变了gunicorn版本的任何地方它似乎匹配什么gunicorn --version说。
1

比较Python版本2和3的open的文档,您会注意到可以传递什么参数的差异很大。特别是参数newline在Python 2中不可用。

所以我的猜测是,当gunicorn运行时它会选择一个版本2的Python可执行文件。

有关更多详细信息,请参见Cannot get gunicorn to use Python 3