[Python.NET] Setting PYTHONPATH from code

Sharon Rozenblum Sharon.Rozenblum at sandisk.com
Sun Feb 27 11:59:19 CET 2011


Did not understand the reply!

Do I need to change the PYTHONPATH from code?

Do I need to change the working directory in the C# program?

Why running the python script is not working?



From: Tribble, Brett [mailto:btribble at ea.com]
Sent: Wednesday, February 23, 2011 9:26 PM
To: Sharon Rozenblum; Oleksii Bidiuk
Cc: pythondotnet at python.org
Subject: RE: [Python.NET] Setting PYTHONPATH from code

This is the code I use all the time to append a dir to the system path and then load assemblies from that dir. (note, they’re not in the GAC {woot!})

if os.path.isfile(myFile):
    if not myDir in sys.path:
        sys.path.insert(0, myDir)

myFile is the name of the assembly dll ala: “C:/testdir/myAssembly.dll”
myDir would be: “C:/testdir”

I always make sure that the namespace in the compiled assembly is the same as the filename. This seems to make things run more smoothly.

From: pythondotnet-bounces+btribble=ea.com at python.org [mailto:pythondotnet-bounces+btribble=ea.com at python.org] On Behalf Of Sharon Rozenblum
Sent: Wednesday, February 23, 2011 5:37 AM
To: Oleksii Bidiuk
Cc: pythondotnet at python.org
Subject: Re: [Python.NET] Setting PYTHONPATH from code


Trying to do as you said:

            string script = "import sys\nsys.path.insert(0, '" + modulePath + "')";

            IntPtr pythonLock = PythonEngine.AcquireLock();

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?



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();

Does this help?

2011/2/10 Sharon Rozenblum <Sharon.Rozenblum at sandisk.com<mailto:Sharon.Rozenblum at sandisk.com>>

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.



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>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20110227/e32af2d4/attachment-0001.html>

More information about the PythonDotNet mailing list