【问题标题】:python3 Error with opening csv file when running app with gunicornpython3 使用 gunicorn 运行应用程序时打开 csv 文件时出错
【发布时间】:2017-11-04 03:09:58
【问题描述】:

当我使用 gunicorn upstart 运行我的应用程序时,我得到:

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/init/
  4. 编辑 my_upstart.conf 以替换为您的主目录
  5. sudo service my_upstart start
  6. curl localhost:5001/vis -H "Content-Type: text/csv"
  7. sudo cat /var/log/upstart/my_upstart.log

my_upstart.log,你会看到上面提到的TypeError

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)

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

【问题讨论】:

    标签: python-3.x flask gunicorn


    【解决方案1】:

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

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

    更多详情请见Cannot get gunicorn to use Python 3

    【讨论】:

      【解决方案2】:

      gunicorn 使用的是 python 2 及其对应的分发包,而我使用的是 python 3。按照以下步骤进行修复:

      1. sudo pip3 install gunicorn
      2. /usr/bin/gunicorn,
        • 将第一行编辑为 #!/usr/bin/python3(而不是 python)和
        • 在任何看起来与 gunicorn --version 所说的匹配的地方更改了 gunicorn 版本。

      【讨论】:

        猜你喜欢
        • 2013-06-09
        • 2019-10-25
        • 1970-01-01
        • 1970-01-01
        • 2014-01-18
        • 2018-02-04
        • 2021-05-24
        • 2020-12-17
        • 1970-01-01
        相关资源
        最近更新 更多