[Python.NET] Setting PYTHONPATH from code

Sharon Rozenblum Sharon.Rozenblum at sandisk.com
Wed Feb 23 15:44:44 CET 2011


Hi!

I need to run some python scripts at the same time where each one of them is in different location & their location is not in the machine environment variable.

So, I need to declare for each thread its PYTHONPATH internally...

Any idea?



-----Original Message-----
From: Lukáš Duběda [mailto:loocas at duber.cz]
Sent: Wednesday, February 23, 2011 3:44 PM
To: Sharon Rozenblum
Cc: Oleksii Bidiuk; pythondotnet at python.org
Subject: Re: [Python.NET] Setting PYTHONPATH from code

Hi Sharon,

I don't use Python.NET, but I use IronPython and this is a snippet
I use to add PYTHONPATH to the sys.path object:

I use system variables to do so (for various reasons) at the studio.

import sys
from System import Environment as sysEnv

PythonPath = sysEnv.GetEnvironmentVariable('PYTHONPATH')
sys.path.extend(PythonPath.split(';'))


Hope this helps.

Lukáš Duběda
Director
[T] +420 602 444 164

duber studio(tm)
[M] info at duber.cz
[W] http://www.duber.cz

[A] R.A.Dvorského 601, Praha 10
[A] 10900, Czech Republic, Europe

On 23.2.2011 14:37, Sharon Rozenblum wrote:
> Hi!
>
> Trying to do as you said:
>
>                         string  script  =  "import  sys\nsys.path.insert(0,  '"  +  modulePath  +  "')";
>
>                         IntPtr  pythonLock  =  PythonEngine.AcquireLock();
>                         PythonEngine.RunSimpleString(script);
>                         PythonEngine.ReleaseLock(pythonLock);
>
> And then:
>
>                         IntPtr  gs  =  PythonEngine.AcquireLock();
>                         pyportal  =  PythonEngine.ImportModule(moduleName);
>                         if  (pyportal  ==  null)
>                         {
>
>>
>                              }
>
>>
> I got Null value from the ImportModule.
>
> When setting the PYTHONPATH manually in the system variables with the
> “modulePath” and opening the VS is works.
>
> Any idea?
>
> Thanks,
>
> SAHRON
>
> *From:*Oleksii Bidiuk [mailto:oleksii.bidiuk at gmail.com]
> *Sent:* Thursday, February 10, 2011 2:21 PM
> *To:* Sharon Rozenblum
> *Cc:* pythondotnet at python.org
> *Subject:* Re: [Python.NET] Setting PYTHONPATH from code
>
> Hi Sharon,
>
> What do you want to achieve with this? If you want to provide path for
> loading your own modules you can change the sys.path value by e.g.
> running a 'initialization' script with
>
> import sys
>
> sys.path.insert(0, 'your/own/path')
>
> You can build up the script above in C# by inserting the path you need
> and then performing e.g. RunSimpleString(<script>) like
>
> string script = "import sys\nsys.path.insert(0, '" +
> <variable_with_path> + "')");
>
> IntPtr pythonLock = AcquireLock();
>
> RunSimpleString(script);
> ReleaseLock(pythonLock);
>
> Does this help?
>
> 2011/2/10 Sharon Rozenblum <Sharon.Rozenblum at sandisk.com
> <mailto:Sharon.Rozenblum at sandisk.com>>
>
> Hi!
>
> Can someone tell me how to set PYTHONPATH variable in code when working
> with Python.Runtime.dll without changing the value in the system
> variables before.
>
> Thanks
>
> SHARON
>
> ------------------------------------------------------------------------
>
> PLEASE NOTE: The information contained in this electronic mail message
> is intended only for the use of the designated recipient(s) named above.
> If the reader of this message is not the intended recipient, you are
> hereby notified that you have received this message in error and that
> any review, dissemination, distribution, or copying of this message is
> strictly prohibited. If you have received this communication in error,
> please notify the sender by telephone or e-mail (as shown above)
> immediately and destroy any and all copies of this message in your
> possession (whether hard copies or electronically stored copies).
>
>
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.org
> <mailto:PythonDotNet at python.org>
> http://mail.python.org/mailman/listinfo/pythondotnet
>
>
>
>
> --
> oleksii
>
>
>
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.org
> http://mail.python.org/mailman/listinfo/pythondotnet

PLEASE NOTE: The information contained in this electronic mail message is intended only for the use of the designated recipient(s) named above. If the reader of this message is not the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify the sender by telephone or e-mail (as shown above) immediately and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies).



More information about the PythonDotNet mailing list