How to import all submodules?

前端 未结 9 1411
盖世英雄少女心
盖世英雄少女心 2020-11-27 16:57

I have a directory structure as follows:

| main.py
| scripts
|--| __init__.py
   | script1.py
   | script2.py
   | script3.py

From ma

9条回答
  •  天命终不由人
    2020-11-27 17:20

    I was writing a small personal library and adding new modules all the time so I wrote a shell script to look for scripts and create the __init__.py's. The script is executed just outside of the main directory for my package, pylux.

    I know it probably isn't the answer you're looking for, but it servered its purpose for me and it might be useful to someone else, too.

    #!/bin/bash
    
    echo 'Traversing folder hierarchy...'
    
    CWD=`pwd`
    
    
    for directory in `find pylux -type d -exec echo {} \;`;
    do
        cd $directory
        #echo Entering $directory
        echo -n "" > __init__.py
    
        for subdirectory in `find . -type d -maxdepth 1 -mindepth 1`;
        do
            subdirectory=`echo $subdirectory | cut -b 3-`
            #echo -n '    ' ...$subdirectory
            #echo -e '\t->\t' import $subdirectory
            echo import $subdirectory >> __init__.py
        done
    
        for pyfile in *.py ;
        do
            if [ $pyfile = $(echo __init__.py) ]; then
                continue
            fi
            #echo -n '    ' ...$pyfile
            #echo -e '\t->\t' import `echo $pyfile | cut -d . -f 1`
            echo import `echo $pyfile | cut -d . -f 1` >> __init__.py
        done
        cd $CWD
    
    done
    
    
    for directory in `find pylux -type d -exec echo {} \;`;
    do
        echo $directory/__init__.py:
        cat $directory/__init__.py | awk '{ print "\t"$0 }'
    done
    

提交回复
热议问题