由于 Web 部署而将绝对路径更改为相对路径(Python - Flask) [英] Changing absolute path to relative paths due to web deployment (Python - Flask)

查看:26
本文介绍了由于 Web 部署而将绝对路径更改为相对路径(Python - Flask)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个可以在本地运行的应用程序,我想将它部署到 Heroku.当我部署它时,我在 Heroku 日志中收到错误,关于找不到我在脚本中指定的文件夹(正如预期的那样,它们是绝对路径).

I've created an app that works locally and I would like to deploy it to Heroku. As I deploy it I get error in the Heroku logs about not finding the folders that I specified in my script(as expected, they are absolute paths).

我正在尝试进行更改,但均无效.我在服务器端使用烧瓶.我尝试使用所有 os.path 函数,但我无法让它工作.

I am trying to make changes but none of them work. I am using flask for the server side. I tried using all of the os.path functions but I can't get it to work.

我也在使用 Windows,如果它改变了任何东西(我认为应该这样做,因为它进入了 Heroku 服务器)

I am also using Windows if it changes anything (I think it should because it goes into the Heroku Server)

架构系统基本上是这样的:

The architecture system basically loos like this:

MonKeyGenerator
└── src
    └── MonkeyGenerator.py
└── server
    ├── static
        └── Images
            └── MonKeys
    └── server.py

一些例子:

server.py 中的这一行不再起作用(需要找到 src文件夹)

this line is in server.py does not work anymore (needs to find the src folder)

dir_of_interest = 'C:\\Users\\user\\PycharmProjects\\MonKeyGenerator\\src'

MonKeyGenerator.py 中的这一行也不起作用,因为它需要在服务器文件夹中找到一个名为MonKeys"的深层文件夹

Also this line is in MonKeyGenerator.py does not work because it needs to find a deep folder named 'MonKeys' inside the server folder

output_img = os.path.join("C:\\Users\\user\\PycharmProjects\\MonKeyGenerator\\server\\static\\images\\MonKeys", image_name)

推荐答案

根据 Heroku 文档,您可以阅读:

堆栈是由 Heroku 策划和维护的操作系统映像.堆栈通常基于现有的开源 Linux 发行版,例如 Ubuntu.

A stack is an operating system image that is curated and maintained by Heroku. Stacks are typically based on an existing open-source Linux distribution, such as Ubuntu.

您提供的代码示例表明您使用了特定于 Windows 的文件路径名,这可能会在您尝试在其他平台上运行代码时导致问题.

The code example you provided shows us that you have used file path names specific for Windows, and this may cause problems when you try to run your code on other platforms.

为了避免此类问题并使路径与平台无关,您应该使用 os.path 负责处理它.

To avoid this kind of problems and make paths platform-independent you should use os.path which takes care of it.

例如,您可以在 server.py 中使用:

You could use for example in your server.py:

# absolute path to this file
FILE_DIR = os.path.dirname(os.path.abspath(__file__))
# absolute path to this file's root directory
PARENT_DIR = os.path.join(FILE_DIR, os.pardir) 

然后:

dir_of_interest = os.path.join(PARENT_DIR, 'src')

阅读更多关于 os.path.join 查看如何处理 MonKeyGenerator.py

Read more about os.path.join to see how to handle the example in MonKeyGenerator.py

如果您使用的是 Python 3.4+,您还可以查看 pathlib.这应该会给你一些关于如何跟随你的代码示例的其余部分的直觉.我希望这会有所帮助.

If you are using Python 3.4+ you could also have a look at pathlib. This should give you some instinct on how to follow with the rest of your code example. I hope this helps.

这篇关于由于 Web 部署而将绝对路径更改为相对路径(Python - Flask)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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