在 python setup.py data

编程入门 行业动态 更新时间:2024-10-25 18:31:37
本文介绍了在 python setup.py data_files 中包含整个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

设置的 data_files 参数采用以下格式的输入:

The data_files parameter for setup takes input in the following format:

setup(... data_files = [(target_directory, [list of files to be put there])] ....)

有没有办法让我指定整个数据目录,这样我就不必单独命名每个文件并在更改项目中的实现时更新它?

Is there a way for me to specify an entire directory of data instead, so I don't have to name each file individually and update it as I change implementation in my project?

我尝试使用 os.listdir(),但我不知道如何使用相对路径来做到这一点,我无法使用 os.getcwd() 或 os.realpath(__file__) 因为它们没有正确指向我的存储库根目录.

I attempted to use os.listdir(), but I don't know how to do that with relative paths, I couldn't use os.getcwd() or os.realpath(__file__) since those don't point to my repository root correctly.

推荐答案

我不知道如何使用相对路径来做到这一点

I don't know how to do that with relative paths

需要先获取目录的路径,所以...

You need to get the path of the directory first, so...

假设你有这个目录结构:

Say you have this directory structure:

cur_directory |- setup.py |- inner_dir |- file2.py

要获取当前文件的目录(在本例中为 setup.py),请使用:

To get the directory of the current file (in this case setup.py), use this:

cur_directory_path = os.path.abspath(os.path.dirname(__file__))

然后,要获得相对于current_directory的目录路径,只需加入一些其他目录,例如:

Then, to get a directory path relative to current_directory, just join some other directories, eg:

inner_dir_path = os.path.join(cur_directory_path, 'inner_dir')

如果你想上移一个目录,只需使用..",例如:

If you want to move up a directory, just use "..", for example:

parent_dir_path = os.path.join(current_directory, '..')

一旦你有了那个路径,你就可以做 os.listdir

Once you have that path, you can do os.listdir

为了完整性:

如果你想要一个文件的路径,在这种情况下file2.py"相对于setup.py,你可以这样做:

If you want the path of a file, in this case "file2.py" relative to setup.py, you could do:

file2_path = os.path.join(cur_directory_path, 'inner_dir', 'file2.py')

更多推荐

在 python setup.py data

本文发布于:2023-07-05 15:55:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1039144.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:setup   python   data   py

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!