开发者

PYTHONPATH environment variable...how do I make every subdirectory afterwards?

I curren开发者_如何学JAVAtly do this:

PYTHONPATH=/home/$USER:/home/$USER/respository:/home/$USER/repository/python-stuff

How can I make it so that the PYTHONPATH can include everything subdirectory?

PYTHONPATH = /home/$USER/....and-all-subdirectories


It's generally a bad idea to have on sys.path two paths one of which is the parent of the other -- assuming the sub-directory one contains an __init__.py file to mark it as a package, of course. If only the parent directory is in the path (and $PYTHONPATH is part of what sys.path is initialized with), modules in the subdirectory can be imported from the package, i.e., through just one filesystem path, avoiding the risk of a single module being imported under many distinct guises.

So why don't you just put __init__.py files in all subdirectories that need it, and use package imports?

While I think your request is a bad idea, it's certainly doable -- the Unix find command can easily list all subdirectories of a directory, one per line (find . -type d), and you can easily glue the lines together e.g. by piping find's output to tr '\n' :.


I did it like this:

import os
import sys

all_modules = {} #keeps track of the module names

def discoverModules():
    ''' Populates a dictionary holding every module name in the directory tree that this file is run '''    
    global all_modules

    for dirname, dirnames, filenames in os.walk('.'):
        # Advanced usage:
        # editing the 'dirnames' list will stop os.walk() from recursing into there.
        if '.git' in dirnames:
            # don't go into any .git directories.
            dirnames.remove('.git')

        # save path to all subdirectories in the sys.path so we can easily access them:
        for subdirname in dirnames:                    
            name = os.path.join(dirname, subdirname) # complete directory path
            sys.path.append(name) # add every entry to the sys.path for easy import

        # save path to all filenames:
        for filename in filenames:        
            # we want to save all the .py modules, but nothing else:
            if '.py' in filename and '.pyc' not in filename and '__init__' not in filename:
                moduleName = '' #this will hold the final module name

                # If on Mac or Linux system:
                if str(os.name) == 'posix': 
                    for element in dirname.split('\/'): # for each folder in the traversal
                        if element is not '.': # the first element is always a '.', so remove it
                            moduleName += element + '.' # add a '.' between each folder

                # If on windoze system:
                elif str(os.name) == 'nt': 
                    for element in dirname.split('\\'): # for each folder in the traversal
                        if element is not '.': # the first element is always a '.', so remove it
                            moduleName += element + '.' # add a '.' between each folder

                # Important to use rstrip, rather than strip. If you use strip, it will remove '.', 'p', and 'y' instead of just '.py'
                moduleName += filename.rstrip('.py') # finally, add the filename of the module to the name, minus the '.py' extension            
                all_modules[str(filename.rstrip('.py'))] = moduleName # add complete module name to the list of modules
    print 'Discovering Modules...Complete!'
    print 'Discovered ' + str(len(all_modules)) + ' Modules.'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜