除非路径是绝对路径,否则引用Flask应用程序中的目录不起作用

时间:2021-09-15 02:07:12

I downloaded nltk data into the data directory in my Flask app. The views reside in a blueprint in another directory on the same level as the data directory. In the view I'm trying to set the path to the data, but it doesn't work.

我将nltk数据下载到我的Flask应用程序的数据目录中。视图驻留在与数据目录相同级别的另一个目录中的蓝图中。在视图中,我正在尝试设置数据的路径,但它不起作用。

nltk.data.path.append('../nltk_data/')

This doesn't work. If I use the whole path, it does work.

这不起作用。如果我使用整个路径,它确实有效。

nltk.data.path.append('/home/username/myapp/app/nltk_data/')

Why does the first form not work? How can I refer to the location of the data correctly?

为什么第一种形式不起作用?如何正确引用数据的位置?

1 个解决方案

#1


10  

In Python (and most languages), where the code resides in a package is different than what the working directory is when running a program. All relative paths are relative to the current working directory, not the code file it's written in. So you would use the relative path nltk_data/ even from a blueprint, or you would use the absolute path and leave no ambiguity.

在Python(和大多数语言)中,代码驻留在程序包中的位置与运行程序时的工作目录不同。所有相对路径都是相对于当前工作目录的,而不是它所写入的代码文件。因此,您将使用相对路径nltk_data /甚至来自蓝图,或者您将使用绝对路径并且不会产生歧义。

The root_path attribute on an app (or blueprint) refers to the package directory for the app (or blueprint). Join your relative path to that to get the absolute path.

应用程序(或蓝图)上的root_path属性引用应用程序(或蓝图)的包目录。加入你的相对路径来获得绝对路径。

resource_path = os.path.join(app.root_path, 'enltk_data')

There's probably no reason to be appending this folder every time you call a view. I'm not familiar with nltk specifically, but there's probably a way to structure this so you set up the data path once when you create your app.

每次调用视图时,可能没有理由附加此文件夹。我并不熟悉nltk,但是可能有一种方法来构建它,以便在创建应用程序时设置一次数据路径。


project    /    app    /    blueprint                       /    data                            ^ join with root_path to get here                ^ app.root_path always points here, no matter where cwd is^ current working directory

#1


10  

In Python (and most languages), where the code resides in a package is different than what the working directory is when running a program. All relative paths are relative to the current working directory, not the code file it's written in. So you would use the relative path nltk_data/ even from a blueprint, or you would use the absolute path and leave no ambiguity.

在Python(和大多数语言)中,代码驻留在程序包中的位置与运行程序时的工作目录不同。所有相对路径都是相对于当前工作目录的,而不是它所写入的代码文件。因此,您将使用相对路径nltk_data /甚至来自蓝图,或者您将使用绝对路径并且不会产生歧义。

The root_path attribute on an app (or blueprint) refers to the package directory for the app (or blueprint). Join your relative path to that to get the absolute path.

应用程序(或蓝图)上的root_path属性引用应用程序(或蓝图)的包目录。加入你的相对路径来获得绝对路径。

resource_path = os.path.join(app.root_path, 'enltk_data')

There's probably no reason to be appending this folder every time you call a view. I'm not familiar with nltk specifically, but there's probably a way to structure this so you set up the data path once when you create your app.

每次调用视图时,可能没有理由附加此文件夹。我并不熟悉nltk,但是可能有一种方法来构建它,以便在创建应用程序时设置一次数据路径。


project    /    app    /    blueprint                       /    data                            ^ join with root_path to get here                ^ app.root_path always points here, no matter where cwd is^ current working directory