Python 为uWSGI Flask应用程序运行的目录中的文件提供服务

Python 为uWSGI Flask应用程序运行的目录中的文件提供服务,python,flask,uwsgi,Python,Flask,Uwsgi,我将我的uWSGI设置更改为使用Emperon进行多应用程序部署。加载“mypasswords.pwds”文件时使用了旧的设置,该文件保存在我的应用程序旁边,没有问题 现在与皇帝一起运行一切,我得到“没有这样的文件或目录”错误。这可能是因为我的设置,或者因为皇帝只是以不同的方式工作,我不确定是什么情况 目录树: Project thisismyapp.py passwords.pwds uWSGI皇帝的配置: [uwsgi] # Variables base = /var/ww

我将我的uWSGI设置更改为使用Emperon进行多应用程序部署。加载“mypasswords.pwds”文件时使用了旧的设置,该文件保存在我的应用程序旁边,没有问题

现在与皇帝一起运行一切,我得到“没有这样的文件或目录”错误。这可能是因为我的设置,或者因为皇帝只是以不同的方式工作,我不确定是什么情况

目录树:

Project
    thisismyapp.py
    passwords.pwds
uWSGI皇帝的配置:

[uwsgi]
# Variables
base = /var/www/Project

# Generic Config
home = %(base)/venv
pythonpath = %(base)
socket = /tmp/%n.sock
module = thisismyapp:app # Is this an issue?
logto = /var/log/uwsgi/%n.log
回溯:

*** Starting uWSGI 2.0.11.2 (64bit) on [Thu Feb  4 00:41:47 2016] ***
compiled with version: 4.9.2 on 24 October 2015 23:42:32
os: Linux-3.19.0-42-generic #48-Ubuntu SMP Thu Dec 17 22:54:45 UTC 2015
nodename: website
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /etc/uwsgi/apps-enabled
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
your processes number limit is 1868
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /tmp/project.sock fd 3
Python version: 2.7.9 (default, Apr  2 2015, 15:37:21)  [GCC 4.9.2]
Set PythonHome to /var/www/Project/venv
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x17355e0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 145520 bytes (142 KB) for 1 cores
*** Operational MODE: single process ***
added /var/www/Project/ to pythonpath.
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x17355e0 pid: 6772 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 6772)
spawned uWSGI worker 1 (pid: 6779, cores: 1)
[pid: 6779|app: 0|req: 1/1] 79.227.145.119 () {46 vars in 967 bytes} [Thu Feb  4 00:41:51 2016] GET / => generated 3552 bytes in 355 msecs (HTTP/1.1 200) 2 headers in 81 bytes (1 switches on core 0)
announcing my loyalty to the Emperor...
[Errno 2] No such file or directory: 'passwords.pwds'
Python文件访问:

f = open('passwords.pwds', 'r')
试试这个:

from os import path

passwords_location = path.join(path.dirname(path.realpath(__file__), 'passwords.pwds'))
with open(passwords_location) as f:
    do_something()

您最初使用相对位置的方式,因此如果您不在项目文件夹中,可能无法找到该文件。另外,将
块一起使用是处理文件的更好方法,它将确保在退出上下文块时关闭文件。

请发布整个回溯c我知道我忘记了smth:d这看起来没问题。。由于您的webui进程正在使用uwsgi成功运行,请检查您的
thisismyapp.py
,查看您是否传入了
passwords.pwds
文件的相对路径。添加了我访问该文件的方式。这对我来说很好,应该可以用,因为它以前确实可以用,而且在没有uwsgi烧瓶的情况下运行时也可以用。你是对的,我本来应该这样做的。我会尽快试一试。这对我来说还是有点奇怪。uwsgi是否从某个位置执行文件?我猜它是
当前工作目录:/etc/uwsgi/apps enabled
,但对此不确定。如果我错了,请纠正我。