pythonpath环境变量是什么_PYTHONPATH环境变量…以后如何生成每个子目录?

我是这样做的: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.'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值