在 python setup.py data_files 中包含整个目录 [英] Include entire directory in 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_files 中包含整个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!