python 全局变量作用域混乱。这些变量是什么不可访问的?

python global variable scope confusion. What are these variables not accessible?

我看到这里有一些这样的问题,但我还没有找到一个与我所追求的相匹配的问题。

我有一个公用文件,我们称它为tools.py。在这个文件中,我有许多要使用的路径定义和一个 init_paths 函数来根据命令行参数设置一些关键路径:

def init_paths(args):
    global tools_dir, tools_src, tools_bin

    if args.tools_set:
        tools_dir = os.path.realpath(os.path.join(args.tools_set,"tools"))
    else:
        tools_dir = os.path.join(BAR_PATH, "tools")

FOO_PATH = os.path.realpath(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
BAR_PATH = os.path.join(FOO_PATH, "foobar")
tools_dir = none
tools_src = none
tools_bin = none

等...

我有一个主文件,在我想使用它们的地方将其命名为 main.py。

if __name__ == "__main__":
    args = parseArgs()
    from tools import init_paths
    init_paths(args)
    doStuffFunction(args.one, args.two, args.three)

为了确定,我省略了核心内容,但我相信这应该足以说明我的全球范围问题。当我 运行 这个:python main.py --tools-set=/path/to/tools 时,我期待对 init_paths 的调用,以设置一些我希望稍后在 doStuffFunction() 中使用的关键路径。

def doStuffFunction():
    searchPath = os.path.join(tools_dir, "folder")

这失败了:AttributeError:'NoneType' 对象没有以

结尾的属性

很确定这是因为它没有设置。但是为什么?

编辑

main.py

#!/usr/bin/env python
import sys
import os
import argparse
import glob
from tools import *


def parseArgs():
    parser = argparse.ArgumentParser(description="parse my args")
    parser.add_argument("--toolchain-root", type=str,default=None,help='specify toolchain directory')
    args = parser.parse_args()

    return args


def doStuffFunction():
    output = 'output'
    if not os.path.isdir(output):
        os.makedirs(output)
    gimmySugar(output)

def gimmySugar(output):
    fileList = []
    linkBook= {}

    searchPath = os.path.join(tools_BIN_ROOT,'gcc-4.8.5')
    for root, dirs, files in os.walk(searchPath):
        for libFile in glob.glob(root+'/*.so*'):
            fileList.append(libFile)
            if os.path.islink(libFile):
                linksWith = os.readlink(libFile)
                linkBook[libFile] = linksWith

if __name__ == "__main__":
    # script was called directly from the command line
    args = parseArgs()
    from tools import init_settings
    init_settings(args)
    doStuffFunction()

tools.py

import os

def init_settings(args):
    global tools_DIR, tools_SRC_ROOT, tools_OBJ_ROOT, tools_BIN_ROOT
    if args.toolchain_root:
        tools_DIR = os.path.realpath(os.path.join(args.toolchain_root, "toolchain"))
    else:
        tools_DIR = os.path.join(USER_DIR, "")
    tools_SRC_ROOT = os.path.join(tools_DIR, "src")
    tools_OBJ_ROOT = os.path.join(tools_DIR, "obj")
    tools_BIN_ROOT = os.path.join(tools_DIR, "bin")


ROOT_PATH = os.path.realpath(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
OUTPUT_PATH = os.path.join(ROOT_PATH, "outputs")
BUILD_PATH = os.path.join(OUTPUT_PATH, "build")
USER_DIR = "/usr/lib64/"
tools_DIR = None
tools_SRC_ROOT = None
tools_OBJ_ROOT = None
tools_BIN_ROOT = None
#This line will failed
searchPath = os.path.join(tools_BIN_ROOT,'gcc-4.8.5')

全局变量的作用域是模块级的,tools_BIN_ROOT不跨模块就不会共享

变量tools_BIN_ROOT仅在tools.py中是全局变量。相反,main.py 不包含 tools.py.

中的任何全局变量

为了检查这一点,您可以在两个文件中使用 print(globals())


Do not use global variable if possible.

这是一个简单的解决方法。

(我强烈建议您通过配置或 OOP)

重构您的代码

tools.py

def get_tools_BIN_ROOT():
    return tools_BIN_ROOT

os.py

from tools import get_tools_BIN_ROOT
searchPath = os.path.join(get_tools_BIN_ROOT(),'gcc-4.8.5')