1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How do I access variable file in a script?

Discussion in 'Alibre Script' started by R-man, Jun 30, 2019.

  1. R-man

    R-man Senior Member

    I have found a method that is so simple it's embarrassing! Not even worthy of a name like 'Hack3'!

    params.py
    Code:
    a = 22
    b = 33
    c = a + b
    test_script.py
    Code:
    import sys
    
    Folder = "C:\\Users\\dh\Documents\\RotoAlibre\\Alibre\\Hack3"
    execfile( Folder + "\\" + "params.py" )
    
    print a, b, c
    
    I saw references to using 'execfile' earlier but couldn't make it work and moved on. I since learned that because of the way Alibre Python is set up, the folder of execution is different the folder containing the file. So it's necessary to explicitly provide the correct path to the file.

    Obviously this method is limited to use with parameters that remain constant throughout a script, but that's all was looking for. Maybe this solution was obvious to some readers of this thread but my explanations of what I was trying to achieve were not adequate.
     
  2. R-man

    R-man Senior Member

    Stefan,

    I was not aware that the test "if p not in sys.path:" was not actually working. Thanks for pointing that out. I'll have to take a look at it, although as it stands I'll be abandoning that code as I implement my "embarrassingly" simple solution.
     
  3. idslk

    idslk Alibre Super User

    It seems that it's doing the same without the "p-check".
    It makes the sys.path longer each time you run the script. If the script is running, you can do the loop without making the path longer.
    So i think it would be the best if andy @ajayre can take a look on it.

    Edit: If you deactivate and reactivate (Launch) Alibre Script, the path gets reset. Running the script again, everything starts over.


    Regards
    Stefan
     
    Last edited: Jul 10, 2019
  4. idslk

    idslk Alibre Super User

    Result of runing "for i in sys.path: print i" from the console:

    >>>for i in sys.path: print i


    .
    C:\Program Files\Alibre Design\Program\Lib
    C:\Program Files\Alibre Design\Program\DLLs
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib\site-packages
    PythonLib/site-packages
    >>>

    Result of running "for i in sys.path: print i" from script:

    >>>
    .
    C:\Program Files\Alibre Design\Program\Lib
    C:\Program Files\Alibre Design\Program\DLLs
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib\site-packages
    PythonLib/site-packages
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib\site-packages
    PythonLib/site-packages
    >>>

    running the script again:

    >>>
    .
    C:\Program Files\Alibre Design\Program\Lib
    C:\Program Files\Alibre Design\Program\DLLs
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib\site-packages
    PythonLib/site-packages
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib\site-packages
    PythonLib/site-packages
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib\site-packages
    PythonLib/site-packages
    >>>

    Regards
    Stefan
     
  5. R-man

    R-man Senior Member

    This code isolates the problem. It seems that 3 specific paths are added on each time a script is run, regardless of whatever script itself is doing.

    Code:
    import sys
    
    print "---- Running the script: ---- "
    
    print len(sys.path)
    for i in sys.path:
      print i
    
    print
    
    Results of 3 runs:
    Code:
    >>>
    ---- Running the script: ----
    10
    .
    C:\Program Files\Alibre Design\Program\Lib
    C:\Program Files\Alibre Design\Program\DLLs
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib\site-packages
    PythonLib/site-packages
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib\site-packages
    PythonLib/site-packages
    >>>
    ---- Running the script: ----
    13
    .
    C:\Program Files\Alibre Design\Program\Lib
    C:\Program Files\Alibre Design\Program\DLLs
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib\site-packages
    PythonLib/site-packages
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib\site-packages
    PythonLib/site-packages
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib\site-packages
    PythonLib/site-packages
    >>>
    ---- Running the script: ----
    16
    .
    C:\Program Files\Alibre Design\Program\Lib
    C:\Program Files\Alibre Design\Program\DLLs
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib\site-packages
    PythonLib/site-packages
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib\site-packages
    PythonLib/site-packages
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib\site-packages
    PythonLib/site-packages
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT
    C:\PROGRAM FILES\ALIBRE DESIGN\PROGRAM\ADDONS\ALIBRESCRIPT\PythonLib\site-packages
    PythonLib/site-packages
    >>>
     
    Last edited: Jul 10, 2019
  6. idslk

    idslk Alibre Super User

    Hello R-man,

    if you take a look in post #24 you will see that it makes a difference to do it in the console or via script...
    Definitely a thing for Andy to judge if it needs to be corrected.

    Regards
    Stefan
     
  7. ajayre

    ajayre Alibre Super User

    If the script has been saved or loaded then use the built in variable 'ScriptFolder' to get the current script location. Then you can do:

    Code:
    execfile(ScriptFolder + "\\" + "params.py" )
    Andy
     
  8. ajayre

    ajayre Alibre Super User

    Looks like a bug. Andy
     
  9. NateLiqGrav

    NateLiqGrav Alibre Super User

    Just a note on paths.
    You can escape the special function of backslash with another backslash like so:
    Code:
    Folder = "C:\\Users\\dh\Documents\\RotoAlibre\\Alibre\\Hack3"
    Or convert to forward slash like so:
    Code:
    Folder = "C:/Users/dh/Documents/RotoAlibre/Alibre/Hack3
    Or just use an r in front of the string to use it in its raw form like so:
    Code:
    Folder = r"C:\Users\dh\Documents\RotoAlibre\Alibre\Hack3
     
  10. ajayre

    ajayre Alibre Super User

    OK, fixed, not sure if it will make it into the next AD release or not.

    Andy
     
    idslk and NateLiqGrav like this.

Share This Page