在基于Azure烧瓶的python应用程序中何处提及文件的路径 [英] Where to mention path of files in azure flask based python app

查看:38
本文介绍了在基于Azure烧瓶的python应用程序中何处提及文件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 Flask 的python网络应用,该应用在azure上运行良好.我进行了一些更改,并添加了一个包含某些项目配置的 input.json 文件.我在项目根目录中有以下文件:

I have a Flask based python web app which is running fine on azure. I have made some changes and added a input.json file which contains some project configuration. I have below files in the project root directory:

app.py -> Main python file
input.json -> Input configuration file used by app.py
requirements.txt -> This file contains python packages used by project
web.config -> Azure web config file

下面是 web.config 文件

<configuration>
  <appSettings>
    <add key="WSGI_HANDLER" value="app.wsgi_app"/>
    <add key="PYTHONPATH" value="D:\home\site\wwwroot"/>
    <add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log"/>
  </appSettings>
  <system.webServer>
    <handlers>
      <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="D:\home\Python364x64\python.exe|D:\home\Python364x64\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>
    </handlers>
  </system.webServer>
</configuration>

在我的本地计算机上一切正常.但是,当我将其部署在azure上时,API没有运行.我打开kudu控制台并检查 wfastcgi.log 的日志,它显示错误:

This is all working fine on my local machine. But when I deployed this on azure, API was not running. I opened the kudu console and checked the logs of wfastcgi.log, it was showing error:

没有这样的文件或目录:'D:\\ home \\ python364x64 \\ input.json'

问题是 input.json 位于 D:\ home \ site \ wwwroot> 内部,但它会自动选择错误的路径,从而使代码失败.

The problem is input.json is located inside D:\home\site\wwwroot> but it is automatically picking up the wrong path and thus failing the code.

1.为什么它在错误的路径中查找 input.json 文件??

1. Why it is looking for input.json file in wrong path.?

2.如何为 input.json 文件提到正确的路径??

2. How can I mention the correct path for input.json file.?

3.如果将来会添加更多文件,应该在哪里正确提及所有文件的路径.我们是否必须在 web.config 文件中提及这一点?

3. If in future, I'll add more files, where should I correctly mention path of all the files. Do we have to mention this in web.config file.?

谢谢!!

推荐答案

请确保您需要使用相对路径,如下所示:

pretty sure you need to use relative path, something like this:

os.path.join(os.path.dirname( __file__ ), 'input.json')

,因此您将在python文件所在的目录中获得路径.这与web.config无关.您的python文件尝试加载此文件(这是我的假设).因此,它是在相对于解释器而不是您的python文件的路径中寻找它的.

so you will get path in the same directory your python file is located. And this is not related to web.config. your python file attempts to load this file (this is my assumption). hence its looking for it in the path relative to the interpreter, not your python file.

这篇关于在基于Azure烧瓶的python应用程序中何处提及文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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