Jump to content
Sign in to follow this  
fubbu

Run Python with system environment variables

Recommended Posts

Can i open from houdini python shell a system python with all data from it.

When i try open C:/Python27/python.exe from houdini python shell it always override system environment to houdini environment.

And i also must say i cant add system python path because it always run houdini python library first and i always get an error, i try write PYTHONHOME and all environments from help

https://www.sidefx.com/docs/houdini/ref/env.html, and nothing works.(Why i need that because i run external program by subprocces and it need system enviroment to run correctly)

Could some help?

pythonProblem.jpg

Share this post


Link to post
Share on other sites

Ok i found one thing. When i run houdini all commands are runing from hython not python now question is the same how run command from python (even this python in orginal houdini folder "C:\Program Files\Side Effects Software\Houdini 17.5.258\python27") because script only dont work in hython.

Share this post


Link to post
Share on other sites

Hi Daniel,

Interesting question, I have never tried relinking the default python libs! The wiki says it's not possible on windows (See the picture below).

Maybe you can get the original host env like described in this StackOverflow Post: https://stackoverflow.com/questions/23728250/start-new-subprocess-with-default-environment-variables

import subprocess
try:
    import _winreg as winreg
except ImportError:
    try:
        import winreg
    except ImportError:
        winreg = None

def env_keys(user=True):
    if user:
        root = winreg.HKEY_CURRENT_USER
        subkey = "Environment"
    else:
        root = winreg.HKEY_LOCAL_MACHINE
        subkey = r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
    return root, subkey

def get_env(name, user=True):
    root, subkey = env_keys(user)
    key = winreg.OpenKey(root, subkey, 0, winreg.KEY_READ)
    try:
        value, _ = winreg.QueryValueEx(key, name)
    except WindowsError:
        return ""
    value = winreg.ExpandEnvironmentStrings(value)
    return value

env_PATH = get_env("PATH", False)
### Check If ENV changes work
subprocess.Popen(["echo" ,"%PATH%"],env={"PATH":str(env_PATH)},shell=True)

### Start File With Original ENV
subprocess.Popen(["S:/python_2.7.5/python.exe" ,"S:/print_sys_path.py"],env={"PATH":str(env_PATH)},shell=True)

 

OverrideEnvWindowsOnly.jpg

Edited by LucaScheller

Share this post


Link to post
Share on other sites

Thx Luca for answere but it didnt work it always run from hython. But now i try something else. When i run houdini shell(not python shell) and I run virtual enviroment (which i create before) and then run python everything is ok. Hython is overrided and my scripts work. Everything starts working like in regular python. Now i try write script that will by run this virtual enviroment and run my scripts. If it will succeed i post solve here.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×