Menu

PYHTONPATH

Auke Nauta
2021-09-02
2021-09-03
  • Auke Nauta

    Auke Nauta - 2021-09-02

    Hi,

    I have a CustomUO IronPython script which I use a lot.
    It -partly- contains this code:

    import sys
    sys.path.append(r"D:\Sources\VC2022\GSM_sim\xlw\x64_Release")
    import clr; 
    clr.AddReference(r'd-model_wrapper')
    Flowsheet.WriteMessage(str(sys.path))
    

    The output of the WriteMessage() is:
    ['.','D:\Sources\VC2022\GSM_sim\xlw\x64_Release']

    As the script will be used on multiple PC's it is not convenient to change the path to my managed dll each time.

    Therefore I would prefer if I could add this path to an environment variable (Windows 10).
    I read somewhere that the variable 'PYTHONPATH' should do it.
    But it does not make a difference, unfortunately.

    Can anyone help?

    Thanks and greetings,
    Auke Nauta

     
  • Daniel Medeiros

    Daniel Medeiros - 2021-09-02

    You can try

    from System.IO import Path
    filepath = Flowsheet.FlowsheetOptions.FilePath
    directory = Path.GetDirectoryName(filepath)
    print(directory)
    
     
    👍
    1
  • Daniel Medeiros

    Daniel Medeiros - 2021-09-02

    This assumes that your DLL is in the same directory as the sim file.

     
  • Auke Nauta

    Auke Nauta - 2021-09-02

    Hi Daniel,

    Thanks for your help.
    Unfortunately, in general, the DWSIM projects will be 'passed around' from user to user.
    As such, they will not reside at the same location as the DLLs.

    I would like to avoid every user to have to modify the scripts every time.

    I will look into it some more...

    Greetings, Auke

     
  • Daniel Medeiros

    Daniel Medeiros - 2021-09-02

    I understand. But will you be able to know where your DLL will reside in the other computers?

     
  • Auke Nauta

    Auke Nauta - 2021-09-02

    Well, not really.
    Therefore an environment variable would be ideal.
    This would need to be set just once in order for any project to be functional.

     
  • Daniel Medeiros

    Daniel Medeiros - 2021-09-02

    Usually you can get the paths for DLLs from registry or from env vars:

    import System
    
    dllpath = System.Environment.GetEnvironmentVariable("DLLPATH", System.EnvironmentVariableTarget.Machine)
    
    print(dllpath)
    
    import Microsoft.Win32
    
    from Microsoft.Win32 import Registry
    
    key = "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\MediaPlayer\\Preferences";
    valueName = "ObfuscatedSyncPlaylistsPath";
    
    value = Registry.GetValue(key, valueName, 'defaultValueIfNotFound')
    
    print(value) 
    
     
  • Auke Nauta

    Auke Nauta - 2021-09-03

    Hi Daniel,

    This works exactly as I intended.
    Thanks!!!

    Greetings,
    Auke

     
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.