Pass variable between python scripts

前端 未结 5 1105
北恋
北恋 2020-12-02 18:37

I\'m sure this is very simple but I\'ve been unable to get it working correctly. I need to have my main python script call another python script and pass variables from the

相关标签:
5条回答
  • 2020-12-02 19:11

    use the following script:

    first.py:

    x=5
    

    second.py

    import first
    print first.x
    

    this will print the x value. Always imported script data should be referenced with the script name, like in first.x

    0 讨论(0)
  • 2020-12-02 19:17

    Try use exec Python3.5:

    first.py

    x=5
    exec(open('second.py').read())
    

    second.py

    print(x)
    

    You can also pass x by using:

    x=5
    myVars = {'x':x}
    exec(open('second.py').read(), myVars)
    

    Not sure if this is a good way.

    0 讨论(0)
  • 2020-12-02 19:19

    When you call a script, the calling script can access the namespace of the called script. (In your case, first can access the namespace of second.) However, what you are asking for is the other way around. Your variable is defined in the calling script, and you want the called script to access the caller's namespace.

    An answer is already stated in this SO post, in the question itself:

    Access namespace of calling module

    But I will just explain it here in your context.

    To get what you want in your case, start off the called script with the following line:

    from __main__ import *
    

    This allows it to access the namespace (all variables and functions) of the caller script.

    So now your calling script is, as before:

    x=5
    import second
    

    and the called script is:

    from __main__ import *
    print x
    

    This should work fine.

    0 讨论(0)
  • 2020-12-02 19:31

    To avoid namespace pollution, import the variables you want individually: from __main__ import x, and so on. Otherwise you'll end up with naming conflicts you weren't aware of.

    0 讨论(0)
  • 2020-12-02 19:31

    Finally,

    I created a package for Python to solve this problem.


    Install Guli from PIP.

    $ pip install guli
    

    Guli doesn't require installing any additional PIP package.

    With the package you can

    Guli can be used to pass between different Python scripts, between many processes or at the same script. pass variables between main Process and another (Multiprocess) Process.

    • Pass variables between different Python scripts.
    • Pass variables between 'Main Process' and another (Multiprocess) Process.
    • Use variables at the same script.
    • Create / Delete / Edit - GuliVariables.

    Example

    import guli
    import multiprocessing
    
    string = guli.GuliVariable("hello").get()
    print(string) # returns empty string ""
    
    def my_function():
      ''' change the value from another process '''
      guli.GuliVariable("hello").setValue(4)
    
    multiprocessing.Process(target=my_function).start()
    
    import time
    time.sleep(0.01) # delay after process to catch the update
    
    
    string = guli.GuliVariable("hello").get()
    print(string) # returns "success!!!"
    

    Hope I solved the problem for many people!

    0 讨论(0)
提交回复
热议问题