Include whole directory in python setup.py data_files

The data_files parameter for installation accepts the input in the following format:

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

Is there a way to specify for me the entire data directory, so I do not need to specify each file individually and update it when I change the implementation in my project?

I tried using os.listdir() , but I do not know how to do this with relative paths, I could not use os.getcwd() or os.realpath(__file__) , because they do not correctly point to my repository root.

+7
source share
3 answers

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

First you need to get the directory path, so ...

Say you have this directory structure:

 cur_directory |- setup.py |- inner_dir |- file2.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__)) 

Then, to get the directory path relative to current_directory , just attach some other directories, for example:

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

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

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

Once you have this path you can do os.listdir

For completeness:

If you need a file path, in this case " file2.py " relative to setup.py , you can do:

 file2_path = os.path.join(cur_directory_path, 'inner_dir', 'file2.py') 
+1
source
 import glob for filename in glob.iglob('inner_dir/**/*', recursive=True): print (filename) 

By doing this, you get directly a list of files relative to the current directory.

+2
source

karelv has the right idea, but to answer the question more directly:

 from glob import glob setup( #... data_files = [ ('target_directory_1', glob('source_dir/**')), ('target_directory_2', glob('nested_source_dir/**/*', recursive=True)), # etc... ], #... ) 
0
source

Source: https://habr.com/ru/post/1210609/


All Articles