简体   繁体   English

从另一个python文件获取变量而不导入

[英]Getting variables from another python file WITHOUT importing

I'm trying to write a cleanup script for my jenkins server. 我正在尝试为我的jenkins服务器编写一个清理脚本。 Without going into too many details, I want to get a variable from a file in each docroot and match it against something, in a for loop. 在没有涉及太多细节的情况下,我想从每个docroot中的文件中获取一个变量,并在for循环中将其与某些内容进行匹配。

So, as seemingly every single google result told me to, I tried adding the docroot to the sys.path.append(docroot), then used imp to find the module, imported it using 所以,看似每一个谷歌的结果告诉我,我尝试将docroot添加到sys.path.append(docroot),然后使用imp查找模块,使用它导入它

imp.find_module('python_vars')
from python_vars import git_branch

and used the variable. 并使用了变量。

The problem here is that it works for the first one, but then it's already loaded, and doesn't change in the loop. 这里的问题是它适用于第一个,但它已经加载,并且在循环中不会改变。 So I'm trying to remove the docroot from the sys.path at the end of each iteration, and then reload the module on the next one using 所以我试图在每次迭代结束时从sys.path中删除docroot,然后使用下一个模块重新加载模块

reload('python_vars')
from python_vars import git_branch

This gives me a 'reload() argument must be module'. 这给了我一个'reload()参数必须是模块'。 And

reload(python_vars)
from python_vars import git_branch

gives me 'NameError: name 'python_vars' is not defined' 给我'NameError:名称'python_vars'未定义'

Tbh, this seems like a horrible way to do it, is there not an easier way to get a variable from a file? Tbh,这似乎是一种可怕的方式,是不是有更简单的方法从文件中获取变量?

In php I would simply do something like include(docroot) in the loop and it would overwrite the variable in memory and it's done. 在php中,我只是在循环中执行类似include(docroot)的操作,它会覆盖内存中的变量并完成。

Here's the full loop, which I am fully aware is rubbish, but I'm just trying stuff out for now. 这是完整的循环,我完全清楚这是垃圾,但我现在只是想尝试一下。

if os.path.exists(docroot):
        sys.path.append(docroot)
        # If docroot exists need to check if it is meant to
        import imp
        try:
                imp.find_module('python_vars')
                from python_vars import git_branch
                remove_project = check_for_git_branch(docroot, git_branch)
        except ImportError:
                remove_project = True
                print 'importerror'
        try:
                imp.find_module('python_vars')
                reload(python_vars)
                from python_vars import git_branch
        except ImportError:
                print 'nope'

        sys.path.remove(docroot)

You can evaluate the contents of a Python file using execfile . 您可以使用execfile评估Python文件的内容。

eg Say this is the contents vars.py : 例如,这是内容vars.py

git_branch = 'feature/awesome'

Then script.py could do: 然后script.py可以这样做:

execfile('./vars.py')
print git_branch

Of course this has just been evaluated in the global scope, which could be a bit dangerous, so a safer option would be to pass in a dict to store the globals evaluated in vars.py : 当然这刚刚在全局范围内进行了评估,这可能有点危险,所以更安全的选择是传入一个dict来存储在vars.py评估的全局变量:

vars = {}
execfile('./vars.py',vars)
print vars['git_branch']

You need to actually grab a reference to the module to be able to reload it: 您需要实际获取对模块的引用才能重新加载它:

# First:
import python_vars

# Then:
do_something_with(python_vars.git_branch)

# Later on
reload(python_vars)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM