与WSGI和Apache运行时,Python的工作目录 [英] Python's working directory when running with WSGI and Apache

查看:460
本文介绍了与WSGI和Apache运行时,Python的工作目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,除其他事项外,将查询数据库和数据创建一个Excel US preadsheet。我需要保存在S preadsheet到服务器的磁盘上的文件发送到客户机之前。我使用的瓶框架和openpyxl产生S preadsheet。瓶的开发服务器上运行时,一切工作正常,但真正的服务器Apache2中WSGI。当我在它上面运行,一个Permission denied,当它试图保存在S preadsheet引发错误。我不知道什么是Python的工作目录中的Apache / WSGI运行时。

I have a web application that, among other things, will query a database and create an Excel spreadsheet with the data. I need to save the spreadsheet to the server's disk before sending the file to the client machine. I'm using the Flask framework and openpyxl to generate the spreadsheet. Everything works fine when running on Flask's dev server, but the real server is Apache2 with WSGI. When I run it there, a "Permission Denied" error is raised when it tries to save the spreadsheet. I don't know what Python's working directory is when running in Apache/WSGI.

有没有一种方法,也许在WSGI配置文件,改变工作目录,或以某种方式控制在那里将保存到?如果可能的话,我想使用相对路径保存(它使code更轻便),这就是为什么改变工作目录是最好的解决方案。

Is there a way, maybe in the WSGI config file, to change the working directory, or somehow control where it will save to? If possible, I'd like to use relative paths for saving (it makes the code more portable) which is why changing the working directory is the best solution.

推荐答案

您不应该改变工作目录。

You should not change working directory.

使用:

import os
here = os.path.dirname(__file__)

变量'这里',然后将包含如该code文件所在的目录。然后,您可以constuct的事情绝对路径相对的。

The variable 'here' will then contain the directory where that code file is located. You can then constuct absolute paths for things relative to that.

database = os.path.join(here, 'database.db')

请注意,您的code在Apache下运行还是用户需求读取该目录/写访问。

Do note that the user your code runs under in Apache still needs read/write access to that directory.

与往常一样,请务必阅读文档:

As always, make sure you read the documentation:

http://$c$c.google.com/p/modwsgi/wiki/ApplicationIssues#Application_Working_Directory
  的http://$c$c.google.com/p/modwsgi/wiki/ApplicationIssues#Access_Rights_Of_Apache_User

这篇关于与WSGI和Apache运行时,Python的工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆