Python 3.x 使用gunicorn运行应用程序时打开csv文件时出现python3错误

Python 3.x 使用gunicorn运行应用程序时打开csv文件时出现python3错误,python-3.x,flask,gunicorn,Python 3.x,Flask,Gunicorn,当与gunicorn upstart一起运行我的应用程序时,我会得到: TypeError:“newline”是此函数的无效关键字参数 但是,当我从命令行运行它时,我没有问题 我看到一些解决方案表明,newline应该在文件打开时出现,而不是使用csv.writer。正如你所看到的,我确实在文件的开头有它 重新创建: 将my_app.py保存到/home/--您的家--/ chmodu+x/home/--您的家--/my_app.py 将my_upstart.conf保存到/etc/init/

当与gunicorn upstart一起运行我的应用程序时,我会得到:

TypeError:“newline”是此函数的无效关键字参数

但是,当我从命令行运行它时,我没有问题

我看到一些解决方案表明,
newline
应该在文件打开时出现,而不是使用
csv.writer
。正如你所看到的,我确实在文件的开头有它

重新创建:

  • 将my_app.py保存到/home/--您的家--/
  • chmodu+x/home/--您的家--/my_app.py
  • 将my_upstart.conf保存到/etc/init/
  • 编辑
    my\u upstart.conf
    以替换为您的主目录
  • sudo服务我的upstart启动
  • curl localhost:5001/vis
    -H“内容类型:text/csv”
  • 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
    
    description“为健康应用程序提供服务的Gunicorn配置文件”
    从运行级别开始[2345]
    在运行级别停止[!2345]
    重生
    setuid ubuntu
    setgid ubuntu
    剧本
    cd/主页//
    exec gunicorn--绑定0.0.0.0:5001我的应用程序:应用程序
    结束脚本
    
    比较Python第2版和第3版的
    open
    文档,您会注意到可以传递的参数有很大的不同。尤其是参数
    newline
    在Python 2中不可用

    所以我猜测,当gunicorn运行时,它将获得一个2版Python可执行文件


    有关更多详细信息,请参阅。

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

  • sudo pip3安装gunicorn
  • /usr/bin/gunicorn
    中,
    • 编辑第一行以读取
      #/usr/bin/python3
      (而不是
      python
      )和
    • 在任何与gunicorn--version匹配的地方更改gunicorn版本